Nu har jeg skrevet et program, der opretter en menu, en rebar og en edit. Jeg har skåret alle overflødige konstanter væk, så der kun er den nødvendige kode. Jeg gemmer den lige her, min computer går sikkert i stykker om lidt alligevel.
#define NTDDI_VERSION NTDDI_WIN2K
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#define _WIN32_IE 0x0600
#include <windows.h>
#include <commctrl.h>
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
HWND hRebar;
HWND hToolbar;
HWND hEdit;
DWORD dwSize;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
VOID InitCCEx(void);
VOID CreateRebar(HWND);
// Application entry
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
const TCHAR szClass[] = TEXT("RebarSampleApp");
WNDCLASS wc;
HWND hwnd;
MSG msg;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szClass;
wc.lpszMenuName = NULL;
wc.style = CS_DBLCLKS;
RegisterClass(&wc);
// Initiate common controls
InitCCEx();
hwnd = CreateWindowEx(0L, szClass, TEXT("Rebar sample application"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
300, 300,
NULL, NULL, hInstance, NULL);
// Add menu
HMENU hMenu = CreateMenu();
HMENU hFile = CreateMenu();
HMENU hHelp = CreateMenu();
AppendMenu(hFile, MF_STRING, 0, TEXT("Ny\tCtrl+N"));
AppendMenu(hFile, MF_STRING, 0, TEXT("Åben...\tCtrl+O"));
AppendMenu(hFile, MF_STRING, 0, TEXT("Gem\tCtrl+S"));
AppendMenu(hFile, MF_STRING, 0, TEXT("Gem som...\tCtrl+Shift+S"));
AppendMenu(hFile, MF_SEPARATOR, 0, 0);
AppendMenu(hFile, MF_STRING, 0, TEXT("Afslut"));
AppendMenu(hHelp, MF_STRING, 0, TEXT("Om...\tF1"));
AppendMenu(hMenu, MF_POPUP, (UINT)hFile, TEXT("&Filer"));
AppendMenu(hMenu, MF_POPUP, (UINT)hHelp, TEXT("&Hjælp"));
SetMenu(hwnd, hMenu);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// Init common controls function
void InitCCEx(void)
{
INITCOMMONCONTROLSEX ccex;
ccex.dwICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
ccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&ccex);
}
// Create rebar control
// This function also creates the toolbar,
// it being a part of the rebar
void CreateRebar(HWND hwnd)
{
hRebar = CreateWindowEx(0L, REBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | CCS_NODIVIDER,
0, 0, 100, 25,
hwnd, NULL, NULL, NULL);
REBARBANDINFO bandinfo;
hToolbar = CreateWindowEx(0L, TOOLBARCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | CCS_NODIVIDER,
0, 0, 0, 0,
hwnd, NULL, NULL, NULL);
// Get the size of the tool bar
dwSize = SendMessage(hToolbar, TB_GETBUTTONSIZE, 0, 0);
// Add buttons
TBBUTTON buttons[2];
memset(&buttons, 0, sizeof(buttons));
buttons[0].fsState = TBSTATE_ENABLED;
buttons[0].fsStyle = TBSTYLE_BUTTON;
buttons[1].fsState = TBSTATE_ENABLED;
buttons[1].fsStyle = TBSTYLE_BUTTON;
SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
SendMessage(hToolbar, TB_AUTOSIZE, 0, 0);
UINT uNumBtns = sizeof(buttons) / sizeof(TBBUTTON);
SendMessage(hToolbar, TB_ADDBUTTONS, uNumBtns, (LPARAM)buttons);
bandinfo.cbSize = sizeof(REBARBANDINFO);
bandinfo.cyMinChild = LOWORD(dwSize);
bandinfo.cx = HIWORD(dwSize);
bandinfo.fMask = RBBIM_CHILD | RBBIM_CHILDSIZE;
bandinfo.hwndChild = hToolbar;
SendMessage(hRebar, RB_INSERTBAND, 0, (LPARAM)&bandinfo);
}
// Windows procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateRebar(hwnd);
hEdit = CreateWindow(TEXT("EDIT"), TEXT("<Edit>..."),
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE,
0, 0, 0, 0,
hwnd, NULL, NULL, NULL);
SendMessage(hEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE);
return 0;
case WM_SIZE:
MoveWindow(hRebar, 0, 0, LOWORD(lParam), HIWORD(lParam), FALSE);
MoveWindow(hEdit, 0, HIWORD(dwSize) + 1, LOWORD(lParam), HIWORD(lParam) - HIWORD(dwSize) - 1, FALSE);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}