Nå, jeg fik endelig fundet lidt tid til at få strikket noget sammen:
#include <windows.h>
#include <commctrl.h>
HINSTANCE InstanceHandle;
HWND MainWindow;
enum MenuCommand
{
FileOpenCmd = 1000,
FileSaveCmd,
FileCloseCmd,
AboutCmd
};
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case FileOpenCmd:
MessageBox(hwnd, "Open", "Test", MB_OK);
break;
case FileSaveCmd:
MessageBox(hwnd, "Save", "Test", MB_OK);
break;
case FileCloseCmd:
MessageBox(hwnd, "Close", "Test", MB_OK);
break;
case AboutCmd:
MessageBox(hwnd, "About", "Test", MB_OK);
break;
}
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
HWND CreateMainWindow()
{
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC )MainWndProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_WINDOW + 1);
wc.lpszClassName = "SimpleWinWndClass";
wc.lpszMenuName = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClass(&wc))
return 0;
HMENU FileMenu = CreateMenu();
AppendMenu(FileMenu, MF_STRING, FileOpenCmd, "Open");
AppendMenu(FileMenu, MF_STRING, FileSaveCmd, "Save");
AppendMenu(FileMenu, MF_SEPARATOR, 0 , 0);
AppendMenu(FileMenu, MF_STRING, FileCloseCmd, "Close");
HMENU HelpMenu = CreateMenu();
AppendMenu(HelpMenu, MF_STRING, AboutCmd, "About");
HMENU MainMenu = CreateMenu();
AppendMenu(MainMenu, MF_POPUP, (UINT )FileMenu, "File");
AppendMenu(MainMenu, MF_POPUP, (UINT )HelpMenu, "Help");
MENUINFO MenuInfo;
memset(&MenuInfo, 0, sizeof(MenuInfo));
MenuInfo.cbSize = sizeof(MenuInfo);
MenuInfo.fMask = MIM_BACKGROUND;
MenuInfo.hbrBack = CreateSolidBrush(RGB(128, 128, 255));
SetMenuInfo(MainMenu, &MenuInfo);
MenuInfo.hbrBack = CreateSolidBrush(RGB(255, 128, 128));
SetMenuInfo(FileMenu, &MenuInfo);
MenuInfo.hbrBack = CreateSolidBrush(RGB(128, 255, 126));
SetMenuInfo(HelpMenu, &MenuInfo);
return CreateWindow("SimpleWinWndClass",
"Simple-Window",
WS_MINIMIZEBOX | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX |
WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
0,
MainMenu,
InstanceHandle,
0);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow)
{
InstanceHandle = hInstance;
InitCommonControls();
if((MainWindow = CreateMainWindow()) == (HWND )0)
{
MessageBox(0, "Failed to create MainWindow!", "Warning", MB_OK);
return 0;
}
ShowWindow(MainWindow, SW_SHOW);
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Det er disse linjer der bestemmer farven:
MENUINFO MenuInfo;
memset(&MenuInfo, 0, sizeof(MenuInfo));
MenuInfo.cbSize = sizeof(MenuInfo);
MenuInfo.fMask = MIM_BACKGROUND;
MenuInfo.hbrBack = CreateSolidBrush(RGB(128, 128, 255));
SetMenuInfo(MainMenu, &MenuInfo);
Man specificerer hvilken brush der skal bruges til at tegen menuens baggrund så der er grænser for hvad man kan lave.
Man kan godt lave mønstre, f.eks. skraveret, men man kan ikke en gradient.