我写了一个小程序,想创建一个工具栏,用SDK,请问是不是要在资源编辑器中创建一个工具栏呢,能直接用代码创建吗?
我的程序是这样的:
#include<windows.h>
LRESULT CALLBACK WndProc
( HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.hCursor=::LoadCursor(NULL,IDC_ARROW);
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName="Draw";
wc.lpszMenuName=NULL;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.style=CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wc))
{
::MessageBox(NULL,"窗口注册失败","提示",MB_OK);
}
HWND hwnd=::CreateWindow("Draw","画图板",WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam;
}
LRESULT CALLBACK WndProc
( HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
HWND hToolBar;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
::EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
我的程序是这样的:
#include<windows.h>
LRESULT CALLBACK WndProc
( HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.hCursor=::LoadCursor(NULL,IDC_ARROW);
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName="Draw";
wc.lpszMenuName=NULL;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.style=CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wc))
{
::MessageBox(NULL,"窗口注册失败","提示",MB_OK);
}
HWND hwnd=::CreateWindow("Draw","画图板",WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
::ShowWindow(hwnd,nCmdShow);
::UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam;
}
LRESULT CALLBACK WndProc
( HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hdc;
HWND hToolBar;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
::EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return ::DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
The following example shows how to use theCreateWindowEx function to create a toolbar that the user can customize and that has a tooltip control associated with it. The example uses the TB_ADDBITMAP and TB_ADDSTRING messages to add button images and buttons strings to the toolbar. The example also adds three buttons by using the TB_ADDBUTTONS message. // CreateAToolBar - creates a toolbar and adds the initial set of
// buttons to it.
// Returns the handle to the toolbar if successful, or NULL otherwise.
// hwndParent - handle to the parent window.
HWND CreateAToolBar(HWND hwndParent)
{
HWND hwndTB;
TBADDBITMAP tbab;
TBBUTTON tbb[3];
char szBuf[16];
int iCut, iCopy, iPaste;
// Ensure that the common control DLL is loaded.
InitCommonControls();
// Create a toolbar that the user can customize and that has a
// tooltip associated with it.
hwndTB = CreateWindowEx(0, TOOLBARCLASSNAME, (LPSTR) NULL,
WS_CHILD | TBSTYLE_TOOLTIPS | CCS_ADJUSTABLE,
0, 0, 0, 0, hwndParent, (HMENU) ID_TOOLBAR, g_hinst, NULL);
// Send the TB_BUTTONSTRUCTSIZE message, which is required for
// backward compatibility.
SendMessage(hwndTB, TB_BUTTONSTRUCTSIZE,
(WPARAM) sizeof(TBBUTTON), 0);
// Add the bitmap containing button images to the toolbar.
tbab.hInst = g_hinst;
tbab.nID = IDB_BUTTONS;
SendMessage(hwndTB, TB_ADDBITMAP, (WPARAM) NUM_BUTTON_BITMAPS,
(WPARAM) &tbab);
// Add the button strings to the toolbar.
LoadString(g_hinst, IDS_CUT, (LPSTR) &szBuf, MAX_LEN);
iCut = SendMessage(hwndTB, TB_ADDSTRING, 0, (LPARAM) (LPSTR) szBuf);
LoadString(g_hinst, IDS_COPY, (LPSTR) &szBuf, MAX_LEN);
iCopy = SendMessage(hwndTB, TB_ADDSTRING, (WPARAM) 0,
(LPARAM) (LPSTR) szBuf);
LoadString(g_hinst, IDS_PASTE, (LPSTR) &szBuf, MAX_LEN);
iPaste = SendMessage(hwndTB, TB_ADDSTRING, (WPARAM) 0,
(LPARAM) (LPSTR) szBuf);
// Fill the TBBUTTON array with button information, and add the
// buttons to the toolbar.
tbb[0].iBitmap = BMP_CUT;
tbb[0].idCommand = IDM_CUT;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].dwData = 0;
tbb[0].iString = iCut;
tbb[1].iBitmap = BMP_COPY;
tbb[1].idCommand = IDM_COPY;
tbb[1].fsState = TBSTATE_ENABLED;
tbb[1].fsStyle = TBSTYLE_BUTTON;
tbb[1].dwData = 0;
tbb[1].iString = iCopy;
tbb[2].iBitmap = BMP_PASTE;
tbb[2].idCommand = IDM_PASTE;
tbb[2].fsState = TBSTATE_ENABLED;
tbb[2].fsStyle = TBSTYLE_BUTTON;
tbb[2].dwData = 0;
tbb[2].iString = iPaste;
SendMessage(hwndTB, TB_ADDBUTTONS, (WPARAM) NUM_BUTTONS,
(LPARAM) (LPTBBUTTON) &tbb);
SendMessage(hwndTB, TB_AUTOSIZE, 0, 0); ShowWindow(hwndTB, SW_SHOW);
return hwndTB;
} 具体的写法参考MSDN