GDI+实践之路(一)

        当我写下这个标题的时候,你是不是会觉得我很花心呢?一项技术都没有摸透,就去看其他的技术了。我又何尝不想专注于一个方面呢?不过,跟着需求走就意味着“一日看尽长安花”了,目不暇接无所谓,脑袋瓜跟上了就好,你说呢?
        实践之路的第一步是急着动手去做吗?不是的,应该对GDI+有个概括的了解才行。GDI+是GDI(Graphic Device Interface)的扩展版本,也是Windows XP和Windows Server 2003的组成部分。使用GDI+提供的API可以很轻松的进行二维图象的处理,并且编写的程序对输出设备的依赖性也相当的低。GDI+主要包括三个组成部分:二维矢量图形、图片资源的显示处理和文本的显示。
        有了这些概括的认识之后,我们就可以动手去写第一个GDI+程序了。GDI+程序设计属于图象处理的范畴,跟Windows编程也有着密切的联系,一些Windows编程的基础知识是必须具备的,至少懂得如何去依靠SDK写一个最简单的窗口程序,当然如果你是在.NET下面使用GDI+的话,WinForm的编程则会简单得多了。这里讲到关于GDI+的内容都是在基于C++的Win32开发环境下的。
        好了,言归正传。要编写GDI+的程序,首先得有GDI+的library及头文件,根据Microsoft的文档介绍,从最新的Platform SDK中可以找到这些必须的文件,但是XP的Platform SDK有200多兆,下载那么多的文件就为了一个lib和一些头文件,真的太浪费了。这里有一个我找了大半天才找到的链接,从这里可以找到所需要的文件:http://www.codersource.net/mfc_gdi_plus_common_issues.html。有了这些必需的文件之后,就可以开始着手写代码了。在Win32开发环境下使用GDI+,必须在创建任何GDI+对象之前调用GdiplusStartup函数进行初始化,而在销毁了所有程序中创建的GDI+对象之后,还需要调用GdiplusShutdown函数去告诉系统去做一些收尾的工作,如内存的释放等等。在GDI+的对象体系中,Graphics类是最重要的一个类,GDI+的三个部分都离不开它,不管是画各种的矢量图形(DrawLine等方法),图片显示(DrawImage等方法),还是文本的显示(DrawString方法)。下面就是一个引自MSDN的例子——在窗口中画一条线的程序,你可以发现在画一条线的时候,需要的对象有三个:一个是Graphics,一个是Pen,而另一个则是Color:

None.gif#define UNICODE
None.gif#include 
<windows.h>
None.gif#include 
<gdiplus.h>
None.gif
using namespace  Gdiplus;
None.gif
None.gifVOID OnPaint(HDC hdc)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   Graphics graphics(hdc);
InBlock.gif   Pen      pen(Color(
25500255));
InBlock.gif   graphics.DrawLine(
&pen, 00200100);
ExpandedBlockEnd.gif}

None.gif
None.gifLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
None.gif
None.gifINT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   HWND                hWnd;
InBlock.gif   MSG                 msg;
InBlock.gif   WNDCLASS            wndClass;
InBlock.gif   GdiplusStartupInput gdiplusStartupInput;
InBlock.gif   ULONG_PTR           gdiplusToken;
InBlock.gif   
InBlock.gif   
// Initialize GDI+.
InBlock.gif
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
InBlock.gif   
InBlock.gif   wndClass.style          
= CS_HREDRAW | CS_VREDRAW;
InBlock.gif   wndClass.lpfnWndProc    
= WndProc;
InBlock.gif   wndClass.cbClsExtra     
= 0;
InBlock.gif   wndClass.cbWndExtra     
= 0;
InBlock.gif   wndClass.hInstance      
= hInstance;
InBlock.gif   wndClass.hIcon          
= LoadIcon(NULL, IDI_APPLICATION);
InBlock.gif   wndClass.hCursor        
= LoadCursor(NULL, IDC_ARROW);
InBlock.gif   wndClass.hbrBackground  
= (HBRUSH)GetStockObject(WHITE_BRUSH);
InBlock.gif   wndClass.lpszMenuName   
= NULL;
InBlock.gif   wndClass.lpszClassName  
= TEXT("GettingStarted");
InBlock.gif   
InBlock.gif   RegisterClass(
&wndClass);
InBlock.gif   
InBlock.gif   hWnd 
= CreateWindow(
InBlock.gif      TEXT(
"GettingStarted"),   // window class name
InBlock.gif
      TEXT("Getting Started"),  // window caption
InBlock.gif
      WS_OVERLAPPEDWINDOW,      // window style
InBlock.gif
      CW_USEDEFAULT,            // initial x position
InBlock.gif
      CW_USEDEFAULT,            // initial y position
InBlock.gif
      CW_USEDEFAULT,            // initial x size
InBlock.gif
      CW_USEDEFAULT,            // initial y size
InBlock.gif
      NULL,                     // parent window handle
InBlock.gif
      NULL,                     // window menu handle
InBlock.gif
      hInstance,                // program instance handle
InBlock.gif
      NULL);                    // creation parameters
InBlock.gif
      
InBlock.gif   ShowWindow(hWnd, iCmdShow);
InBlock.gif   UpdateWindow(hWnd);
InBlock.gif   
InBlock.gif   
while(GetMessage(&msg, NULL, 00))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      TranslateMessage(
&msg);
InBlock.gif      DispatchMessage(
&msg);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   GdiplusShutdown(gdiplusToken);
InBlock.gif   
return msg.wParam;
ExpandedBlockEnd.gif}
  // WinMain
None.gif

None.gifLRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
None.gif   WPARAM wParam, LPARAM lParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   HDC          hdc;
InBlock.gif   PAINTSTRUCT  ps;
InBlock.gif   
InBlock.gif   
switch(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif   
case WM_PAINT:
InBlock.gif      hdc 
= BeginPaint(hWnd, &ps);
InBlock.gif      OnPaint(hdc);
InBlock.gif      EndPaint(hWnd, 
&ps);
InBlock.gif      
return 0;
InBlock.gif   
case WM_DESTROY:
InBlock.gif      PostQuitMessage(
0);
InBlock.gif      
return 0;
InBlock.gif   
default:
InBlock.gif      
return DefWindowProc(hWnd, message, wParam, lParam);
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}
 //  WndProc
None.gif

        编译并运行,如果一切正常的话,你将看到一条蓝线。至此,第一个能够运行的GDI+程序就这样完成了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值