CreateWindowExW函数该怎么写,win32自绘按键

转发请注解出处

win32自绘开关,使用GDI+(三),win32gdi

消除日前的难题。达成鼠标移动进入到开关的特效。

作用是如此的

鼠标移到按键上,退换按键的水彩(这里用的是一贯换贴在按键上的图片)

程序运转

1 #ifndef ULONG_PTR 2 //#define ULONG_PTR unsigned long* 3 #endif
4 #include <windows.h> 5 #define _WIN32_WINNT 0x0500 6
#include<Winuser.h > 7 //#include <objidl.h> 8
#include<tchar.h> 9 #include<stdlib.h> 10 #include
<gdiplus.h> 11 #include”resource.h” 12 using namespace Gdiplus;
13 #pragma comment (lib,”Gdiplus.lib”) 14 #define ID_BUTTON1 1 15
#define ID_CreateWindowExW函数该怎么写,win32自绘按键。BUTTON2 2 16 #define ID_MAINPAGE 3 17 // 18 #define
IDM_MAINPAGE 10 19 #define IDM_REFRESH 11 20 21 HINSTANCE hInst; 22
HMENU hMenu1; 23 WNDPROC oldBtnProc; 24 BOOL btnEntry=FALSE; 25 void
DrawRegn(HDC hdc) 26 { 27 28 } 29 30 31 void DrawIco(HDC hdc) 32 { 33
Graphics graphics(hdc); 34 Pen pen1(Color(255,248,0,0),2); 35 Pen
pen2(Color(255,0,255,0),2); 36 Pen pen3(Color(255,43,102,149),2); 37
graphics.DrawEllipse(&pen1,3,3,14,14); 38
graphics.DrawEllipse(&pen2,10,10,14,14); 39
graphics.DrawEllipse(&pen3,13,3,14,14);//calc 40 //text 41 HFONT
hOldFont; 42 HFONT hFont =
CreateFont(20,9,0,0,0,0,0,0,OEM_CHARSET,OUT_DEFAULT_PRECIS, 43
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH
|FF_SCRIPT,”script” ); 44 hOldFont = (HFONT)SelectObject(hdc,hFont); 45
SetTextColor(hdc,RGB(200,200,50));// 46 SetBkMode(hdc,TRANSPARENT); 47
TextOut(hdc,30,3,”www.cnblogs.com”,sizeof(“www.cnblogs.com”)-1);//calc
48 SelectObject(hdc,hOldFont); 49 DeleteObject(hFont); 50 } 51 void
LoadBkImge(HDC hdc) 52 { 53 Graphics graphics(hdc); 54 Image
image(L”pic1.png”); 55 graphics.DrawImage(&image,0,0); 56 } 57 58 59
void FillRec(HDC hdc,Rect myRect,Color* colors,float *positions,int i)
60 { 61 Graphics graphics(hdc); 62 //多彩渐变色 63 LinearGradientBrush
myLinearGradientBrush( 64 myRect, 65 Color(255,255,255,0), 66
Color(255,255,0,0), 67 LinearGradientModeVertical 68
);//LinearGradientModeHorizontal*/ 69 70
myLinearGradientBrush.SetInterpolationColors(colors, positions, i); 71
graphics.FillRectangle(&myLinearGradientBrush,myRect); 72 } 73 74
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 75 LRESULT
CALLBACK BtnProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam); 76 77
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
78 { 79 HWND hWnd; 80 MSG msg; 81 WNDCLASS wndClass; 82
GdiplusStartupInput gdiplusStartupInput; 83 ULONG_PTR gdiplusToken; 84
hInst=hInstance; 85 86 // Initialize GDI+. 87
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 88 89
wndClass.style = CS_HREDRAW | CS_VREDRAW|DS_CENTER; 90
wndClass.lpfnWndProc = WndProc; 91 wndClass.cbClsExtra = 0; 92
wndClass.cbWndExtra = 0; 93 wndClass.hInstance = hInstance; 94
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 95 wndClass.hCursor =
LoadCursor(NULL, IDC_ARROW); 96 wndClass.hbrBackground =
(HBRUSH)GetStockObject(WHITE_BRUSH); 97 wndClass.lpszMenuName = NULL;
98 wndClass.lpszClassName = TEXT(“Gdiplustest”); 99 100
RegisterClass(&wndClass); 101 hWnd = CreateWindow( 102
TEXT(“Gdiplustest”), 103 TEXT(“Gdiplustest”), 104
WS_OVERLAPPED|WS_POPUP, 105 CW_USEDEFAULT, 106 CW_USEDEFAULT, 107
400, 108 250, 109 NULL, 110 NULL, 111 hInstance, 112 NULL); 113 114
ShowWindow(hWnd, iCmdShow); 115 UpdateWindow(hWnd); 116 117
while(GetMessage(&msg, NULL, 0, 0)) 118 { 119 TranslateMessage(&msg);
120 DispatchMessage(&msg); 121 } 122 123 GdiplusShutdown(gdiplusToken);
124 return msg.wParam; 125 } 126 127 LRESULT CALLBACK WndProc(HWND hWnd,
UINT message, 128 WPARAM wParam, LPARAM lParam) 129 { 130 static HDC
hdc; 131 static HWND hButton1,hButton2,hMainPage; 132 PAINTSTRUCT ps;
133 LPDRAWITEMSTRUCT pdis; 134 135 switch(message) 136 { 137 case
WM_CREATE: 138 { 139 hdc=GetDC(hWnd); 140 //窗口居中显得 141 int
scrWidth,scrHeight; 142 RECT rect; 143
scrWidth=GetSystemMetrics(SM_CXSCREEN); 144
scrHeight=GetSystemMetrics(SM_CYSCREEN); 145 GetWindowRect(hWnd,&rect);
146 rect.left=(scrWidth-rect.right)/2; 147
rect.top=(scrHeight-rect.bottom)/2; 148
SetWindowPos(hWnd,HWND_TOP,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW);
149
hButton1=CreateWindow(“button”,””,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,355,5,15,15,hWnd,(HMENU)ID_BUTTON1,hInst,NULL);
150
hButton2=CreateWindow(“button”,””,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,380,5,15,15,hWnd,(HMENU)ID_BUTTON2,hInst,NULL);
151 // 152
hMainPage=CreateWindow(“button”,””,WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,10,32,20,20,hWnd,(HMENU)ID_MAINPAGE,hInst,NULL);
153
oldBtnProc=(WNDPROC)SetWindowLong(hMainPage,GWL_WNDPROC,(long)BtnProc);
154 // 159 return 0; 160 } 161 case WM_SIZE: 162 return 0; 163 case
WM_PAINT: 164 { 165 hdc = BeginPaint(hWnd, &ps); 166 167 Graphics
graphics(hdc); 168 //LoadBkImge(hdc); 169 //标题栏 170 Rect
myRect(0,0,400,25); 171 Color colors[]={ 172 Color(255,255,255), 173
Color(247,251,255), 174 Color(239,239,247), 175 Color(222,227,231), 176
Color(231,235,239), 177 Color(214,219,222) 178 }; 179 float
positions[]={ 180 0.0f, 181 0.2f, 182 0.4f, 183 0.6f, 184 0.8f, 185
1.0f 186 }; 187 FillRec(hdc,myRect,colors,positions,6); 188 //菜单栏 189
Rect myRect1(0,26,400,30); 190 Color colors1[]={ 191
Color(251,231,252), 192 Color(199,222,251), 193 Color(181,211,251), 194
Color(162,201,250), 195 Color(139,188,250), 196 Color(120,178,250) 197
}; 198 float positions1[]={ 199 0.0f, 200 0.2f, 201 0.4f, 202 0.6f,
203 0.8f, 204 1.0f 205 }; 206 FillRec(hdc,myRect1,colors1,positions1,6);
207 /*SolidBrush brush11(Color(255,240,240,240)); 208
graphics.FillRectangle(&brush11,myRect1);*/ 209 210 Pen
pen1(Color(255,255,255,255)); 211 Pen pen2(Color(255,160,160,160)); 212
Pen pen3(Color(255,181,178,181)); 213 Pen pen4(Color(255,255,0,0)); 214
{ 215 graphics.DrawLine(&pen1,0,0,400,0); 216
graphics.DrawLine(&pen2,0,1,400,1); 217
graphics.DrawLine(&pen4,0,25,400,25); 218 } 219 { 220
graphics.DrawLine(&pen1,0,250,400,250); 221
graphics.DrawLine(&pen2,0,249,400,249); 222
graphics.DrawLine(&pen1,0,248,400,248); 223 } 224 { 225
graphics.DrawLine(&pen1,0,0,0,250); 226
graphics.DrawLine(&pen2,1,0,1,250); 227 } 228 { 229
graphics.DrawLine(&pen1,400,0,400,250); 230
graphics.DrawLine(&pen2,399,0,399,250); 231
graphics.DrawLine(&pen1,398,2,398,250); 232 } 233 //菜单栏 234
graphics.DrawLine(&pen1,0,56,400,56); 235
graphics.DrawLine(&pen3,0,57,400,57); 236 237 DrawIco(hdc); 238
EndPaint(hWnd, &ps); 239 return 0; 240 } 241 case WM_CTLCOLORBTN: 242 {
243 HBRUSH hBrush; 244 //hBrush = CreateSolidBrush(RGB(255, 0, 0)); 245
hBrush=(HBRUSH)GetStockObject(NULL_BRUSH); 246 return (long)hBrush; 247
} 248 249 case WM_COMMAND: 250 { 251 switch(wParam) 252 { 253 case
ID_BUTTON1: 254 { 255 ShowWindow(hWnd,SW_SHOWMINIMIZED); 256 break;
257 } 258 case ID_BUTTON2: 259 { 260 PostQuitMessage(0); 261 break; 262
} 263 case ID_MAINPAGE: 264 { 265 POINT pt; 266 pt.x=10; 267 pt.y=52;
274 break; 275 } 276 // 277 } 278 279 return 0; 280 281 } 282 283 case
WM_DRAWITEM: 284 { 285 286 pdis=(LPDRAWITEMSTRUCT)lParam; 287
//SetBkMode(pdis->hDC, TRANSPARENT ); 288
//FillRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(NULL));
289
//FrameRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(BLACK_BRUSH));
290 291 int cx=pdis->rcItem.right-pdis->rcItem.bottom; 292 int
cy=pdis->rcItem.bottom-pdis->rcItem.top; 293 294 295
switch(pdis->CtlID) 296 { 297 298 case ID_BUTTON1: 299 { 300
Graphics graphics(pdis->hDC); 301 Rect rectt(0,0,40,30); 302 Color
colors[]={ 303 Color(247,251,255), 304 Color(239,239,247), 305
Color(222,227,231), 306 Color(231,235,239), 307 }; 308 float
positions[]={ 309 0.0f, 310 0.333333f, 311 0.666666f, 312 1.0f 313 };
314 //FillRec(pdis->hDC,rectt,colors,positions,4); 315 { 316
SolidBrush m_pBrush(Color(255,0,0,0)); 317 PointF point1(3.0f, 6.5f);
318 PointF point2(12.0f, 6.5f); 319 PointF point3(12.0f, 8.50f); 320
PointF point4(3.0f, 8.50f); 321 PointF points[4] = {point1, point2,
point3,point4}; 322 graphics.FillPolygon(&m_pBrush, points, 4,
FillModeAlternate); 323 } 324 325 break; 326 } 327 328 case ID_BUTTON2:
329 { 330 Graphics graphics(pdis->hDC); 331 Rect rectt(0,0,40,30);
332 Color colors[]={ 333 Color(247,251,255), 334 Color(239,239,247),
335 Color(222,227,231), 336 Color(231,235,239), 337 }; 338 float
positions[]={ 339 0.0f, 340 0.33333f, 341 0.66633f, 342 1.0f 343 };
344 //FillRec(pdis->hDC,rectt,colors,positions,4); 345 346 SolidBrush
m_pBrush(Color(255,0,0,0)); 347 // 348 PointF point1(2.0f, 4.0f); 349
PointF point2(4.0f, 2.0f); 350 PointF point3(13.0f, 11.0f); 351 PointF
point4(11.0f, 13.0f); 352 PointF points[4] = {point1, point2,
point3,point4}; 353 graphics.FillPolygon(&m_pBrush, points, 4,
FillModeAlternate); 354 // 355 PointF point5(13.0f, 4.0f); 356 PointF
point6(11.0f, 2.0f); 357 PointF point7(2.0f, 11.0f); 358 PointF
point8(4.0f, 13.0f); 359 360 PointF points2[4] = {point5, point6,
point7,point8 }; 361 graphics.FillPolygon(&m_pBrush, points2, 4,
FillModeAlternate); 362 break; 363 } 364 case ID_MAINPAGE: 365 { 387
if(btnEntry==TRUE) 388 { 389 //bitmap2.png 390 Graphics graphics(
pdis->hDC); 391 Image image(L”bitmap2.png”, FALSE); 392
graphics.DrawImage(&image, 0,0); 393 } 394 else 395 { 396 Graphics
graphics( pdis->hDC); 397 Image image(L”bitmap.png”, FALSE); 398
graphics.DrawImage(&image, 0,0); 399 400 } 401 402
//TransparentBitmap(pdis->hDC,hBitmap1,-5,-5,20,20,RGB(255,255,255));
403 //ReleaseDC( 404 break; 405 } 406 407 } 408 // 409 410 411 if
(pdis->itemState & ODS_SELECTED) 412 { 413 Graphics graphics(hdc);
414 SolidBrush brush2(Color(255,255,0,0)); 415
//graphics.FillPolygon(&brush2,pointt,7,FillModeAlternate); 416 417
//InvertRect (pdis->hDC, &pdis->rcItem) ; 418 } 419 420 // Draw a
focus rectangle if the button has the focus 421 422 if
(pdis->itemState & ODS_FOCUS) 423 { 424 pdis->rcItem.left += cx /
16 ; 425 pdis->rcItem.top += cy / 16 ; 426 pdis->rcItem.right -=
cx / 16 ; 427 pdis->rcItem.bottom -= cy / 16 ; 428 429 DrawFocusRect
(pdis->hDC, &pdis->rcItem) ; 430 } 431 return 0 ; 432 } 433 434
case WM_DESTROY: 435 PostQuitMessage(0); 436 return 0; 437 case
WM_LBUTTONDOWN: 438 SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
439 return 0; 440 default: 441 return DefWindowProc(hWnd, message,
wParam, lParam); 442 } 443 } 444 445 LRESULT CALLBACK BtnProc(HWND
hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) 446 { 447 switch(uMsg) 448 {
449 case WM_MOUSEMOVE: 450 { 451 if(btnEntry == FALSE) 452 { 453
TRACKMOUSEEVENT tme; 454 tme.cbSize = sizeof(TRACKMOUSEEVENT); 455
tme.dwFlags = TME_HOVER | TME_LEAVE; 456 tme.dwHoverTime = 1; 457
tme.hwndTrack = hwnd; 458 TrackMouseEvent(&tme); 459 } 460 break; 461 }
462 case WM_MOUSEHOVER: 463 { 464 btnEntry = TRUE; 465
InvalidateRect(hwnd,NULL,TRUE); 466 UpdateWindow(hwnd); 467 break; 468 }
469 case WM_MOUSELEAVE: 470 { 471 btnEntry = FALSE; 472
InvalidateRect(hwnd,NULL,TRUE); 473 UpdateWindow(hwnd); 474 break; 475 }
476 default:return CallWindowProc(oldBtnProc,hwnd,uMsg,wParam,lParam);
477 } 478 } 479

程序的短处比较多的,但起码实现了职能,未来私底下渐渐完善呢。再装进一下。何况,关于GDI+和自绘按键就学到那。现在写程序的时候,这么些就是起源了,须要贯彻任何的功效,再探讨

消除日前的标题。完结鼠标移动步入到开关的特效。 效果是那般的
鼠标移到按键上,改换按键的颜…

Windows程序设计(Charles Petzold)HELLOWIN程序完结,petzoldhellowin

www.5929.com 1 1
/*————————————————————– 2
HELLOWIN.C–DisPlays “Hello, Windows!” in client area 3 (c) Charles
Petzold, 1998 4
————————————————————–*/ 5
#include <windows.h> 6 LRESULT CALLBACK WndProc(HWND, UINT,
WPARAM, LPARAM); 7 8 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPreVInstance, 9 PSTR szCmdLine, int iCmdShow) 10 { 11 static TCHAR
szAppName[] = TEXT(“HelloWin”); 12 HWND hwnd; 13 MSG msg; 14 WNDCLASS
wndclass; 15 16 wndclass.style = CS_HREDRAW | CS_VREDRAW; 17
wndclass.lpfnWndProc = WndProc; 18 wndclass.cbClsExtra = 0; 19
wndclass.cbWndExtra = 0; 20 wndclass.hInstance = hInstance; 21
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 22 wndclass.hCursor =
LoadCursor(NULL, IDC_ARROW); 23 wndclass.hbrBackground = (HBRUSH)
GetStockObject(WHITE_BRUSH); 24 wndclass.lpszMenuName = NULL; 25
wndclass.lpszClassName = szAppName; 26 27 if (!RegisterClass(&wndclass))
28 { 29 MessageBox(NULL, TEXT(“This progam requires Windows NT!”), 30
szAppName, MB_ICONERROR); 31 return 0; 32 } 33 34 hwnd =
CreateWindow(szAppName, //window class name 35 TEXT(“The Hello
Program”), //window caption 36 WS_OVERLAPPEDWINDOW, //window style 37
CW_CreateWindowExW函数该怎么写,win32自绘按键。USEDEFAULT, //initial x position 38 CW_USEDEFAULT, //initial y
position 39 CW_USEDEFAULT, //initial x size 40 CW_USEDEFAULT,
//initial y size 41 NULL, //parent window handle 42 NULL, //window menu
handle 43 hInstance, //program instance 44 NULL //creation parameters 45
); 46 ShowWindow(hwnd, iCmdShow); 47 UpdateWindow(hwnd); 48 49 while
(GetMessage(&msg, NULL, 0, 0)) 50 { 51 TranslateMessage(&msg); 52
DispatchMessage(&msg); 53 } 54 return msg.wParam; 55 } 56 57 LRESULT
CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
58 { 59 HDC hdc; 60 PAINTSTRUCT ps; 61 RECT rect; 62 63 switch (message)
64 { 65 case WM_CREATE: 66 //PlaySound(TEXT(“hellowin.wav”), NULL,
SND_FILENAME | SND_ASYNC); 67 return 0; 68 69 case WM_PAINT: 70 hdc =
BeginPaint(hwnd, &ps); 71 GetClientRect(hwnd, &rect); 72 DrawText(hdc,
TEXT(“Hello, windows!”), -1, &rect, 73 DT_SINGLELINE | DT_CENTER|
DT_VCENTER); 74 EndPaint(hwnd, &ps); 75 return 0; 76 77 case
WM_DESTROY: 78 PostQuitMessage(0); 79 return 0; 80 } 81 return
DefWindowProc(hwnd, message, wParam, lParam); HELLOWIN.c

vs新建一空的win32应用程序,将曾经编写制定好的代码文件增多至源文件中。在工程属性->附加依赖项中丰盛”winmm.lib”(或将PlaySound()函数注释掉),编译运转,查看结果。

Petzold)HELLOWIN程序达成,petzoldhellowin 1 /*
————————————————————– 2
HELLOWIN.C–DisPlays “Hello, Wi…

大神,CreateWindowExW函数该怎么写?
#include
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)
{
WNDCLASS wndclass;
HWND hWnd;
www.5929.com,MSG msg;
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=GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=TEXT(“window窗口创建”);
if(!RegisterClass(&wndclass))
return FALSE;
hWnd=CreateWindowExW(TEXT(“window窗口创造”),TEXT(“window窗口创制”),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage;
DispatchMessage;
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM
lParam)
{
switch
{
case WM_PAINT:HDC hdc;
PAINTSTRUCT ps;
hdc=BeginPaint(hWnd,&ps);
TextOutW(hdc,10,20,TEXT(“哈哈,windows编制程序创立的窗口!”),28);
EndPaint(hWnd,&ps);
return 0;
case WM_DESTROY:PostQuitMessage;
return 0;
}
return DefWindowProcW(hWnd,message,wParam,lParam);
}

作者:小马

能告诉自个儿CreateWindowExW函数该怎么写吧?
HWND CreateWindowExW(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR
lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight, HWND
hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)

 

一 介绍
实际上自身对GDI+没办法算是大方,只是在多少个小品种中央银行使了部分而已,
算是入门了. 刚好近日有一点时光, 把温馨支配的东东做个总结.

 

假诺你在此以前用过GDI, 那么恭喜您,因为你转到GDI+举手之劳.
假设你没用过GDI,那么也恭喜您,
因为您能够直接学GDI+,不用经历学习GDI的这种优伤.
因为GDI+相对于GDI,越发空虚,尤其上层, 遮掩了越多底层的事物,
全部架构尤其清晰明了.

 

GDI+通过c++ class的款型对外引出接口,
程序猿直接调用这一个接口来造成相应的成效. 共有38个相关的类,
四二十个枚举常量和6个组织体. 它的局限性是不得不运转在windows XP和windows
server2000的系统上.

 

简短说多少个GDI+与GDI分裂的地点.
1
GDI+很关键叁个更进一竿就是增了对八种图像格式的辅助.它最近帮助上边几中格式的图像:
BMP
Graphics Interchange Format (GIF)
JPEG
 Exif
 PNG
TIFF
ICON
WMF
EMF

2 DC(设备条件内容)的定义变得模糊,基本对程序猿是透明了.
用GDI的人都清楚, 不懂DC可那些, 那几个DC保存着呈现设备的天性和意义消息,
每当你想要drawing时, 你得先获得DC的句柄,
然后把那一个句柄作为参数字传送给GDI的API,
别的还要思量关于”选到设备(selectObjec),释放设备”等麻烦的操作.
GDI+对下边包车型客车拍卖简单明了比非常多,它有多少个Graphics类,叁个Graphics的对象似乎于DC的角色,
并且它跟pen,brush等的涉及也变得独立, 不用再”选来选去”,
在GDI+里,像pen,brush那些事物都以独立的对象,用的时候只需把它们的实例作为参数字传送给Graphics的接口就可以了(在上面包车型客车示范中会看到).

3 扩充了对渐变颜色效果的帮助.
原先在GDI里做渐变效果, 那叫一眼花缭乱呀.
下边包车型客车示范中会具体说说GDI+里的渐变.

二 应用

最早筹划

行使GDI+, 首先要包涵相关的头文件, 唯有多少个,如下:
#include <gdiplus.h>
gdiplus.h这一个文件vs二〇〇七的include目录下, 所以在vs二〇〇五下,
你能够平昔以”<>”的款型包罗.

还供给利用一个lib, 正是Gdiplus.lib, 你能够在源文件中通过
#pragma comment (lib,”Gdiplus.lib”)
的样式链接进来, 也足以一向在”项目性质-linker-input里加多, 效果是一样的.

为了不使GDI+专项使用的相干类,常量,结构体与另外应用混淆,GDI+有本身的命名空间,
叫Gdiplus, 当你要用到GDI+的连带API时,就要动用下边包车型客车话语:
using namespace Gdiplus;

最终, GDI+有个使用开关, GdiplusStartup和GdiplusShutdown, 一个开,贰个关,
使用GDI+任何功用前,先调用GdiplusStartup,程序截止时,调用GdiplusShutdown关闭.
譬如在依赖mfc的对话框应用中, 我一般那样做, 要是本人的工程为MyGDIPlusTest,
首先在MyGDIPlusTest.cpp的InitInstance函数中开垦GDI+,如下:
…..
CWinApp::InitInstance();
GdiplusStartup(&m_gdiPlusToken, &m_gdiPlusStartupInput, NULL);
…..
下一场重载ExitInstance函数, 在那个函数里关闭GDI+
GdiplusShutdown(m_gdiPlusToken);//程序退出时关闭
return CWinApp::ExitInstance();

多个函数的参数,本人去查吧 ,不说了.

 

示例
1 先看一个msdn中的示例

view plaincopy to clipboardprint?#include <stdafx.h>  
#include <windows.h>  
#include <objidl.h>  
#include <gdiplus.h>  
using namespace Gdiplus; 
#pragma comment (lib,”Gdiplus.lib”)  
 
VOID OnPaint(HDC hdc) 

   Graphics graphics(hdc); 
   Pen      pen(Color(255, 0, 0, 255)); 
   graphics.DrawLine(&pen, 0, 0, 200, 100); 

 
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; 
    
   // Initialize 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)GetStockObject(WHITE_BRUSH); 
   wndClass.lpszMenuName   = NULL; 
   wndClass.lpszClassName  = TEXT(“GettingStarted”); 
    
   RegisterClass(&wndClass); 
    
   hWnd = CreateWindow( 
      TEXT(“GettingStarted”),   // window class name  
      TEXT(“Getting Started”),  // window caption  
      WS_OVERLAPPEDWINDOW,      // window style  
      CW_USEDEFAULT,            // initial x position  
      CW_USEDEFAULT,            // initial y position  
      CW_USEDEFAULT,            // initial x size  
      CW_USEDEFAULT,            // initial y size  
      NULL,                     // parent window handle  
      NULL,                     // window menu handle  
      hInstance,                // program instance handle  
      NULL);                    // creation parameters  
    
   ShowWindow(hWnd, iCmdShow); 
   UpdateWindow(hWnd); 
    
   while(GetMessage(&msg, NULL, 0, 0)) 
   { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
   } 
    
   GdiplusShutdown(gdiplusToken); 
   return msg.wParam; 
}  // WinMain  
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,  
   WPARAM wParam, LPARAM lParam) 

   HDC          hdc; 
   PAINTSTRUCT  ps; 
    
   switch(message) 
   { 
   case WM_PAINT: 
      hdc = BeginPaint(hWnd, &ps); 
      OnPaint(hdc); 
      EndPaint(hWnd, &ps); 
      return 0; 
   case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 
   default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
   } 
} // WndProc 
#include <stdafx.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,”Gdiplus.lib”)

VOID OnPaint(HDC hdc)
{
   Graphics graphics(hdc);
   Pen      pen(Color(255, 0, 0, 255));
   graphics.DrawLine(&pen, 0, 0, 200, 100);
}

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;
  
   // Initialize 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)GetStockObject(WHITE_BRUSH);
   wndClass.lpszMenuName   = NULL;
   wndClass.lpszClassName  = TEXT(“GettingStarted”);
  
   RegisterClass(&wndClass);
  
   hWnd = CreateWindow(
      TEXT(“GettingStarted”),   // window class name
      TEXT(“Getting Started”),  // window caption
      WS_OVERLAPPEDWINDOW,      // window style
      CW_USEDEFAULT,            // initial x position
      CW_USEDEFAULT,            // initial y position
      CW_USEDEFAULT,            // initial x size
      CW_USEDEFAULT,            // initial y size
      NULL,                     // parent window handle
      NULL,                     // window menu handle
      hInstance,                // program instance handle
      NULL);                    // creation parameters
  
   ShowWindow(hWnd, iCmdShow);
   UpdateWindow(hWnd);
  
   while(GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
  
   GdiplusShutdown(gdiplusToken);
   return msg.wParam;
}  // WinMain

LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
   WPARAM wParam, LPARAM lParam)
{
   HDC          hdc;
   PAINTSTRUCT  ps;
  
   switch(message)
   {
   case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      OnPaint(hdc);
      EndPaint(hWnd, &ps);
      return 0;
   case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
   default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
} // WndProc

 

这么些示例是画线的,尽管是打字与印刷字符串, 只用修改OnPaint中的内容,如下:

view plaincopy to clipboardprint?VOID OnPaint(HDC hdc) 

   Graphics    graphics(hdc); 
   SolidBrush  brush(Color(255, 0, 0, 255)); 
   FontFamily  fontFamily(L”Times New Roman”); 
   Font        font(&fontFamily, 24, FontStyleRegular, UnitPixel); 
   PointF      pointF(10.0f, 20.0f); 
    
   graphics.DrawString(L”Hello World!”, -1, &font, pointF, &brush); 

VOID OnPaint(HDC hdc)
{
   Graphics    graphics(hdc);
   SolidBrush  brush(Color(255, 0, 0, 255));
   FontFamily  fontFamily(L”Times New Roman”);
   Font        font(&fontFamily, 24, FontStyleRegular, UnitPixel);
   PointF      pointF(10.0f, 20.0f);
  
   graphics.DrawString(L”Hello World!”, -1, &font, pointF, &brush);
}

 

2 自个儿写的多个人作品体现图片的简练示例
据说mfc的对话框应用,点一下按键,显示一张图片. 只展现主要代码

view plaincopy to clipboardprint?void
CGDI_Plus_TestDlg::OnBnClickedButtonPen() 

 // TODO: Add your control notification handler code here  
 HDC hdc = this->GetDC()->m_hDC; 
 Graphics graphics(hdc); 
 Image image(L”c:\\image\\ip.jpg”); 
 graphics.DrawImage(&image, 200, 200); 

void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
 // TODO: Add your control notification handler code here
 HDC hdc = this->GetDC()->m_hDC;
 Graphics graphics(hdc);
 Image image(L”c:\\image\\ip.jpg”);
 graphics.DrawImage(&image, 200, 200);
}

有以下几点要表达,
第一点, Image只帮助通过路线和流的样式提供实例伊始化,不扶助财富句柄,
假使要用财富句柄的方式,能够先把能源数量读到流里,这里不说了.
其次点, 关于DrawImage,有比比较多种载函数,
提供了大概你能体会领会的持有机能,作者这里用了上边这一个重载
Status DrawImage(      Image *image,INT x,INT y);
也正是在内定地方显得加载的图像.

 

3 渐变效果

这段时间说了,GDI+补助渐变, 分为三种, linear gradient和path gradient,
这里商讨第一种.
linear gradient又分为

Horizontal Linear Gradients,

Customizing Linear Gradients,

Diagonal Linear Gradients

二种. 那几个相比较好通晓. 上面的代码示例来自msdn,小编做了有个别修改.

view plaincopy to clipboardprint?void
CGDI_Plus_TestDlg::OnBnClickedButtonPen() 

 // TODO: Add your control notification handler code here  
 HDC hdc = this->GetDC()->m_hDC; 
 Graphics graphics(hdc); 
 LinearGradientBrush linearBrush(Point(0, 10), Point(200, 10),  
  Color(255,255,0,0),Color(255,0,0,255)); 
 
 Pen pen(&linearBrush, 4); 
 graphics.DrawLine(&pen, 100,100, 200, 100); 
 graphics.FillEllipse(&linearBrush, 100, 150, 200, 100); 
 graphics.FillRectangle(&linearBrush, 100, 255, 500, 30); 

void CGDI_Plus_TestDlg::OnBnClickedButtonPen()
{
 // TODO: Add your control notification handler code here
 HDC hdc = this->GetDC()->m_hDC;
 Graphics graphics(hdc);
 LinearGradientBrush linearBrush(Point(0, 10), Point(200, 10),
  Color(255,255,0,0),Color(255,0,0,255));

 Pen pen(&linearBrush, 4);
 graphics.DrawLine(&pen, 100,100, 200, 100);
 graphics.FillEllipse(&linearBrush, 100, 150, 200, 100);
 graphics.FillRectangle(&linearBrush, 100, 255, 500, 30);
}

 

 

程序运转的效果与利益如下图

 www.5929.com 2

附: GDI+与GDI混合编制程序
GDI+提供了一种机制, 可以和GDI混合使用,
主假诺行使Graphics中的ReleaseHDC和GetHDC, 上面包车型客车代码示例来自msdn:

view plaincopy to clipboardprint?VOID Example_GetReleaseHDC(Graphics*
g) 

   Pen pen(Color(255, 0, 0, 255)); 
   g->DrawEllipse(&pen, 10, 10, 100, 50);  // GDI+  
   HDC hdc = g->GetHDC(); 
      // Make GDI calls, but don’t call any methods  
      // on g until after the call to ReleaseHDC.  
      Rectangle(hdc, 120, 10, 220, 60);  // GDI    
   g->ReleaseHDC(hdc); 
   // Ok to call methods on g again.  
   g->DrawLine(&pen, 240, 10, 340, 60);   

作者:小马 一 介绍
其实本身对GDI+不可能算是大方,只是在多少个小项目中使用了有些罢了,
算是入门了. 刚好近些日子有个别时刻, 把…

Leave a Comment.