Here is a small windows program you can take a look at for an example of form and structure...
It's a unicode editor and it has all the essentials of a full blown windows program. Notice how easily it's written using API calls. You can even compile it and try it out...
/*
Tiny Unicode Editor Example
*/
// for the compiler
#define UNICODE
#define _UNICODE
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0502
#define _X86_
// Windows headers
#include <windows.h>
#include <commdlg.h>
// PellesC headers
#include <stdlib.h>
#include <wchar.h>
#include <tchar.h>
#define BOM_FLAG 0xFFFE
// Window handles
HWND Wind[5];
HINSTANCE Inst;
TCHAR FileName[MAX_PATH]; // filename when opened
BOOL RevBytes = 0;
// save a file
void SaveToFile(void)
{ OPENFILENAME ofn; // filename struct
HANDLE fh; // handle for opening files
DWORD fs; // size of the data
PTCHAR fd; // pointer to data
// get the filename
memset(&ofn,0,sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Wind[0];
ofn.hInstance = Inst;
ofn.lpstrFilter = _T("All Files\0*.*\0\0");
ofn.lpstrFile = FileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = L"Save your work";
ofn.Flags = OFN_NONETWORKBUTTON |
OFN_HIDEREADONLY |
OFN_NOTESTFILECREATE |
OFN_OVERWRITEPROMPT;
if (!GetSaveFileName(&ofn))
return;
// get unicode adjusted file size from edit control
fs = (SendMessage(Wind[4],WM_GETTEXTLENGTH,0,0) * sizeof(TCHAR));
if (fs < 1)
return;
// create text buffer
fd = malloc(fs);
// get the text from the control
SendMessage(Wind[4],WM_GETTEXT,fs,(LPARAM)fd);
// open the file
fh = CreateFile(FileName,GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_WRITE_THROUGH,NULL);
// save the file
if (fh != INVALID_HANDLE_VALUE)
{ WriteFile(fh,fd,fs,&fs,NULL);
CloseHandle(fh); }
free(fd); }
// open a file
void OpenFromFile(void)
{ OPENFILENAME ofn; // filename struct
HANDLE fh; // handle for opening files
DWORD fs; // size of the data
PTCHAR fd; // pointer to data
// get the filename
memset(&ofn,0,sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Wind[0];
ofn.hInstance = Inst;
ofn.lpstrFilter = L"All Files\0*.*\0\0";
ofn.lpstrFile = FileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = L"Open a file";
ofn.Flags = OFN_NONETWORKBUTTON |
OFN_HIDEREADONLY |
OFN_NOTESTFILECREATE |
OFN_OVERWRITEPROMPT;
if (!GetOpenFileName(&ofn))
return;
// open the file
fh = CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if (fh == INVALID_HANDLE_VALUE)
{ MessageBox(Wind[0],L"Error opening the file",L"OOPS!",0);
return; }
// get the file size
fs = GetFileSize(fh,NULL) + sizeof(TCHAR);
// create text buffer
fd = malloc(fs);
// clear to 0
memset(fd,0,fs);
// read from disk
ReadFile(fh,fd,fs,&fs,NULL);
// close the file
CloseHandle(fh);
// put the text in the control
SendMessage(Wind[4],WM_SETTEXT,fs,(LPARAM)fd);
free(fd); }
// resize the window
VOID ResizeWindow(LPARAM lParm)
{ MoveWindow(Wind[4],60,2,LOWORD(lParm) - 62,HIWORD(lParm) - 4,1); }
// Message Loop
LRESULT CALLBACK MsgProc(HWND wnd,UINT msg,WPARAM wparm,LPARAM lparm)
{ switch (msg)
{ case WM_COMMAND :
switch (LOWORD(wparm))
{ case 1001 :
OpenFromFile();
return 0;
case 1002 :
SaveToFile();
return 0;
case 1003 :
PostMessage(Wind[0],WM_CLOSE,0,0);
return 0;
default :
return DefWindowProc(wnd,msg,wparm,lparm); }
case WM_SIZE :
ResizeWindow(lparm);
return 0;
case WM_CLOSE : // close window
DestroyWindow(Wind[0]);
return 0;
case WM_DESTROY : // NC Exit button
PostQuitMessage(0);
return 0;
default :
return DefWindowProc(wnd,msg,wparm,lparm); } }
// create the window
VOID CreateMainWindow(void)
{ WNDCLASS wc;
// register App Class
memset(&wc,0,sizeof(wc));
wc.style = CS_CLASSDC;
wc.hInstance = Inst;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
wc.lpfnWndProc = &MsgProc;
wc.lpszClassName = L"TINY_UNICODE";
RegisterClass(&wc);
// create the main window
Wind[0] = CreateWindowEx( WS_EX_CONTROLPARENT,
L"TINY_UNICODE",L"Tiny Unicode Editor",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,500,300,NULL,NULL,Inst,NULL);
// buttons
Wind[1] = CreateWindow(L"BUTTON",L"Open",
WS_CHILD | WS_VISIBLE | BS_FLAT,
2,2,50,25,Wind[0],(HMENU) 1001,Inst,NULL);
Wind[2] = CreateWindow(L"BUTTON",L"Save",
WS_CHILD | WS_VISIBLE | BS_FLAT,
2,30,50,25,Wind[0],(HMENU) 1002,Inst,NULL);
Wind[3] = CreateWindow(L"BUTTON",L"Quit",
WS_CHILD | WS_VISIBLE | BS_FLAT,
2,60,50,25,Wind[0],(HMENU) 1003,Inst,NULL);
// edit window
Wind[4] = CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",NULL,
WS_CHILD | WS_VISIBLE |
ES_MULTILINE,
60,2,200,200,Wind[0],NULL,Inst,NULL);
UpdateWindow(Wind[0]);
ShowWindow(Wind[0],SW_SHOWNORMAL); }
// Program Entry Procedure
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE pinst, LPSTR cmdl, int show)
{ MSG wmsg;
// save instance handle
Inst = hinst;
// make the window
CreateMainWindow();
// dispatch window messages
while (GetMessage(&wmsg,NULL,0,0))
{ TranslateMessage(&wmsg);
DispatchMessage(&wmsg); }
return 0; }