win32自绘按钮,使用GDI+(三)
解决前面的问题。实现鼠标移动进入到按钮的特效。
效果是这样的
鼠标移到按钮上,改变按钮的颜色(这里用的是直接换贴在按钮上的图片)
程序运行
鼠标进入按钮
代码
#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+(三)的更多相关文章
- win32自绘按钮,使用GDI+(二)
一.解决上一篇的两个问题. 1.按钮背景透明 方法是,在绘制按钮之前,向按钮的父窗口发生WM_CTLCOLORBTN消息.该消息返回一个画刷句柄,系统使用该画刷句柄画出按钮的背景.所以我们在处理这个消 ...
- win32自绘按钮,使用GDI+(一)
第一次写随笔,我本来想将win32窗口的标题栏设置成渐变色,像这样的效果 但发现找不到设置标题栏属性的api,SetWindowLong也只是增减窗口的固定的样式而已.所以想到一个思路,把标题栏去掉, ...
- VC++ WIN32 sdk实现按钮自绘详解.
网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片: 首先建立一个标准的Win32 Application 工程.选择a simple Wi ...
- VC++ WIN32 sdk实现按钮自绘详解 之二(关键是BS_OWNERDRAW和WM_DRAWITEM)
网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片: 首先建立一个标准的Win32 Application 工程.选择a simple Wi ...
- VC++ WIN32 sdk实现按钮自绘详解 之二.
网上找了很多,可只是给出代码,没有详细解释,不便初学者理解.我就抄回冷饭.把这个再拿出来说说. 实例图片: 首先建立一个标准的Win32 Application 工程.选择a simple Wi ...
- MFC自绘按钮的实现,按钮动态效果
最近项目需要实现按钮的动态效果,多方学习,现在终于能实现一些功能了. 过程如下: 第一,新建一MFC对话框应用程序. 第二,删除自带按钮,并添加两个按钮,button1,button2,ID为IDB_ ...
- swing重绘按钮为任意形状图案的方法
swing重绘按钮为任意形状图案的方法 摘自https://www.jb51.net/article/131290.htm 转载 更新时间:2017年12月22日 13:43:00 作者:_Th ...
- Eclipse的debug按钮介绍(三)
本文链接:https://blog.csdn.net/u011781521/article/details/55000066 http://blog.csdn.net/u010075335/ar ...
- 自绘按钮,添加Color属性(转载)
在标准的Windows程序中所有按钮均没有颜色.因此Delphi提供的所有按钮组件也均无颜色属性,有时你可能做了一个五颜六色的程序界面,而按钮颜色可能很不相称. 在此本人提供一种用自定义组件制作有颜色 ...
随机推荐
- wampServer安装注意
http://www.glbwl.com/wampServer-403-forbidden.html http://jingyan.baidu.com/article/e75aca8578147d14 ...
- sql行列旋转
一,行转列 先建立测试数据 if OBJECT_ID('week_income') is not null drop table week_income go create table week_in ...
- Cannot send session cache limiter Cannot modify header information
当php报出 Cannot send session cache limiter 或Cannot modify header information 的错误时 其理论上是因为php代码以前有 ...
- PHPCMS后台登陆路径修改方法(V9版)
转自:http://hi.baidu.com/geek_cheng/item/b903ebe7b4ac3af9e0a5d4aa?qq-pf-to=pcqq.c2c 最新发布的PHPCMS V9由于采用 ...
- Android下载压缩文件与解压案例
ackage com.example.jsontest.biz; import java.io.BufferedInputStream; import java.io.BufferedOutputSt ...
- 浅谈对ionic项目的理解
在思考怎么将客户端app连接到服务器的时候,就在想ionic项目的本质是什么,一开始因为ionic serve这一命令,我以为它自己就是个服务器,但是后来一细想又感觉不是这样,不然客户端又该怎么和服务 ...
- C++小项目:directx11图形程序(三):graphicsclass
这是框架的第三层graphicsclass,这个类才真正可以说是整个程序的框架,因为它组织了后面所有的成员. 代码: graphicsclass.h #pragma once #include< ...
- windows8.1下android开发环境搭建(Eclipse+Android sdk+ADT+Genymotion)
一.基本jdk.eclipse环境 二.android sdk 1.下载安装:https://developer.android.com/sdk/installing/index.html?pkg=t ...
- java常用基础知识点 (持续追加)
作为一个Java编程人,会经常用上JDK中的大量的API类库,常用的有字符串操作.集合操作.文本操作.输入输出操作.以及网络操作, 常用的包: 1. java.lang Java程序的基础类,字符串, ...
- setProgressBarIndeterminateVisibility(true);
此为在标题栏 上 设置一个loading 圈 实用...