我正在为USB控制的逻辑分析仪构建一个win32 gui,我想在那里实现一个工具栏。因此,我选择了一个教程,并在Code :: Blocks中尝试了此代码。它工作正常,除了工具栏图标将替换为灰色框。问题是什么 ??该代码似乎还不错,因为我可以使用分隔符并看到它们的效果。首先,我期望我的toolbar.bmp图像出现位图问题,并使用其他工具创建了它—结果相同。我将不胜感激任何建议...马丁
哦-我发现问题与TBBUTTON tbb.iBitmap有关。如果将其设置为0,我会看到第一个图像(但是不太正确)。如何正确设置此值?
这是我的代码:
// Win32 Tutorial (Toolbars)
// Alan Baylis 2004
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
const char ClassName[] = "MainWindowClass";
HWND hTool;
LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam );
INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow ) {
InitCommonControls();
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = ClassName;
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Failed To Register The Window Class.", "Error", MB_OK | MB_ICONERROR);
return 0;
}
HWND hWnd;
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, ClassName, "Toolbars",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
240, // width
120, // height
NULL, NULL, hInstance, NULL);
if (!hWnd) {
MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
return 0;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) {
switch (Msg) {
case WM_CREATE: {
TBADDBITMAP tbab;
TBBUTTON tbb[3];
hTool = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR)NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
hWnd, (HMENU)IDTB_TOOLBAR, GetModuleHandle(NULL), NULL);
if (!hTool) {
MessageBox(NULL, "Tool Bar Failed.", "Error", MB_OK | MB_ICONERROR);
return 0;
}
// Send the TB_BUTTONSTRUCTSIZE message, which is required for backward compatibility.
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hTool, TB_SETBITMAPSIZE, (WPARAM)0, (LPARAM)MAKELONG(20, 20));
tbab.hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
tbab.nID = IDR_TOOLBAR1;
SendMessage(hTool, TB_ADDBITMAP, (WPARAM) 1, (LPARAM) &tbab);
ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = 20;
tbb[0].idCommand = ID_BUT1_OPEN;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[1].fsStyle = TBSTYLE_SEP;
tbb[2].iBitmap = 21;
tbb[2].idCommand = ID_BUT2_SAVE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
SendMessage(hTool, TB_ADDBUTTONS, 3, (LPARAM)&tbb);
ShowWindow(hTool, SW_SHOW);
}
break;
case WM_COMMAND: {
switch(LOWORD(wParam)) {
case ID_BUT1_OPEN: {
MessageBox(NULL, "Toolbar Button One", "Success", MB_OK | MB_ICONINFORMATION);
}
break;
case ID_BUT2_SAVE: {
MessageBox(NULL, "Toolbar Button Two", "Success", MB_OK | MB_ICONINFORMATION);
}
break;
}
return 0;
}
break;
case WM_SIZE:
SendMessage(hTool, TB_AUTOSIZE, 0, 0);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, Msg, wParam, lParam));
}
return 0;
}
resource.h
#define IDI_ICON 101
#define IDTB_TOOLBAR 200
#define IDR_TOOLBAR1 200
#define ID_BUT1_OPEN 201
#define ID_BUT2_SAVE 202
resource.rc
#include "resource.h"
IDI_ICON ICON "icon.ico"
// Toolbar
IDR_TOOLBAR1 TOOLBAR DISCARDABLE 16, 16
BEGIN
BUTTON ID_BUT1_OPEN
SEPARATOR
BUTTON ID_BUT2_SAVE
END
IDR_TOOLBAR1 BITMAP "toolbar1.bmp"
确定-谢谢,这解决了问题。我之前尝试过,但没有成功-原因是cpp文件中的TB_SETBITMAPSIZE与rc文件中的IDR_TOOLBAR1 TOOLBAR DISCARDABLE大小不一致。谢谢您的帮助