自定义按钮(API) 修改按钮的颜色

tangshan305 2010-09-09 10:38:57
我先创建了一个窗口,在在上面创建了几个BUTTON 按钮(用预定义的"button"类)。
我怎么改变这个按钮的背景颜色?(我用的是纯的win 32 API 编程)希望
能够尽量的详细点 非常感谢!我是一名初学者,希望能给了例子或代码。谢谢!
...全文
796 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qthl006 2012-08-03
  • 打赏
  • 举报
回复
楼主说的是这个么???

关键字:

3D Button API是由MultiMedia Soft开发的最新的3D按钮软件开发包(SDK),源于ActiveX 控件3D Active Button Magic的经验,在您现有的应用程序和新的开发工具中,它代替了过时的Windows平面直角按钮。对于喜欢使用DLL组件而不喜欢ActiveX控件的开发者来说,它也是3D按钮的替代品。此产品包含在产品集合 3D Button Suite 中。

具体功能:

不论您为销售点市场开发一个简单的Windows应用程序、一个互动对话亭或是触屏应用程序,3D Button API都会为您提供一个出色用户界面,并快速将您的应用程序及时推向市场。

用新版的SDK,您可以创建具有现代渲染界面的出色的3D按钮,例如Aqua, Gel, Vista's "Aero", Office 2003, Office 2007等:请看下面一些小界面的外观图:



您的应用程序已经开发出来了?没问题!不必改变现有的“业务逻辑”代码,仅用几步您就可以改变按钮的“外观和感觉”。您只需在3D Button API的函数中添加几个调用,就可以改变按钮的外观。

停止浪费时间,您自己设计位图按钮并开始使用3D Button API。改变用户界面,用这个精心制作的3D按钮软件开发包(SDK)来设计,仅需几分钟的时间就可以完成过去需要几天甚至几个月的任务。

给您的windows,对话框,窗体一个全新的样子,用您最喜欢的形状,3D效果、界面、结构、位图、光标、动画、声音、颜色和字体。在试用包里您会发现一些示例程序,它们向您展示了这个强大软件的易用性。

3D Button API
tangshan305 2010-09-10
  • 打赏
  • 举报
回复
谢谢 各位
搞定了,
就加了一个可以重绘的风格
然后就可以自己修改颜色了
tangshan305 2010-09-09
  • 打赏
  • 举报
回复

#include "FontControls.h"


BOOL OnCtrlColor(WPARAM wParam, LPARAM lParam)
{
SetBkColor((HDC)wParam, backcolor) ;
return (BOOL)hbrush; //返回画刷
}



LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
static COLORREF customcolors[16];
HINSTANCE hInst = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);//对象的实例句柄

static LOGBRUSH logbrush;

switch (iMsg)
{
case WM_CREATE:
//here is our combobox we do not need the HMENU parameter in this example
//since im not checking for any notifications here
combo = CreateWindow("combobox",NULL,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
15,15,130,200,hWnd,(HMENU)NULL,hInst,NULL);
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
SendMessage(combo,CB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");

//here is our edit control
edit = CreateWindow("edit","edit control",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,
15,40,130,25,hWnd,(HMENU)NULL,hInst,NULL);
//here is the listbox
list = CreateWindow("listbox","",WS_CHILD|WS_VISIBLE|LBS_STANDARD,15,70,100,150,
hWnd,(HMENU)NULL,hInst,NULL);
SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item1");
SendMessage(list,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"item2");
//here is the button
button = CreateWindow("button","Checkbox",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,15,230,100,20,hWnd,
(HMENU)NULL,hInst,NULL);
//static:
stat = CreateWindow("static","Hi, i'm a static\nColor me!",WS_CHILD|WS_VISIBLE|SS_CENTER,
150,105,150,50,hWnd,(HMENU)NULL,hInst,NULL);
//here is the buttons to change to colors
changetxtcolor = CreateWindow("button","Change Txt Color",WS_VISIBLE|WS_CHILD,
150,15,150,25,hWnd,(HMENU)CHANGETEXT,hInst,NULL);
changebkcolor = CreateWindow("button","Change BK Color",WS_VISIBLE|WS_CHILD|BS_OWNDERDRAW,
150,45,150,25,hWnd,(HMENU)CHANGEBKG,hInst,NULL);
//we need to set up the brush
logbrush.lbHatch = 0;//模板
logbrush.lbStyle = BS_SOLID;//物理刷子的风格
logbrush.lbColor = RGB(255,255,255); //initially set it to black颜色
hbrush = CreateBrushIndirect(&logbrush);// logbrush被Windows的CreateBrushIndirect和ExtCreatePen函数使用。

//and finally we set up the choosecolor struct for that common dialog
SetupChooseColor(&choosecolor,customcolors,hWnd,currentcolor);
//NEW CODE
changefont = CreateWindow("button","Change Font",WS_VISIBLE|WS_CHILD,150,75,150,25,
hWnd,(HMENU)CHANGEFONT,hInst,NULL);


SetupChooseFont(hWnd,currentfont,logfont);
currentfont = CreateFontIndirect(&logfont);
UpdateFonts();
break;
case WM_PAINT://当视窗显示区域的一部分显示内容或者全部变为「无效」,以致于必须「更新画面」时,将由这个讯息通知程式。
// hdc = BeginPaint(hWnd,&ps);
hdc = ::GetWindowDC(hWnd);
// InvalidateRect(hWnd,NULL,true);//去掉之后不能显示颜色了 几个重绘函数

InvalidateRect(NULL,NULL,TRUE);

// EndPaint(hWnd,&ps);
ReleaseDC(hWnd, hdc);
break;
//since all of the controls will have the same color attributes i can
//switch them all at once like so, use seperate ones if you need but return
//the brush for each of them
case WM_CTLCOLORSTATIC://WM_CTLCOLORSTATIC是当静态控件重画时,控件给父窗口发送的消息,在处理该消息时应该返回一个画刷,系统用这个画刷重绘控件的背景颜色。

case WM_CTLCOLOREDIT://WM_CTLCOLOREDIT 控制EDIT控件外观
case WM_CTLCOLORLISTBOX://LISTBOX
case WM_CTLCOLORBTN:// 当一个按钮控件将要被绘制时
// 发送此消息给它的父窗口 通过响应这条消息,所有者窗口可以通过使用给定的相关显示设备的句柄来设置按纽的文本和背景颜色

SetTextColor((HDC)wParam,textcolor);//前景色
SetBkMode((HDC)wParam,OPAQUE); //*look on the bottom for more info //非透明
SetBkColor((HDC)wParam,backcolor); //一个背景,
logbrush.lbColor = backcolor;
//now because ive changed the logbrush have to update the brush
//if your keeping your control at a constant color just set the text and
//return the brush, no need for this in that case
hbrush = CreateBrushIndirect(&logbrush);

return (long)hbrush;
// return OnCtrlColor(wParam, lParam);
break;
case WM_CLOSE:
DeleteObject(hbrush);
DeleteObject(currentfont);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;

case WM_COMMAND:
switch(LOWORD(wParam))
{
case CHANGETEXT:
if(ChooseColor(&choosecolor))//调用窗口
{
currentcolor = choosecolor.rgbResult;//set currentcolor cause
//we need that variable to have value for the RGB_INIT
textcolor = currentcolor;
SendMessage(hWnd,WM_PAINT,0,0);
}
break;
case CHANGEBKG:
if(ChooseColor(&choosecolor))
{
currentcolor = choosecolor.rgbResult;//set currentcolor cause
//we need that variable to have value for the RGB_INIT
backcolor = currentcolor;
SendMessage(hWnd,WM_PAINT,0,0);

}
break;
//NEW CODE


case CHANGEFONT:
if(ChooseFont(&choosefont))//call the dialog for fonts
{

currentfont = CreateFontIndirect(&logfont);
UpdateFonts(); //update windows
SendMessage(hWnd,WM_PAINT,0,0);

}
break;

}
}
return DefWindowProc(hWnd,iMsg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{


const char *ClassName = "PCPP_ColorControls";
WNDCLASSEX wclass; //here we set up a variable for our window class 定义一个窗口结构的对象
MSG msg; //this will hold the window messages later on
//Next we set up the properties of the window class
wclass.cbSize = sizeof(wclass); //the size 设置窗口结构的大小
wclass.style = CS_HREDRAW | CS_VREDRAW; //style
wclass.lpfnWndProc = WndProc; //this tells windows the function to send messages to 设置窗口消息处理函数WndProc
wclass.cbClsExtra = 0;
wclass.cbWndExtra = 0;
wclass.hInstance = hInstance; //the instance of your program
wclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); //which icon to use
wclass.hCursor = LoadCursor(NULL,IDC_ARROW); //the cursor to use
wclass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //background color
wclass.lpszMenuName = NULL; //set the menu
wclass.lpszClassName = ClassName; //set classname 设置窗口类型名称。
wclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); //set small icon 设置窗口小图标。


RegisterClassEx(&wclass); //this registers your window with windows 调用函数RegisterClassEx注册这个窗口类型

main = CreateWindow(ClassName, "Neato Fonts :-)",//通过调用CreateWindow或CreateWindowEx函数来创建控件,这时必须指定控件的窗口类
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 200, 200, 320, 300,NULL, NULL, hInstance, NULL);


while(GetMessage(&msg,NULL,0,0)) //getmessage loops until a message is on the queue
{ //then it returns focus,peekmessage does the same except returns focus no matter what
TranslateMessage(&msg); //translate the message into its char equivelent 把虚拟键消息转换为字符消息
DispatchMessage(&msg); //dispatch to the window 把这条消息发送到窗口里的消息处理函数WindowProc
}


return msg.wParam;//消息的参数
}


这是代码 能帮忙说下吗?我刚开始学习这个
hilevel 2010-09-09
  • 打赏
  • 举报
回复
3楼正解
Eleven 2010-09-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tangshan305 的回复:]
我试了,不行的,对按钮根本不起作用
[/Quote]
SetWindowLong(按钮HWND,GWL_WNDPROC, ...);
处理WM_DRAWITEM消息,创建按钮的时候CreateWindow带上BS_OWNDERDRAW属性
tangshan305 2010-09-09
  • 打赏
  • 举报
回复
我试了,不行的,对按钮根本不起作用
stjay 2010-09-09
  • 打赏
  • 举报
回复
父窗口响应WM_CTLCOLORBTN消息
jack54877012 2010-09-09
  • 打赏
  • 举报
回复
。。。楼主我建议你将《WINDOWS程序设计》好好看一下,里面有很详细的讲解
tangshan305 2010-09-09
  • 打赏
  • 举报
回复
能具体点吗?
我对这块一点都不熟悉!
谢谢了
AIHANZI 2010-09-09
  • 打赏
  • 举报
回复
从CBUTTON类派生一个类,,然后OnEraseBkgnd之类的函数可以改变颜色

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧