Below a small template (88 lines) for coding a GUI application with Pelles C. It has a menu and an edit control, and you can read a text file into that control.
Windows GUI programming is neither easy nor complicated. You simply have to study the essential stuff. The old Petzold book is a good starter, and MSDN is your friend.
#include <windows.h> // simple GUI template
#include <stdio.h> // needed for fopen
#pragma comment(linker, "gdi32.lib comdlg32.lib comctl32.lib") // needed for GetOpenFileName, GetStockObject and InitCommonControlsEx
#pragma warn(disable:2216 2118) // retval never used, parameter .. is not referenced
#define winX 44 // main window x, y, width and height
#define winY 44
#define winW 888
#define winH 666
void ReadFile2EditControl(char* filename, HWND hEdit) { // does what its name says
FILE *ofp = fopen(filename, "rb");
if (ofp) {
fseek(ofp, 0, SEEK_END); // GetFileSize() won't work, so let's do it the archaic C way
int fLen=ftell(ofp); // position at end equals length of file
fseek(ofp, 0, SEEK_SET); // go back to start
PSTR pstrBuffer=malloc(fLen+1); // buffer includes a zero delimiter
if (pstrBuffer) {
fread(pstrBuffer, 1, fLen, ofp); // read content into buffer
pstrBuffer[fLen] = '\0'; // set the delimiter explicitly
SetWindowText(hEdit, pstrBuffer) ; // put text into the edit control
free(pstrBuffer);
}
fclose(ofp);
}
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
{
#define mOpen 101
#define mSave 102
HMENU hMenu=CreateMenu(); // create the main menu
HMENU hSubMenu=CreatePopupMenu(); // create a sub-menu
AppendMenu(hMenu, MF_POPUP, (int)hSubMenu, "&File"); // add it to the main menu
AppendMenu(hSubMenu, MF_STRING, mOpen, "&Open"); // and add
AppendMenu(hSubMenu, MF_STRING, mSave, "&Save"); // two items
SetMenu(hwnd, hMenu); // attach menu to main window
HWND hEdit=CreateWindowEx(WS_EX_CLIENTEDGE, "edit", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE,
2, 1, winW-12, winH-GetSystemMetrics(SM_CYMENU)-33, hwnd, (HMENU) 103, GetModuleHandle(0), NULL); // we have added an edit control
SendMessage(hEdit, EM_LIMITTEXT, 0, 0); // no limit
SetFocus(hEdit); // make sure you can start typing right away
SendMessage(hEdit, WM_SETFONT, (int)GetStockObject(ANSI_FIXED_FONT), 0); // SYSTEM_FIXED_FONT is bold and larger
}
break;
case WM_COMMAND:
if ((short)wParam==mOpen) {
char fileName[MAX_PATH]="\x0000";
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter ="Sources and Headers\0*.c;*.h;*.asm\0\0";
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if (GetOpenFileName(&ofn)) ReadFile2EditControl(fileName, GetDlgItem(hwnd, 103));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
MSG msg; WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszClassName = "MyClass";
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
wc.lpszMenuName = NULL;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, "Hello World",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | nCmdShow,
winX, winY, winW, winH,
NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}