您的位置:首页 > 其它

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

2015-12-14 13:08 211 查看
解决前面的问题。实现鼠标移动进入到按钮的特效。

效果是这样的

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

程序运行



鼠标进入按钮



代码

#ifndef ULONG_PTR
//#define ULONG_PTR unsigned long*
#endif
#include <windows.h>
#define   _WIN32_WINNT   0x0500
#include<Winuser.h >
//#include <objidl.h>
#include<tchar.h>
#include<stdlib.h>
#include <gdiplus.h>
#include"resource.h"
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
#define ID_BUTTON1 1
#define ID_BUTTON2 2
#define ID_MAINPAGE 3
//
#define IDM_MAINPAGE 10
#define IDM_REFRESH 11

HINSTANCE hInst;
HMENU hMenu1;
WNDPROC oldBtnProc;
BOOL btnEntry=FALSE;
void DrawRegn(HDC hdc)
{

}

void DrawIco(HDC hdc)
{
Graphics graphics(hdc);
Pen pen1(Color(255,248,0,0),2);
Pen pen2(Color(255,0,255,0),2);
Pen pen3(Color(255,43,102,149),2);
graphics.DrawEllipse(&pen1,3,3,14,14);
graphics.DrawEllipse(&pen2,10,10,14,14);
graphics.DrawEllipse(&pen3,13,3,14,14);//calc
//text
HFONT hOldFont;
HFONT hFont = CreateFont(20,9,0,0,0,0,0,0,OEM_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH |FF_SCRIPT,"script" );
hOldFont = (HFONT)SelectObject(hdc,hFont);
SetTextColor(hdc,RGB(200,200,50));//
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,30,3,"www.cnblogs.com",sizeof("www.cnblogs.com")-1);//calc
SelectObject(hdc,hOldFont);
DeleteObject(hFont);
}
void LoadBkImge(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"pic1.png");
graphics.DrawImage(&image,0,0);
}

void FillRec(HDC hdc,Rect myRect,Color* colors,float *positions,int i)
{
Graphics graphics(hdc);
//多彩渐变色
LinearGradientBrush myLinearGradientBrush(
myRect,
Color(255,255,255,0),
Color(255,255,0,0),
LinearGradientModeVertical
);//LinearGradientModeHorizontal*/

myLinearGradientBrush.SetInterpolationColors(colors, positions, i);
graphics.FillRectangle(&myLinearGradientBrush,myRect);
}

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK BtnProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
HWND                hWnd;
MSG                 msg;
WNDCLASS            wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
hInst=hInstance;

// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

wndClass.style          = CS_HREDRAW | CS_VREDRAW|DS_CENTER;
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("Gdiplustest");

RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("Gdiplustest"),
TEXT("Gdiplustest"),
WS_OVERLAPPED|WS_POPUP,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
250,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(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)
{
static HDC   hdc;
static HWND hButton1,hButton2,hMainPage;
PAINTSTRUCT  ps;
LPDRAWITEMSTRUCT pdis;

switch(message)
{
case WM_CREATE:
{
hdc=GetDC(hWnd);
//窗口居中显示
int scrWidth,scrHeight;
RECT rect;
scrWidth=GetSystemMetrics(SM_CXSCREEN);
scrHeight=GetSystemMetrics(SM_CYSCREEN);
GetWindowRect(hWnd,&rect);
rect.left=(scrWidth-rect.right)/2;
rect.top=(scrHeight-rect.bottom)/2;
SetWindowPos(hWnd,HWND_TOP,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW);
hButton1=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,355,5,15,15,hWnd,(HMENU)ID_BUTTON1,hInst,NULL);
hButton2=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,380,5,15,15,hWnd,(HMENU)ID_BUTTON2,hInst,NULL);
//
hMainPage=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,10,32,20,20,hWnd,(HMENU)ID_MAINPAGE,hInst,NULL);
oldBtnProc=(WNDPROC)SetWindowLong(hMainPage,GWL_WNDPROC,(long)BtnProc);
//
return 0;
}
case WM_SIZE:
return 0;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);

Graphics graphics(hdc);
//LoadBkImge(hdc);
//标题栏
Rect myRect(0,0,400,25);
Color colors[]={
Color(255,255,255),
Color(247,251,255),
Color(239,239,247),
Color(222,227,231),
Color(231,235,239),
Color(214,219,222)
};
float positions[]={
0.0f,
0.2f,
0.4f,
0.6f,
0.8f,
1.0f
};
FillRec(hdc,myRect,colors,positions,6);
//菜单栏
Rect myRect1(0,26,400,30);
Color colors1[]={
Color(251,231,252),
Color(199,222,251),
Color(181,211,251),
Color(162,201,250),
Color(139,188,250),
Color(120,178,250)
};
float positions1[]={
0.0f,
0.2f,
0.4f,
0.6f,
0.8f,
1.0f
};
FillRec(hdc,myRect1,colors1,positions1,6);
/*SolidBrush brush11(Color(255,240,240,240));
graphics.FillRectangle(&brush11,myRect1);*/

Pen pen1(Color(255,255,255,255));
Pen pen2(Color(255,160,160,160));
Pen pen3(Color(255,181,178,181));
Pen pen4(Color(255,255,0,0));
{
graphics.DrawLine(&pen1,0,0,400,0);
graphics.DrawLine(&pen2,0,1,400,1);
graphics.DrawLine(&pen4,0,25,400,25);
}
{
graphics.DrawLine(&pen1,0,250,400,250);
graphics.DrawLine(&pen2,0,249,400,249);
graphics.DrawLine(&pen1,0,248,400,248);
}
{
graphics.DrawLine(&pen1,0,0,0,250);
graphics.DrawLine(&pen2,1,0,1,250);
}
{
graphics.DrawLine(&pen1,400,0,400,250);
graphics.DrawLine(&pen2,399,0,399,250);
graphics.DrawLine(&pen1,398,2,398,250);
}
//菜单栏
graphics.DrawLine(&pen1,0,56,400,56);
graphics.DrawLine(&pen3,0,57,400,57);

DrawIco(hdc);
EndPaint(hWnd, &ps);
return 0;
}
case WM_CTLCOLORBTN:
{
HBRUSH hBrush;
//hBrush = CreateSolidBrush(RGB(255, 0, 0));
hBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
return (long)hBrush;
}

case WM_COMMAND:
{
switch(wParam)
{
case ID_BUTTON1:
{
ShowWindow(hWnd,SW_SHOWMINIMIZED);
break;
}
case ID_BUTTON2:
{
PostQuitMessage(0);
break;
}
case ID_MAINPAGE:
{
POINT pt;
pt.x=10;
pt.y=52;
break;
}
//
}

return 0;

}

case WM_DRAWITEM:
{

pdis=(LPDRAWITEMSTRUCT)lParam;
//SetBkMode(pdis->hDC, TRANSPARENT );
//FillRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(NULL));
//FrameRect(pdis->hDC,&pdis->rcItem,(HBRUSH)GetStockObject(BLACK_BRUSH));

int cx=pdis->rcItem.right-pdis->rcItem.bottom;
int cy=pdis->rcItem.bottom-pdis->rcItem.top;

switch(pdis->CtlID)
{

case ID_BUTTON1:
{
Graphics graphics(pdis->hDC);
Rect rectt(0,0,40,30);
Color colors[]={
Color(247,251,255),
Color(239,239,247),
Color(222,227,231),
Color(231,235,239),
};
float positions[]={
0.0f,
0.333333f,
0.666666f,
1.0f
};
//FillRec(pdis->hDC,rectt,colors,positions,4);
{
SolidBrush  m_pBrush(Color(255,0,0,0));
PointF point1(3.0f, 6.5f);
PointF point2(12.0f, 6.5f);
PointF point3(12.0f, 8.50f);
PointF point4(3.0f, 8.50f);
PointF points[4] = {point1, point2, point3,point4};
graphics.FillPolygon(&m_pBrush, points, 4, FillModeAlternate);
}

break;
}

case ID_BUTTON2:
{
Graphics graphics(pdis->hDC);
Rect rectt(0,0,40,30);
Color colors[]={
Color(247,251,255),
Color(239,239,247),
Color(222,227,231),
Color(231,235,239),
};
float positions[]={
0.0f,
0.33333f,
0.66633f,
1.0f
};
//FillRec(pdis->hDC,rectt,colors,positions,4);

SolidBrush  m_pBrush(Color(255,0,0,0));
//
PointF point1(2.0f, 4.0f);
PointF point2(4.0f, 2.0f);
PointF point3(13.0f, 11.0f);
PointF point4(11.0f, 13.0f);
PointF points[4] = {point1, point2, point3,point4};
graphics.FillPolygon(&m_pBrush, points, 4, FillModeAlternate);
//
PointF point5(13.0f, 4.0f);
PointF point6(11.0f, 2.0f);
PointF point7(2.0f, 11.0f);
PointF point8(4.0f, 13.0f);

PointF points2[4] = {point5, point6, point7,point8    };
graphics.FillPolygon(&m_pBrush, points2, 4, FillModeAlternate);
break;
}
case ID_MAINPAGE:
{
if(btnEntry==TRUE)
{
//bitmap2.png
Graphics graphics( pdis->hDC);
Image image(L"bitmap2.png", FALSE);
graphics.DrawImage(&image, 0,0);
}
else
{
Graphics graphics( pdis->hDC);
Image image(L"bitmap.png", FALSE);
graphics.DrawImage(&image, 0,0);

}

//TransparentBitmap(pdis->hDC,hBitmap1,-5,-5,20,20,RGB(255,255,255));
//ReleaseDC(
break;
}

}
//

if (pdis->itemState & ODS_SELECTED)
{
Graphics graphics(hdc);
SolidBrush brush2(Color(255,255,0,0));
//graphics.FillPolygon(&brush2,pointt,7,FillModeAlternate);

//InvertRect (pdis->hDC, &pdis->rcItem) ;
}

//  Draw a focus rectangle if the button has the focus

if (pdis->itemState & ODS_FOCUS)
{
pdis->rcItem.left   += cx / 16 ;
pdis->rcItem.top    += cy / 16 ;
pdis->rcItem.right  -= cx / 16 ;
pdis->rcItem.bottom -= cy / 16 ;

DrawFocusRect (pdis->hDC, &pdis->rcItem) ;
}
return 0 ;
}

case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_LBUTTONDOWN:
SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}

LRESULT CALLBACK BtnProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_MOUSEMOVE:
{
if(btnEntry == FALSE)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.dwHoverTime = 1;
tme.hwndTrack = hwnd;
TrackMouseEvent(&tme);
}
break;
}
case WM_MOUSEHOVER:
{
btnEntry = TRUE;
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
break;
}
case WM_MOUSELEAVE:
{
btnEntry = FALSE;
InvalidateRect(hwnd,NULL,TRUE);
UpdateWindow(hwnd);
break;
}
default:return CallWindowProc(oldBtnProc,hwnd,uMsg,wParam,lParam);
}
}


程序的瑕疵比较多的,但至少实现了功能,以后私底下慢慢完善吧。再封装一下。并且,关于GDI+和自绘按钮就学到这。以后写程序的时候,这个就是起点了,需要实现其他的效果,再研究
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: