设为首页 加入收藏

TOP

关于GDI+ (三)
2014-11-23 22:30:37 来源: 作者: 【 】 浏览:5
Tags:关于 GDI
py to clipboardprint VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
SolidBrush brush(Color(255, 0, 0, 255));
FontFamily fontFamily(L"Times New Roman");
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
PointF pointF(10.0f, 20.0f);

graphics.DrawString(L"Hello World!", -1, &font, pointF, &brush);
}
VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
SolidBrush brush(Color(255, 0, 0, 255));
FontFamily fontFamily(L"Times New Roman");
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
PointF pointF(10.0f, 20.0f);

graphics.DrawString(L"Hello World!", -1, &font, pointF, &brush);
}


2 自己写的一个显示图片的简单示例
基于mfc的对话框应用,点一下按钮,显示一张图片. 只显示关键代码

view plaincopy to clipboardprint void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
// TODO: Add your control notification handler code here
HDC hdc = this->GetDC()->m_hDC;
Graphics graphics(hdc);
Image image(L"c:\\image\\ip.jpg");
graphics.DrawImage(&image, 200, 200);
}
void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
// TODO: Add your control notification handler code here
HDC hdc = this->GetDC()->m_hDC;
Graphics graphics(hdc);
Image image(L"c:\\image\\ip.jpg");
graphics.DrawImage(&image, 200, 200);
}


有以下几点要说明,
第一点, Image只支持通过路径和流的形式提供实例初始化,不支持资源句柄, 如果要用资源句柄的形式,可以先把资源数据读到流里,这里不说了.
第二点, 关于DrawImage,有很多重载函数, 提供了几乎你能想到的所有功能,我这里用了下面这个重载
Status DrawImage( Image *image,INT x,INT y);
也就是在指定位置显示加载的图像.

3 渐变效果

前面说了,GDI+支持渐变, 分为两种, linear gradient和path gradient, 这里讨论第一种.
linear gradient又分为

Horizontal Linear Gradients,

Customizing Linear Gradients,

Diagonal Linear Gradients

三种. 这些比较好理解. 下面的代码示例来自msdn,我做了一些修改.

view plaincopy to clipboardprint void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
// TODO: Add your control notification handler code here
HDC hdc = this->GetDC()->m_hDC;
Graphics graphics(hdc);
LinearGradientBrush linearBrush(Point(0, 10), Point(200, 10),
Color(255,255,0,0),Color(255,0,0,255));

Pen pen(&linearBrush, 4);
graphics.DrawLine(&pen, 100,100, 200, 100);
graphics.FillEllipse(&linearBrush, 100, 150, 200, 100);
graphics.FillRectangle(&linearBrush, 100, 255, 500, 30);
}
void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
// TODO: Add your control notification handler code here
HDC hdc = this->GetDC()->m_hDC;
Graphics graphics(hdc);
LinearGradientBrush linearBrush(Point(0, 10), Point(200, 10),
Color(255,255,0,0),Color(255,0,0,255));

Pen pen(&linearBrush, 4);
graphics.DrawLine(&pen, 100,100, 200, 100);
graphics.FillEllipse(&linearBrush, 100, 150, 200, 100);
graphics.FillRectangle(&linearBrush, 100, 255, 500, 30);
}

程序运行的效果如下图

\


附: GDI+与GDI混合编程
GDI+提供了一种机制, 可以和GDI混合使用, 主要是利用Graphics中的ReleaseHDC和GetHDC, 下面的代码示例来自msdn:


view plaincopy to clipboardprint VOID Example_GetReleaseHDC(Graphics* g)
{
Pen pen(Color(255, 0, 0, 255));
g->DrawEllipse(&pen, 10, 10, 100, 50); // GDI+
HDC hdc = g->GetHDC();
// Make GDI calls, but don't call any methods
// on g until after the call to ReleaseHDC.
Rectangle(hdc, 120, 10, 220, 60); // GDI
g->ReleaseHDC(hdc);
// Ok to call methods on g again.
g->DrawLine(&pen, 240, 10, 340, 60);
}

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 木杆上的蚂蚁问题 下一篇获取操作系统CPU及内存使用信息的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: