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

效果是这样的

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

程序运行

鼠标进入按钮

代码

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

win32自绘按钮,使用GDI+(三)的更多相关文章

  1. win32自绘按钮,使用GDI+(二)

    一.解决上一篇的两个问题. 1.按钮背景透明 方法是,在绘制按钮之前,向按钮的父窗口发生WM_CTLCOLORBTN消息.该消息返回一个画刷句柄,系统使用该画刷句柄画出按钮的背景.所以我们在处理这个消 ...

  2. win32自绘按钮,使用GDI+(一)

    第一次写随笔,我本来想将win32窗口的标题栏设置成渐变色,像这样的效果 但发现找不到设置标题栏属性的api,SetWindowLong也只是增减窗口的固定的样式而已.所以想到一个思路,把标题栏去掉, ...

  3. VC++ WIN32 sdk实现按钮自绘详解.

    网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片:    首先建立一个标准的Win32 Application 工程.选择a simple Wi ...

  4. VC++ WIN32 sdk实现按钮自绘详解 之二(关键是BS_OWNERDRAW和WM_DRAWITEM)

    网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片:    首先建立一个标准的Win32 Application 工程.选择a simple Wi ...

  5. VC++ WIN32 sdk实现按钮自绘详解 之二.

    网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片:    首先建立一个标准的Win32 Application 工程.选择a simple Wi ...

  6. MFC自绘按钮的实现,按钮动态效果

    最近项目需要实现按钮的动态效果,多方学习,现在终于能实现一些功能了. 过程如下: 第一,新建一MFC对话框应用程序. 第二,删除自带按钮,并添加两个按钮,button1,button2,ID为IDB_ ...

  7. swing重绘按钮为任意形状图案的方法

    swing重绘按钮为任意形状图案的方法 摘自https://www.jb51.net/article/131290.htm 转载  更新时间:2017年12月22日 13:43:00   作者:_Th ...

  8. Eclipse的debug按钮介绍(三)

    本文链接:https://blog.csdn.net/u011781521/article/details/55000066    http://blog.csdn.net/u010075335/ar ...

  9. 自绘按钮,添加Color属性(转载)

    在标准的Windows程序中所有按钮均没有颜色.因此Delphi提供的所有按钮组件也均无颜色属性,有时你可能做了一个五颜六色的程序界面,而按钮颜色可能很不相称. 在此本人提供一种用自定义组件制作有颜色 ...

随机推荐

  1. CSSOM视图模式(CSSOM View Module)相关整理:scrollWidth,scrollLeft,offsetLeft,clientX ,offsetX 定义和区别

    转:http://www.zhangxinxu.com/wordpress/2011/09/cssom%E8%A7%86%E5%9B%BE%E6%A8%A1%E5%BC%8Fcssom-view-mo ...

  2. CALayer 2 详解 -----转自李明杰

    CALayer2-创建新的层   本文目录 一.添加一个简单的图层 二.添加一个显示图片的图层 三.为什么CALayer中使用CGColorRef和CGImageRef这2种数据类型,而不用UICol ...

  3. 【图像处理】【SEED-VPM】1.注意点

    1. 新装系统要启动NFX 每次虚拟机复位要执行以下命令 /usr/sbin/exportfs -av /sbin/service nfs restart —————————————————————— ...

  4. php翻页

    <?php$conn = mysql_connect("localhost","root","") or die("连接数据 ...

  5. thunkify和co的邂逅

    上一篇说到thunkify的作用,这一篇说一下thunkify和co的集合 co中有一块代码 /** * Convert a thunk to a promise. * * @param {Funct ...

  6. lnmp 设置ci pathinfo和去掉index.php

    LNMP上各个版本pathinfo各个版本的设置基本一样: lnmp v1.1上,修改对应虚拟主机的配置文件去掉#include pathinfo.conf前面的#,把try_files $uri = ...

  7. CSS表达式

    一直以来我们被教育说CSS Expression是个坏东西,很影响性能,应该禁止使用,但是如果仔细想想CSS表达式影响性能的原因,规避掉影响性能的写法,CSS表达式还是能给我们带来一些惊喜的.CSS表 ...

  8. 静态绑定网关,防止ARP攻击

    Windows XP 下 写个批处理文件:内容如下,名称自取 @echo offard -darp -s 192.168.1.1 00-14-78-ef-10-45//绑定IP地址 WIN 7 下 1 ...

  9. android 开发中 添加库文件 和so 文件的存放位置和添加依赖

    so文件一般存储在  main 当中  jniLibs 当中 然后在build.gradle中添加 sourceSets { main { jniLibs.srcDirs = ['src/main/j ...

  10. Xcode开发小问题集锦

    Q:用Xcode 6 创建的工程在iOS 7的设备上运行时上下部均会出现黑色的区域且应用不能全屏运行. A:Targets -> General -> App Icons and Laun ...