win32自绘按钮,使用GDI+(三)

解决前面的问题。实现鼠标移动进入到按钮的特效。

效果是这样的

鼠标移到按钮上,改变按钮的颜色(这里用的是直接换贴在按钮上的图片)

程序运行

鼠标进入按钮

代码

  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_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+和自绘按钮就学到这。以后写程序的时候,这个就是起点了,需要实现其他的效果,再研究

posted @ 2015-12-14 13:08  廖凡  阅读(2648)  评论(0编辑  收藏  举报