您的位置:首页 > 其它

WIN32 GDI+ 学习笔记(二):使用画笔

2015-01-06 19:20 507 查看
首先把win32程序的模板贴上来:#include <windows.h>#include <gdiplus.h>using namespace Gdiplus;VOID OnPaint(HDC hdc){//绘图代码}LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow){HWND hWnd;MSG msg;WNDCLASS wndClass;GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;//初始化GDI+.GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);wndClass.style = CS_HREDRAW | CS_VREDRAW;wndClass.lpfnWndProc = WndProc;wndClass.cbClsExtra = 0;wndClass.cbWndExtra = 0;wndClass.hInstance = hInstance;wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wndClass.lpszMenuName = NULL;wndClass.lpszClassName = TEXT("GettingStarted");RegisterClass(&wndClass);hWnd = CreateWindow(TEXT("GettingStarted"), // window class nameTEXT("Getting Started"), // window captionWS_OVERLAPPEDWINDOW, // window styleCW_USEDEFAULT, // initial x positionCW_USEDEFAULT, // initial y positionCW_USEDEFAULT, // initial x sizeCW_USEDEFAULT, // initial y sizeNULL, // parent window handleNULL, // window menu handlehInstance, // program instance handleNULL); // creation parametersShowWindow(hWnd, iCmdShow);UpdateWindow(hWnd);while(GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}GdiplusShutdown(gdiplusToken);return msg.wParam;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam){PAINTSTRUCT ps;switch(message){case WM_PAINT:BeginPaint(hWnd, &ps);OnPaint(ps.hdc);EndPaint(hWnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;default:return DefWindowProc(hWnd, message, wParam, lParam);}}我们以后大部分绘图代码都在函数OnPaint中实现。首先来认识一下gdi+中的画笔对象Pen(参照MSDN):画笔对象是用来画直线和曲线的。1.构造画笔对象Pen(Color&,REAL);//用颜色对象和一个表示宽度的实数来构造。Pen(Brush*,REAL);//用一个画刷和一个表示宽度的实数来构造。我们暂时只讨论第一种构造方法。例子:Pen p(Color(255,0,0),1);graphics.DrawLine(&p,0,0,100,100);//从(0,0)到(100,100)画一条线2.画笔的对象方法画笔对象的方法有很多,我们先来了解一些比较常用的方法:SetColor(Color& color);//改变画笔的颜色SetWidth(REAL width);//改变画笔宽度SetAlignment(PenAlignment penAlignment);//设置画笔对齐方式PenAlignment是一个枚举类型,包括PenAlignmentCenter   = 0,//居中对齐PenAlignmentInset    = 1 //内对齐两个值,它们的区别如下图:
SetLineCap(LineCap startCap, LineCap endCap, DashCap dashCap);
用这个方法我们可以一次性设置线段的头尾样式和虚线样式,当然我们也可以用SetStartCap,SetEndCap,SetDashCap来分别设置。
LineCap也是一个枚举类型,包括:
LineCapFlat            = 0LineCapSquare          = 1LineCapRound           = 2LineCapTriangle        = 3LineCapNoAnchor        = 0x10LineCapSquareAnchor    = 0x11LineCapRoundAnchor     = 0x12LineCapDiamondAnchor   = 0x13LineCapArrowAnchor     = 0x14LineCapCustom          = 0xff //自定义样式
如下图:
SetLineJoin(LineJoin lineJoin);
用这个方法我们可以设置两条线段连接处的形状。
LineJoin是枚举类型,包括:
LineJoinMiter          = 0LineJoinBevel          = 1LineJoinRound          = 2LineJoinMiterClipped   = 3 
如下图:
SetDashStyle(DashStyle dashStyle);
SetDashPattern(REAL *dashArray, INT count);
这两个方法可以让我们自定义线段的样式。
DashStyle是枚举类型,包括:
DashStyleSolid        = 0DashStyleDash         = 1DashStyleDot          = 2DashStyleDashDot      = 3DashStyleDashDotDot   = 4DashStyleCustom       = 5//自定义
如下图:
如果上面的几种样式还不能满足你的需求,我们可以考虑自定义。
REAL *dashArray是一个实数数组,里面按照线,间隔,线,间隔,线,间隔……的顺序定义长度,n代表n个笔宽长,注意数组元素为偶数个,
INT count是数组元素的个数。
效果如图:
关于画笔的用法还有一些,在这里我就不一一列出来了。
最后附上一个小程序:
<pre name="code" class="cpp">#include <windows.h>#include <gdiplus.h>using namespace Gdiplus;#define maxl 5000int flag;int cx,cy;int lx1[maxl],ly1[maxl],lx2[maxl],ly2[maxl];int cnt;void drawline(Graphics *g){int i;Pen p(Color(255,0,0),1);p.SetLineCap(LineCapSquare,LineCapSquare,DashCapFlat);//p.SetDashStyle(DashStyleDot);for(i=0;i<cnt;i++)g->DrawLine(&p,lx1[i],ly1[i],lx2[i],ly2[i]);}void addline(int x1,int y1,int x2,int y2){if(cnt>=maxl)return;lx1[cnt]=x1;lx2[cnt]=x2;ly1[cnt]=y1;ly2[cnt]=y2;cnt++;}VOID OnPaint(HDC hdc){Graphics g(hdc);drawline(&g);}VOID OnLBtnDown(HWND hwnd,int x,int y){flag=1;SetCapture(hwnd);cx=x;cy=y;}VOID OnLBtnUp(HWND hwnd,int x,int y){flag=0;ReleaseCapture();}VOID OnMouseMove(HWND hwnd,int x,int y,int status){if(flag){addline(cx,cy,x,y);cx=x;cy=y;InvalidateRect(hwnd,NULL,FALSE);}}LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow){HWND                hWnd;MSG                 msg;WNDCLASS            wndClass;GdiplusStartupInput gdiplusStartupInput;ULONG_PTR           gdiplusToken;//初始化GDI+.GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);wndClass.style          = CS_HREDRAW | CS_VREDRAW;wndClass.lpfnWndProc    = WndProc;wndClass.cbClsExtra     = 0;wndClass.cbWndExtra     = 0;wndClass.hInstance      = hInstance;wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);wndClass.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);wndClass.lpszMenuName   = NULL;wndClass.lpszClassName  = TEXT("GettingStarted");RegisterClass(&wndClass);hWnd = CreateWindow(TEXT("GettingStarted"),   // window class nameTEXT("Getting Started"),  // window captionWS_OVERLAPPEDWINDOW,      // window styleCW_USEDEFAULT,            // initial x positionCW_USEDEFAULT,            // initial y positionCW_USEDEFAULT,            // initial x sizeCW_USEDEFAULT,            // initial y sizeNULL,                     // parent window handleNULL,                     // window menu handlehInstance,                // program instance handleNULL);                    // creation parametersShowWindow(hWnd, iCmdShow);UpdateWindow(hWnd);while(GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}GdiplusShutdown(gdiplusToken);return msg.wParam;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam){PAINTSTRUCT  ps;switch(message){case WM_LBUTTONDOWN:OnLBtnDown(hWnd,(short)LOWORD(lParam),(short)HIWORD(lParam));return 0;case WM_LBUTTONUP:OnLBtnUp(hWnd,(short)LOWORD(lParam),(short)HIWORD(lParam));return 0;case WM_MOUSEMOVE:OnMouseMove(hWnd,(short)LOWORD(lParam),(short)HIWORD(lParam),wParam);return 0;case WM_PAINT:BeginPaint(hWnd, &ps);OnPaint(ps.hdc);EndPaint(hWnd, &ps);return 0;case WM_DESTROY:PostQuitMessage(0);return 0;default:return DefWindowProc(hWnd, message, wParam, lParam);}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: