初始化GDI+库进行图形图像处理

在程序中使用GDI+的步骤为:

1,  添加库头文件,在include path中加入“C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\include\\”,具体要根据SDK的安装路径来定,也可以在代码中直接#include <C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\include\\gdiplus.h>引用。

2,  添加Lib库,直接引用#pragma comment(lib,"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0A\\Lib\\GDIplus.lib")

3,  使用GDI+命名空间using namespace Gdiplus

4,  GDI资源初始与销毁。在使用GDI+资源之间,应该通过GdiplusStartup函数进行GDI+系统资源的初始化,在程序结束前,调用GdiplusShutdown函数进行GDI+资源的销毁。在App类的InitInstanceExitInstance或析购函数中分别调用。

code 示例如下:

CGDI_Plus_LearningApp theApp;
ULONG_PTR gdiplusToken; //定义全局变量,表明对GDI+的一个引用

/
// CGDI_Plus_LearningApp initialization

BOOL CGDI_Plus_LearningApp::InitInstance()
{
 AfxEnableControlContainer();

 // Standard initialization
 // If you are not using these features and wish to reduce the size
 //  of your final executable, you should remove from the following
 //  the specific initialization routines you do not need.

#ifdef _AFXDLL
 Enable3dControls();   // Call this when using MFC in a shared DLL
#else
 Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

 // Change the registry key under which our settings are stored.
 // TODO: You should modify this string to be something appropriate
 // such as the name of your company or organization.
 SetRegistryKey(_T("Local AppWizard-Generated Applications"));


 // To create the main window, this code creates a new frame window
 // object and then sets it as the application's main window object.
 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
 CMainFrame* pFrame = new CMainFrame;
 m_pMainWnd = pFrame;

 // create and load the frame with its resources

 pFrame->LoadFrame(IDR_MAINFRAME,
  WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
  NULL);

 // The one and only window has been initialized, so show and update it.
 pFrame->ShowWindow(SW_SHOW);
 pFrame->UpdateWindow();

 return TRUE;
}

int CGDI_Plus_LearningApp::ExitInstance()
{
 // TODO: Add your specialized code here and/or call the base class
 Gdiplus::GdiplusShutdown(gdiplusToken); //也可以在析构函数中调用此函数
 return CWinApp::ExitInstance();
}

 

Visual C++6.0使用GDI+的一般方法 1. 载解压GDI+开发包; 2. 正确设置include & lib 目录; 3. stdafx.h 添加: #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include 4. 程序中添加GDI+的包含文件gdiplus.h以及附加的类gdiplus.lib。 通常gdiplus.h包含文件添加在应用程序的stdafx.h文件中,而gdiplus.lib可用两种进行添加: 第一种是直接在stdafx.h文件中添加下列语句: #pragma comment( lib, "gdiplus.lib" ) 另一种方法是: 在VC.net中添加文件在:项目菜单->属性->链接器->输入 举个例子: (1)在应用程序项目的应用类中,添加一个成员变量,如下列代码: ULONG_PTR m_gdiplusToken; 其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。 (2)在应用类中添加ExitInstance的重载,并添加下列代码用来关闭GDI+: int CGDITestApp::ExitInstance() { Gdiplus::GdiplusShutdown(m_gdiplusToken); return CWinApp::ExitInstance(); } (3)在应用类的InitInstance函数中添加GDI+初始化代码: 注意:下面这些GDI+初始化代码必须放在m_pMainWnd->UpdateWindow();之前。 CWinApp::InitInstance(); Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL); (4)在需要绘图的窗口或视图类中添加GDI+的绘制代码。 下面分别就单文档和基于对话框应用程序为例,说明使用GDI+的一般过程和方法。 1. 在单文档应用程序中使用GDI+ 在上面的过程中,我们就是以一个单文档应用程序Ex_GDIPlus作为示例的。下面列出第4步所涉及的代码: void CGDITestView::OnDraw(CDC* pDC) { CGDITestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here usingnamespace Gdiplus; Graphics graphics(pDC->m_hDC); Pen newPen(Color(255,0,0),3); HatchBrush newBrush(HatchStyleCross,Color(255,0,255,0),Color(255,0,0,255));//创建一个填充画刷,前景色为绿色,背景色为蓝色 graphics.DrawRectangle(&newPen,50,50,100,60);// 在(50,50)处绘制一个长为100,高为60的矩形 graphics.FillRectangle(&newBrush,50,50,100,60); // 在(50,50)处填充一个长为100,高为60的矩形区域 } 编译并运行,结果如图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值