Code:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <merlin.h>
#include "Global.h"
#include "Main.h"
#include "Menu.h"
#include "Turingres.h"
HINSTANCE ghInstance = NULL;
HWND hwndMain = NULL;
LPSTR gClassName = "TurningMachineClass";
HWND hEditLog = NULL;
HWND hEditInput = NULL;
HWND hButtonTalk = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
MSG Msg;
ghInstance = hInstance;
if(RegisterWindow()==FAILURE){
return 1;
}
if(CreateMainWindow(nCmdShow)==FAILURE){
return 1;
}
while(GetMessage(&Msg, NULL, 0, 0) > 0){
if(Msg.message==WM_KEYDOWN&&LOWORD(Msg.wParam)==VK_RETURN){
SendMessage(hButtonTalk, BM_CLICK, 0, 0);
continue;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UnregisterClass(gClassName, ghInstance);
return Msg.wParam;
}
int static RegisterWindow(void){
InitCommonControls();
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = WndMainProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghInstance;
wc.hIcon = LoadIcon(NULL, IDI_QUESTION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 11);
wc.lpszMenuName = NULL;
wc.lpszClassName = gClassName;
wc.hIconSm = NULL;
if(RegisterClassEx(&wc)==0){
ERRBOX(ERR_TITLE, ERR_REG);
return FAILURE;
}
return SUCCESS;
}
int static CreateMainWindow(int nCmdShow){
hwndMain = CreateWindowEx(WS_EX_CLIENTEDGE,
gClassName, WND_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WND_WIDTH, WND_HEIGHT,
NULL, NULL, ghInstance, NULL);
if(hwndMain == NULL){
ERRBOX(ERR_TITLE, ERR_WND);
return FAILURE;
}
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
return SUCCESS;
}
LRESULT CALLBACK WndMainProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg)
{
case WM_CREATE:
CreateControls(hwnd);
CreateMenus(hwnd);
break;
case WM_CLOSE:
CloseProgram(hwnd);
break;
case WM_SIZE:
ResizeWindow(hwnd);
break;
case WM_SETFOCUS:
SetFocus(hEditInput);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case BN_CLICKED:
if((HWND)lParam==hButtonTalk){
TalkPressed(hwnd);
}
break;
case MENU_FILE_EXIT:
CloseProgram(hwnd);
break;
case MENU_HELP_VERSION:
DisplayVersion();
break;
case MENU_HELP_HELP:
case MENU_HELP_ABOUT:
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void static CreateControls(HWND hwnd){
HFONT hFont = CreateFont(FONT_HEIGHT, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FONT_NAME);
hEditLog = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_LEFT | ES_READONLY | ES_WANTRETURN,
0, 0, WND_WIDTH-10, WND_HEIGHT-100, hwnd, NULL, ghInstance, NULL);
hEditInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_LEFT,
0, WND_HEIGHT-90, WND_WIDTH-50, 25, hwnd, NULL, ghInstance, NULL);
hButtonTalk = CreateWindowEx(WS_EX_CONTROLPARENT, "BUTTON", "Talk",
WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
WND_WIDTH-30, WND_HEIGHT-90, 50, 25, hwnd, NULL, ghInstance, NULL);
if(hEditLog==NULL||hEditInput==NULL||hFont==NULL){
ERRBOX(ERR_TITLE, ERR_CHILD_WND);
CloseProgram(hwnd);
}
SendMessage(hEditLog, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
SendMessage(hEditInput, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
}
void static CreateMenus(HWND hwnd){
HMENU hMenu;
HMENU hSubMenu;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
AppendMenu(hSubMenu, MF_STRING, MENU_FILE_EXIT, "E&xit");
hSubMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Help");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_HELP, "&Contents");
AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_VERSION, "&Version");
AppendMenu(hSubMenu, MF_STRING, MENU_HELP_ABOUT, "&About");
SetMenu(hwnd, hMenu);
}
void static DisplayVersion(void){
char Buffer[1024];
sprintf(Buffer, "%s\nCompiled on %s at %s",MSG_VERSION, __DATE__, __TIME__);
MSGBOX("Information", Buffer);
}
void static ResizeWindow(HWND hwnd){
RECT Wnd;
RECT EditLog;
RECT EditInput;
RECT Button;
int WndHeight = 0;
int WndLegnth = 0;
int ButtonLegnth = 0;
int ButtonHeight = 0;
GetWindowRect(hwnd, &Wnd);
GetWindowRect(hEditLog, &EditLog);
GetWindowRect(hEditInput, &EditInput);
GetWindowRect(hButtonTalk, &Button);
WndHeight = Wnd.bottom-Wnd.top;
WndLegnth = Wnd.right-Wnd.left;
ButtonHeight = Button.bottom-Button.top;
ButtonLegnth = Button.right-Button.left;
SetWindowPos(hEditLog, NULL, 0, 0, WndLegnth-10, WndHeight-85, SWP_NOZORDER);
SetWindowPos(hEditInput, NULL, 0, WndHeight-80, WndLegnth-(ButtonLegnth+20), 25, SWP_NOZORDER);
SetWindowPos(hButtonTalk, NULL, (WndLegnth-ButtonLegnth-15), WndHeight-80, ButtonLegnth, 25, SWP_NOZORDER);
}
void static TalkPressed(HWND hwnd){
char Buffer[1024];
GetWindowText(hEditInput, Buffer, 1024);
sprintf(Buffer, "%s\r\n", Buffer);
SendMessage(hEditLog, EM_REPLACESEL, FALSE, (long int)Buffer);
SetWindowText(hEditInput, "");
SetFocus(hEditInput);
}
void CloseProgram(HWND hwnd){
DestroyWindow(hwnd);
}
Here are the two header files. I just lumped them together.