解决前面的问题。实现鼠标移动进入到按钮的特效。
效果是这样的
鼠标移到按钮上,改变按钮的颜色(这里用的是直接换贴在按钮上的图片)
程序运行
鼠标进入按钮
代码
#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(,,,),);
Pen pen2(Color(,,,),);
Pen pen3(Color(,,,),);
graphics.DrawEllipse(&pen1,,,,);
graphics.DrawEllipse(&pen2,,,,);
graphics.DrawEllipse(&pen3,,,,);//calc
//text
HFONT hOldFont;
HFONT hFont = CreateFont(,,,,,,,,OEM_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH |FF_SCRIPT,"script" );
hOldFont = (HFONT)SelectObject(hdc,hFont);
SetTextColor(hdc,RGB(,,));//
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,,,"www.cnblogs.com",sizeof("www.cnblogs.com")-);//calc
SelectObject(hdc,hOldFont);
DeleteObject(hFont);
}
void LoadBkImge(HDC hdc)
{
Graphics graphics(hdc);
Image image(L"pic1.png");
graphics.DrawImage(&image,,);
} void FillRec(HDC hdc,Rect myRect,Color* colors,float *positions,int i)
{
Graphics graphics(hdc);
//多彩渐变色
LinearGradientBrush myLinearGradientBrush(
myRect,
Color(,,,),
Color(,,,),
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 = ;
wndClass.cbWndExtra = ;
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,
,
,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd); while(GetMessage(&msg, NULL, , ))
{
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)/;
rect.top=(scrHeight-rect.bottom)/;
SetWindowPos(hWnd,HWND_TOP,rect.left,rect.top,rect.right,rect.bottom,SWP_SHOWWINDOW);
hButton1=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,,,,,hWnd,(HMENU)ID_BUTTON1,hInst,NULL);
hButton2=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,,,,,hWnd,(HMENU)ID_BUTTON2,hInst,NULL);
//
hMainPage=CreateWindow("button","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,,,,,hWnd,(HMENU)ID_MAINPAGE,hInst,NULL);
oldBtnProc=(WNDPROC)SetWindowLong(hMainPage,GWL_WNDPROC,(long)BtnProc);
//
return ;
}
case WM_SIZE:
return ;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps); Graphics graphics(hdc);
//LoadBkImge(hdc);
//标题栏
Rect myRect(,,,);
Color colors[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,)
};
float positions[]={
0.0f,
0.2f,
0.4f,
0.6f,
0.8f,
1.0f
};
FillRec(hdc,myRect,colors,positions,);
//菜单栏
Rect myRect1(,,,);
Color colors1[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,)
};
float positions1[]={
0.0f,
0.2f,
0.4f,
0.6f,
0.8f,
1.0f
};
FillRec(hdc,myRect1,colors1,positions1,);
/*SolidBrush brush11(Color(255,240,240,240));
graphics.FillRectangle(&brush11,myRect1);*/ Pen pen1(Color(,,,));
Pen pen2(Color(,,,));
Pen pen3(Color(,,,));
Pen pen4(Color(,,,));
{
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen2,,,,);
graphics.DrawLine(&pen4,,,,);
}
{
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen2,,,,);
graphics.DrawLine(&pen1,,,,);
}
{
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen2,,,,);
}
{
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen2,,,,);
graphics.DrawLine(&pen1,,,,);
}
//菜单栏
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen3,,,,); DrawIco(hdc);
EndPaint(hWnd, &ps);
return ;
}
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();
break;
}
case ID_MAINPAGE:
{
POINT pt;
pt.x=;
pt.y=;
break;
}
//
} return ; } 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(,,,);
Color colors[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
};
float positions[]={
0.0f,
0.333333f,
0.666666f,
1.0f
};
//FillRec(pdis->hDC,rectt,colors,positions,4);
{
SolidBrush m_pBrush(Color(,,,));
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[] = {point1, point2, point3,point4};
graphics.FillPolygon(&m_pBrush, points, , FillModeAlternate);
} break;
} case ID_BUTTON2:
{
Graphics graphics(pdis->hDC);
Rect rectt(,,,);
Color colors[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
};
float positions[]={
0.0f,
0.33333f,
0.66633f,
1.0f
};
//FillRec(pdis->hDC,rectt,colors,positions,4); SolidBrush m_pBrush(Color(,,,));
//
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[] = {point1, point2, point3,point4};
graphics.FillPolygon(&m_pBrush, points, , 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[] = {point5, point6, point7,point8 };
graphics.FillPolygon(&m_pBrush, points2, , FillModeAlternate);
break;
}
case ID_MAINPAGE:
{
if(btnEntry==TRUE)
{
//bitmap2.png
Graphics graphics( pdis->hDC);
Image image(L"bitmap2.png", FALSE);
graphics.DrawImage(&image, ,);
}
else
{
Graphics graphics( pdis->hDC);
Image image(L"bitmap.png", FALSE);
graphics.DrawImage(&image, ,); } //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(,,,));
//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 / ;
pdis->rcItem.top += cy / ;
pdis->rcItem.right -= cx / ;
pdis->rcItem.bottom -= cy / ; DrawFocusRect (pdis->hDC, &pdis->rcItem) ;
}
return ;
} case WM_DESTROY:
PostQuitMessage();
return ;
case WM_LBUTTONDOWN:
SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,);
return ;
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 = ;
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+和自绘按钮就学到这。以后写程序的时候,这个就是起点了,需要实现其他的效果,再研究