有关CToolBar上的DropArrow的问题

xiaolizi 2005-09-28 12:00:56
近日在弄一个CToolBar,需要的功能是有一个按钮有DropArrow,点击后出现一个菜单,上面有3个图标,选中其中一个后可以使得该DropArrow关联的CToolBar上的按钮的图标换成选中的图标,请问各位大侠,这个该怎么弄,主要是怎么替换ToolBar上的按钮的图标?
另外,碰到一个郁闷的地方是,为什么在RC编辑器中编辑的ToolBar上的按钮都是灰色的(Disable状态),必需要关联相应的Command响应后才能使用?
...全文
87 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xiaolizi 2005-10-03
这个问题我已经解决了,谢谢楼上所有的朋友。^_^
为了方便以后其他人有类似的问题,贴出具体做法。
CImageList成员函数Replace(id, HICON),可以动态替换索引为id的Image,所以可以使用类似GetToolBarCtrl().GetImageList()->Replace(0, hicon);的方式得到动态替换工具条上某个按钮图标的目的。

结贴了。
回复
QuickGo 2005-10-03
给段代码先:
#include <windows.h>
#include <commctrl.h>
#include <iostream.h>
#include "resource.h"
#define BTNS_WHOLEDROPDOWN 0x80
#define CX_EDIT 140
#define CY_EDIT 24
#define OUT_MESSAGE "This is a test line"

char AppName[]="Win32 Application";
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam);
int err;//for debug


VOID Error(LPCSTR message)//for debug
{
MessageBox(NULL,message,"Runtime Message!",MB_OK|MB_ICONEXCLAMATION);
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd )
{
WNDCLASS wndclass;
HWND hwnd;
MSG msg;

wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=NULL;
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=AppName;
wndclass.lpszMenuName=MAKEINTRESOURCE(ID_MENU);
wndclass.style=0;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"注册窗口类别出错,程序被迫中止!","Warning!",MB_OK|MB_ICONWARNING);

return 1;
}
InitCommonControls();
hwnd=CreateWindow(AppName,"Win32 Application",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,0);

ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);

while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
static HINSTANCE hInstance;
static HWND hToolbar,hRebar,hEdit,hButton,hEmbEdit;
static TBBUTTON Button;
static TBBUTTONINFO ButtonInfo;
static REBARBANDINFO BandInfo;
static HDC hdc;

static LOGFONT lf;
static HFONT hfont;

switch(msg)
{
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lparam)->hInstance;
INITCOMMONCONTROLSEX cc;
cc.dwICC=ICC_COOL_CLASSES|ICC_BAR_CLASSES|ICC_USEREX_CLASSES ;
cc.dwSize=sizeof(cc);
InitCommonControlsEx(&cc);

hRebar=CreateWindowEx(NULL,REBARCLASSNAME,NULL,
WS_CHILD|WS_VISIBLE|WS_DLGFRAME|RBS_BANDBORDERS|RBS_AUTOSIZE,
0,0,0,0,hwnd,(HMENU)334,hInstance,0);

hToolbar=CreateWindowEx(NULL ,TOOLBARCLASSNAME,NULL,
WS_CHILD|WS_VISIBLE|CCS_NODIVIDER|TBSTYLE_LIST|TBSTYLE_WRAPABLE|TBSTYLE_FLAT|CCS_NORESIZE ,
0,0,0,0,hwnd,(HMENU)333,hInstance,0);

hEdit=CreateWindowEx(WS_EX_TOOLWINDOW,
WC_COMBOBOXEX,NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|CBS_DROPDOWNLIST ,0,0,0,0,hRebar,(HMENU)555,hInstance,0);
hEmbEdit=CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",NULL,WS_CHILD|WS_VISIBLE,0,0,CX_EDIT,CY_EDIT,hToolbar,
(HMENU)987,hInstance,0);
hButton=CreateWindowEx(NULL,"button","按钮",WS_CHILD|WS_VISIBLE|BS_FLAT,0,0,0,0,hwnd,(HMENU)3321,hInstance,0);


SendMessage(hToolbar,TB_BUTTONSTRUCTSIZE,(WPARAM)sizeof(TBBUTTON),(LPARAM)0);
SendMessage(hToolbar,TB_SETBUTTONSIZE,(WPARAM)0,(LPARAM)MAKELONG(20,16));
SendMessage(hToolbar,TB_LOADIMAGES,(WPARAM)IDB_HIST_SMALL_COLOR,(LPARAM)HINST_COMMCTRL);
SendMessage(hToolbar,TB_SETEXTENDEDSTYLE,(WPARAM)0,(LPARAM)TBSTYLE_EX_DRAWDDARROWS );

int i;
for(i=335;i<339;i++)
{
Button.idCommand=i;
SendMessage(hToolbar,TB_ADDBUTTONS,(WPARAM)1,(LPARAM)&Button);
}
ButtonInfo.cbSize=sizeof(ButtonInfo);

ButtonInfo.dwMask=TBIF_IMAGE|TBIF_STATE|TBIF_STYLE|TBIF_TEXT|TBIF_SIZE;
ButtonInfo.iImage=-1;
ButtonInfo.fsState=TBSTATE_INDETERMINATE;
ButtonInfo.fsStyle=TBSTYLE_BUTTON;
ButtonInfo.cx=CX_EDIT;
ButtonInfo.pszText=NULL;

SendMessage(hToolbar,TB_SETBUTTONINFO,(WPARAM)335,(LPARAM)&ButtonInfo);

ButtonInfo.dwMask=TBIF_IMAGE|TBIF_STATE|TBIF_STYLE|TBIF_TEXT|TBIF_SIZE;
ButtonInfo.iImage=HIST_FORWARD;
ButtonInfo.fsState=TBSTATE_ENABLED;
ButtonInfo.cx=110;
ButtonInfo.fsStyle=TBSTYLE_DROPDOWN|TBSTYLE_AUTOSIZE;
ButtonInfo.pszText="wow";

SendMessage(hToolbar,TB_SETBUTTONINFO,(WPARAM)336,(LPARAM)&ButtonInfo);

ButtonInfo.dwMask=TBIF_IMAGE|TBIF_STATE|TBIF_STYLE|TBIF_TEXT;
ButtonInfo.iImage=HIST_FAVORITES;
ButtonInfo.fsState=TBSTATE_ENABLED;
ButtonInfo.fsStyle=TBSTYLE_BUTTON|TBSTYLE_AUTOSIZE;
ButtonInfo.pszText=NULL;

SendMessage(hToolbar,TB_SETBUTTONINFO,(WPARAM)337,(LPARAM)&ButtonInfo);

ButtonInfo.dwMask=TBIF_IMAGE|TBIF_STATE|TBIF_STYLE;
ButtonInfo.iImage=HIST_VIEWTREE;
ButtonInfo.fsState=TBSTATE_ENABLED;
ButtonInfo.fsStyle=BTNS_WHOLEDROPDOWN|TBSTYLE_AUTOSIZE;

SendMessage(hToolbar,TB_SETBUTTONINFO,(WPARAM)338,(LPARAM)&ButtonInfo);

BandInfo.cbSize=sizeof(REBARBANDINFO);
BandInfo.fMask=RBBIM_CHILD|RBBIM_CHILDSIZE|RBBIM_ID|RBBIM_STYLE|RBBIM_TEXT;
BandInfo.hwndChild=hToolbar;
BandInfo.cxMinChild=0;
BandInfo.cyMinChild=CY_EDIT;
BandInfo.cyMaxChild=CY_EDIT;
BandInfo.cyChild=CY_EDIT;
BandInfo.wID=9989;
BandInfo.fStyle=RBBS_GRIPPERALWAYS|RBBS_CHILDEDGE|RBBS_BREAK;
BandInfo.lpText=NULL;

SendMessage(hRebar,RB_INSERTBAND,(WPARAM)-1,(LPARAM)&BandInfo);

BandInfo.hwndChild=hEdit;
BandInfo.cyMinChild=22;
BandInfo.cyChild=22;
BandInfo.fStyle=RBBS_GRIPPERALWAYS|RBBS_CHILDEDGE|RBBS_BREAK;
SendMessage(hRebar,RB_INSERTBAND,(WPARAM)-1,(LPARAM)&BandInfo);
err=GetLastError();

BandInfo.hwndChild=hButton;
BandInfo.cyMinChild=22;
BandInfo.cyChild=22;
BandInfo.fStyle=RBBS_GRIPPERALWAYS|RBBS_CHILDEDGE|RBBS_BREAK;
SendMessage(hRebar,RB_INSERTBAND,(WPARAM)-1,(LPARAM)&BandInfo);


strcpy(lf.lfFaceName,"TAHOMA");
lf.lfHeight=-12;
lf.lfWidth=0;

hfont=CreateFontIndirect(&lf);
SendMessage(hEmbEdit,WM_SETFONT,(WPARAM)hfont,(LPARAM)TRUE);
SendMessage(hButton,WM_SETFONT,(WPARAM)hfont,(LPARAM)TRUE);
break;

case WM_SIZE:
SendMessage(hToolbar,WM_SIZE,wparam,lparam);
SendMessage(hRebar,WM_SIZE,wparam,lparam);
SetFocus(hEmbEdit);
return 0;
break;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}

return DefWindowProc(hwnd,msg,wparam,lparam);
}
回复
fgq5188 2005-09-30
不错
回复
xiaolizi 2005-09-30
To zxyjyzxyjy(星星),
具体怎么做?可否详细些?

To laiyiling(Technology),
这个例子很合适,我看看,先谢谢先~~~
回复
xiaolizi 2005-09-28
To pomelowu(羽战士) ,
首先谢谢你的回复。^_^,上面的例子中使用了3个ImageList来处理一般状态,激活状态和禁止状态,而且是所有的ToolBar上的按钮都一致。我想要的是可以动态修改其中某个按钮的图标。类似SetButtonInfo这样的接口直接替换按钮的图标。这个不知道是否可行?

另外,我看到的ToolBar使用,都是在类似CMainFrame中加入ON_COMMAND事件响应函数来处理点击消息,请问此类消息可否直接由CToolBar的继承类内部完成处理,而不需要再通过父窗口的ON_COMMAND?
回复
pomelowu 2005-09-28
替换图标:
http://www.codeproject.com/docking/tbimages.asp
回复
Kudeet 2005-09-28
这里有一个类似功能的,实现了图标的替换,里面的方法你可以借鉴:
http://www.codeproject.com/docking/fotoolbar.asp
回复
zxyjyzxyjy 2005-09-28
第一个问题还得学习,第二个问题是能够象你说的那么做的,不过由CToolBar的继承类内部做会使其功能混乱。
回复
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2005-09-28 12:00
社区公告
暂无公告