Et meget simpelt eksempel:
#include <windows.h>
#include <commctrl.h>
#include <algorithm>
#include <richedit.h>
enum WindowCtrlId
{
EditWindowId = 130
};
enum CommandEnum
{
AboutCmd = 1025,
ChooseFontCmd,
};
HINSTANCE InstanceHandle;
HWND MainWindow, EditWindow;
HFONT Font;
LOGFONT LogFont;
void OnChooseFont()
{
CHOOSEFONT ChooseFontStruct;
memset(&ChooseFontStruct, 0, sizeof(ChooseFontStruct));
ChooseFontStruct.lStructSize = sizeof(ChooseFontStruct);
ChooseFontStruct.hwndOwner = MainWindow;
ChooseFontStruct.lpLogFont = &LogFont;
ChooseFontStruct.rgbColors = RGB(0, 0, 0);
ChooseFontStruct.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_EFFECTS;
if(ChooseFont(&ChooseFontStruct))
{
DeleteObject(Font);
Font = CreateFontIndirect(&LogFont);
SendMessage(EditWindow, WM_SETFONT, (WPARAM)Font, TRUE);
}
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
MainWindow = hwnd;
EditWindow = CreateWindow(RICHEDIT_CLASS,
"",
WS_VISIBLE | WS_VSCROLL | WS_CHILD | ES_MULTILINE | WS_HSCROLL,
20, 20, 100, 100,
MainWindow,
(HMENU )EditWindowId,
InstanceHandle,
0);
SendMessage(EditWindow, WM_SETFONT, (WPARAM )Font, FALSE);
SetFocus(EditWindow);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case AboutCmd:
MessageBox(MainWindow, "TextEditor\\r\\nCopyright Bertel Brander 2006", "TextEditor", MB_OK);
break;
case ChooseFontCmd:
OnChooseFont();
break;
}
break;
case WM_SIZE:
RECT ClientRect;
GetClientRect(hwnd, &ClientRect);
MoveWindow(EditWindow, ClientRect.left, ClientRect.top, ClientRect.right - ClientRect.left, ClientRect.bottom - ClientRect.top, TRUE);
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 OptionsMenu = CreateMenu();
AppendMenu(OptionsMenu, MF_STRING, ChooseFontCmd, "Font");
HMENU HelpMenu = CreateMenu();
AppendMenu(HelpMenu, MF_STRING, AboutCmd, "About");
HMENU MainMenu = CreateMenu();
AppendMenu(MainMenu, MF_POPUP, (UINT )OptionsMenu, "Options");
AppendMenu(MainMenu, MF_POPUP, (UINT )HelpMenu, "Help");
return CreateWindow("SimpleWinWndClass",
"Simple Text Editor",
WS_MINIMIZEBOX | WS_VISIBLE | WS_MAXIMIZEBOX |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION |
WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
0,
MainMenu,
InstanceHandle,
0);
}
int WINAPI WinMain(HINSTANCE Instance,
HINSTANCE,
LPSTR,
INT)
{
InstanceHandle = Instance;
InitCommonControls();
HMODULE hMod = LoadLibrary("riched20.DLL");
if(!hMod)
{
MessageBox(0, "Failed to load dll", "TextApp", MB_OK);
}
if((MainWindow = CreateMainWindow()) == (HWND )0)
{
MessageBox(0, "Failed to create MainWindow!", "TextEditor", MB_OK);
return 0;
}
Font = (HFONT )GetStockObject(DEFAULT_GUI_FONT);
ShowWindow(MainWindow, SW_SHOW);
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}