第一次写随笔,我本来想将win32窗口的标题栏设置成渐变色,像这样的效果

但发现找不到设置标题栏属性的api,SetWindowLong也只是增减窗口的固定的样式而已。所以想到一个思路,把标题栏去掉,自己绘制一个标题栏,包括标题栏上的按钮都自己来绘制创建。这里用到了gdi+,对于这个库也是刚接触到的。

最后程序实现的效果如下。

代码:

 #ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include <windows.h>
//#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
#define ID_BUTTON1 1
#define ID_BUTTON2 2
HINSTANCE hInst; 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); 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;
PAINTSTRUCT ps;
LPDRAWITEMSTRUCT pdis; switch(message)
{
case WM_CREATE:
{
//窗口居中显示
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); //
hdc=GetDC(hWnd);
return ;
}
case WM_SIZE:
return ;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
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,);
Graphics graphics(hdc);
Pen pen1(Color(,,,));
Pen pen2(Color(,,,));
Pen pen3(Color(,,,));
graphics.DrawLine(&pen1,,,,);
graphics.DrawLine(&pen2,,,,);
graphics.DrawLine(&pen3,,,,); EndPaint(hWnd, &ps);
return ;
}
case WM_COMMAND:
{
switch(wParam)
{
case ID_BUTTON1:
{
ShowWindow(hWnd,SW_SHOWMINIMIZED);
break;
}
case ID_BUTTON2:
{
PostQuitMessage();
break;
}
//
} return ; } case WM_DRAWITEM:
{ pdis=(LPDRAWITEMSTRUCT)lParam;
//SetBkMode(pdis->hDC, TRANSPARENT );
FillRect(pdis->hDC,&pdis->rcItem,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,);
{
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,); 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;
}
//if (pdis->itemState & ODS_SELECTED)
// 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);
}
}

说明:

这里有一个严重的问题,如何设置按钮的背景透明?临时的解决办法是将按钮填充相同的渐变色。如画的标题栏的五种渐变色的position数组值为

       float positions[]={
0.0f,
0.2f,
0.4f,
0.6f,
0.8f,
1.0f
};

颜色为

         Color colors[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,),
Color(,,)
};

根据坐标计算出按钮的渐变色颜色和位置分别为

              Color colors[]={
Color(,,),
Color(,,),
Color(,,),
Color(,,),
};
float positions[]={
0.0f,
0.333333f,
0.666666f,
1.0f
};

这种方法看起来挺笨的,所以最后留两个问题。

问题一:如何设置系统菜单栏为渐变色?

问题二:按钮背景如何透明?

参考:杂七杂八找了很多资料

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

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

    解决前面的问题.实现鼠标移动进入到按钮的特效. 效果是这样的 鼠标移到按钮上,改变按钮的颜色(这里用的是直接换贴在按钮上的图片) 程序运行 鼠标进入按钮 代码 #ifndef ULONG_PTR // ...

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

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

  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. swing重绘按钮为任意形状图案的方法

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

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

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

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

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

  9. 继承CWnd自绘按钮

    头文件: //头文件 #pragma once // CLhsButton #define MYWM_BTN_CLICK WM_USER+3001 //关闭按钮单击响应 //tab按钮的状态 enum ...

随机推荐

  1. Phonegap中自定义插件的使用

    在phonegap中需要实现特定相关的功能,可能需要自定义扩展一下功能,那么扩展phonegap组件就成为了可能. 源代码结构图: 本文目的在于讲述怎么扩展一个phonegap组件以及实现. 针对ph ...

  2. The requested resource is not available...

    运行tomcat 提示如下错误: The requested resource () is not available的解决方案 出现这个问题,接口肯定是没问题了.问题可能有两个: 1.文件设置无法访 ...

  3. Fibonacci(斐波那契)非递归实现。容易看懂

    #include<iostream>using namespace std;int main(){ int n; cout<<"please input a n\n& ...

  4. jQuery中animate的height的自适应

    可以用 animate() 方法几乎可以操作大部分CSS 属性,但其属性的值不能为字符串,很多人都遇到过这个问题.   例如:获取一个元素自适应时的高,但el.animate({height:‘aut ...

  5. C++重载流运算符,将存储结构体的vector直接写入文件

    我们知道,当vector很大的时候,如果使用循环的方式将其中的元素写入文件将非常费时,因此有没有办法将vector一次性写入文件呢? 采用流运算符重载的方法可以做到,不仅基本类型的vector可以一次 ...

  6. 【转】Tomcat的默认访问路径

    放在外网的应用,用户多是直接输入域名访问,相信没有哪个后面还加个尾巴,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml ...

  7. Jacoco入门

    Jacoco介绍 转自:wangmuming 的博客 Jacoco是一个开源的覆盖率工具.Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaA ...

  8. linux安装jdk(以centos安装jdk1.7为例)

    1准备工作: 1 虚拟机一台vmware12,安装64位centos 2 oracle官网下载jdk1.7-linux-x64.rpm 3 winscp将jdk传送到linux上面 2开始安装: 1 ...

  9. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>

    作者:Statmoon 出处:http://leolis.cnblogs.com/   在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...

  10. 如何查看前端部署的tracker代码

    1.www.gov.cn 2.F12>Source>左侧选择static.gridsumdissector.com/js 3.点击代码下方区域的中括号,展开代码preety print{}