C++ GDI+绘制界面

GDI:(Graphics Device Interfase)图形设备接口,是一个应用程序与输出设备之间的中介。

运行环境:Gdiplus.dll 包含在Windows系统中。【在system32中包含Gdiplus.dll文件】。

1.包含头文件:#include <Gdiplus.h>

2.链接库文件:属性->配置->连接器->输入->附加依赖项->Gdiplus.lib;

3.定义成员变量:ULONG_PTR m_gdiplusToken;

4.在CMYAPP类的函数InitInstance()中加入:

        GdiplusStartupInput gdiplusStartupInput;

        GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

5.在CMYAPP类的函数ExitInstance()中加入:

        GdiplusShutdown(m_gdiplusToken);

6.一个Text属性结构体:

typedef struct{

        RectF rectF;//文字区域

        Color color;//文字颜色

        CString text;//文本

        int fontSize;//文字大小

        Gdiplus::StringAlignment styleX; //水平对齐方式

        Gdiplus::StringAlignment styleY; //垂直对齐方式

        Gdiplus::StringFormatFlags styleWrap; //是否换行

        int fontArial; //是否粗体

        CString fontStyle; //字体名称

}m_Text;

7.一个Image属性结构体:

typedef struct{

        RectF rectF;//图片区域

        CString szPath; //图片路径

}m_Image;

 8.绘制文字:

Void SetGDIFont(m_Text text, HDC hdc)

{

        Graphics      graphics(hdc);

        SolidBrush    brush(text.color); //字体颜色

        FontFamily    fontFamily(text.fontStyle);

        Gdiplus::Font font(&fontFamily, text.fontSize, text.fontArial, UnitPixel);

        RectF         rectF(text.rectF);

        graphics.SetTextRenderingHint(TextRenderingHintAntiAlias); //平滑处理

        StringFormat stringformat = new StringFormat;

        stringformat.SetAlignment(text.styleX);

        stringformat.SetLineAlignment(text.styleY);

        graphics.DrawString(text.text,-1,&font,rectF,&stringformat,&brush); //绘制

        graphics.ReleaseHDC(hdc);

}

9.绘制图片:

 void SetGDIImage(m_Image image, HDC hdc)

{

        Graphics      graphics(hdc);

        Image image(szImagePath,FALSE);

        graphics.DrawImage(&image, image.rectF.left, image.rectF.top,

                 image.rectF.right-rectF.left, image.rectF.bottom-rectF.top); //绘制

        graphics.ReleaseHDC(hdc);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑兔子JH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值