在 VC6 中使用 GdiPlus-使用

下面用 VC6 来写一个 GdiPlus 的 Demo 工程

Demo_GdiPlus 运行效果图

 

Step1:新建一个名为 Demo_GdiPlus 的 MFC AppWizard(exe) 工程

操作步骤:
(1)主菜单File->New...,选择 Projects 选项卡;
(2)在工程类型列表中选中 MFC AppWizard(exe);
(3)Project name 填入 Demo_GdiPlus,按 OK 进入下一页;
(4)选择单文档(Single document)类型的程序框架,按 Finish 完成工程创建工作。


Step2:添加头文件声明

在 StdAfx.h 中添加以下代码:

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
typedef unsigned long ULONG_PTR, *PULONG_PTR;
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "GdiPlus.lib")

 

Step3:在 CDemo_GdiPlusApp 中增加成员变量 m_gdiplusToken,并在构造函数中进行初始化

class CDemo_GdiPlusApp : public CWinApp
{
private:
 ULONG_PTR m_gdiplusToken;
 // …… ……
};

CDemo_GdiPlusApp::CDemo_GdiPlusApp()
{
 // TODO: add construction code here,
 // Place all significant initialization in InitInstance
 m_gdiplusToken = NULL;
}

 

Step4:添加安装和卸载 GdiPlus 的代码
通过 ClassWizard 在 CDemo_GdiPlusApp 中增加成员函数

// .h 中的声明
virtual BOOL InitInstance();
virtual int ExitInstance();

// .cpp 中的实现
BOOL CDemo_GdiPlusApp::InitInstance()
{
 // 加载 GdiPlus
 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
 Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
 // …… ……
}

 
int CDemo_GdiPlusApp::ExitInstance() 
{
 // TODO: Add your specialized code here and/or call the base class
 // 卸载 GdiPlus
 if (m_gdiplusToken)
  Gdiplus::GdiplusShutdown(m_gdiplusToken);  
 return CWinApp::ExitInstance();
}

 

Step5:找到 CDemo_GdiPlusView::OnDraw() 函数,在里面添加一段 GdiPlus 的绘图代码

void CDemo_GdiPlusView::OnDraw(CDC* pDC)
{
 CDemo_GdiPlusDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 // TODO: add draw code for native data here
 
 Graphics graphics(pDC->GetSafeHdc());
 
 // Pen can also be constructed using a brush or another pen.  There is a second parameter - a width which defaults to 1.0f
 Pen blue (Color(255, 0, 0, 255));
 Pen red  (Color(255, 255, 0, 0));
 
 int y = 256;
 for (int x = 0; x < 256; x += 5)
 {
  graphics.DrawLine(&blue, 0, y, x, 0);
  graphics.DrawLine(&red, 256, x, y, 256);  
  y -= 5;
 }  
}


编译运行,Demo 程序完成。留住这个 Demo 程序,今后我们会利用它进行更加深入的 GdiPlus 学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值