30/09/2018, 20:12
Ai có ai biết về lập trình api win 32 C++. Cho mình hỏi về cách add toolbar. Mình add nhưng đang bị lỗi và chưa tìm được cách sửa
đây là code của mình
#include <windows.h>
#include "resource.h"
#include "wchar.h"
#include <CommCtrl.h>
#include<afxres.h>
HWND CreateSimpleToolbar(HWND hWndParent);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND CreateToolbar(HWND hwnd);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = MAKEINTRESOURCE(112);
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("The Hello Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
1024,
512,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit,hwndStatus,hwndlbl,hWndtoolbar;
int resual;
LPCTBBUTTON button;
static HWND hwndtoolbar;
static int cxClient, cyClient;
HINSTANCE hInst = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE);
HBITMAP hbPict = LoadBitmap (hInst, MAKEINTRESOURCE (IDB_BITMAP1));
switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
MoveWindow(hwndEdit, 100, 50, cxClient-200, cyClient-70, TRUE);
SetScrollRange(hwndEdit,1,1,1,TRUE);
SetScrollPos (hwndEdit, cxClient+50, cyClient+50, TRUE) ;
return 0;
case WM_CREATE:
hwndEdit = CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_LEFT | ES_RIGHT|ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL|WS_BORDER,0, 0, 1000, 450, hwnd, (HMENU)0, NULL, NULL);
hwndStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,hwnd, NULL, GetModuleHandle(NULL), NULL);
//hwndtoolbar =CreateToolbarEx(hwnd, WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS,IDR_TOOLBAR1,8,NULL,(UINT) hbPict,button,1,16,16,16,16,sizeof(LPCTBBUTTON));
hwndtoolbar = CreateSimpleToolbar(hwnd);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case 40004:
resual = MessageBox(NULL, TEXT("You want to exit now?"), TEXT("Information"), MB_YESNO | MB_ICONINFORMATION);
if (resual == IDYES)
{
PostQuitMessage(0);
}
break;
case 40001:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
HIMAGELIST g_hImageList = NULL;
HWND CreateSimpleToolbar(HWND hWndParent)
{
// Declare and initialize local constants.
const int ImageListID = 0;
const int numButtons = 3;
const int bitmapSize = 16;
const DWORD buttonStyles = BTNS_AUTOSIZE;
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
hWndParent, NULL,NULL,NULL);
if (hWndToolbar == NULL)
return NULL;
// Create the image list.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize, // Dimensions of individual bitmaps.
ILC_COLOR16 | ILC_MASK, // Ensures transparent background.
numButtons, 0);
// Set the image list.
SendMessage(hWndToolbar, TB_SETIMAGELIST,
(WPARAM)ImageListID,
(LPARAM)g_hImageList);
// Load the button images.
SendMessage(hWndToolbar, TB_LOADIMAGES,
(WPARAM)IDB_STD_SMALL_COLOR,
(LPARAM)HINST_COMMCTRL);
// Initialize button info.
// IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.
TBBUTTON tbButtons[numButtons] =
{
{ MAKELONG(STD_FILENEW, ImageListID),IDM_NEW, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" },
{ MAKELONG(STD_FILEOPEN, ImageListID),IDM_OPEN ,TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Open"},
{ MAKELONG(STD_FILESAVE, ImageListID),IDM_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)L"Save"}
};
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
// Resize the toolbar, and then show it.
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, TRUE);
return hWndToolbar;
}

Trung Duc
viết 22:13 ngày 30/09/2018

Bài liên quan