Code:
//Windows.h is a header file you will need for virtually all Windows
//programs. Be sure to always include it
#include<windows.h>
//These defines are used later to identify the child windows
#define CWE_OUTTEXT 1
#define CWE_INTEXT 2
//Prototype for the function that will recieve all messages for the
//windows
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
//Many windows API's want to know the instance of the running
//application, for convenince I'm saving it in a global variable
HINSTANCE Instance;
//Windows does not start with the standard main(), instead it
//uses WinMain() with this prototype, just like main you don't
//need to prototype it:
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
{
MSG msg;
WNDCLASS wcl;
HWND Handle;
char Caption[512];
char ClassName[512];
//Setting some temporary variables to hold the class and
//caption names for the window (more on these later)
Caption[0] = '\0';
ClassName[0] = '\0';
strcat(Caption, "Windows Application Shell");
strcat(ClassName, "WinAppShell");
//Copying the instance value (its passd as a parameter to
//WinMain()
Instance = hThisInst;
//All windows are assigned to a particular windows class
//The class holds many values that describe the window
//such as the background color (wcl.hbrBackground),
//the icon (wcl.hIcon) and the look of the mouse cursor
//(wcl.hCursor)
wcl.hInstance = hThisInst;
wcl.lpszClassName = ClassName;
wcl.lpfnWndProc = WindowFunc;
wcl.style = 0;
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
//Attempt to register the window class for use in creating
//the window
if(RegisterClass(&wcl) == 0)
{
return 0;
}
//If the class was successfully registered, we can create
//the main window.
Handle = CreateWindow(ClassName, Caption, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 160, 155, HWND_DESKTOP, NULL, hThisInst, NULL);
//The main window needs to be initialized and drawn on the
//screen, these function calls take care of that
ShowWindow(Handle, nWinMode);
UpdateWindow(Handle);
//So that the window can recieve messages (messages are how
//all user input and other things are reported to the window)
//we need to establish a message loop.
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//When the message loop ends, the window has closed and the
//program can end
return msg.wParam;
}
//The class defines this function as being the recipient of all
//of the window's messages (wcl.lpfnWndProc). All of the message
//details are passed as parameters to this function
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char String[512];
//The message variable holds the type of message passed
//Its common to put it in a switch statement to handle all
//of the events passed
switch(message)
{
case (WM_CREATE) :
{//The WM_CREATE message is passed when the window
//is created, but not yet placed on the screen
//At this point we are going to create all of the
//child windows. You can see that the defines
//made above are passed as arguments, that is to
//assign those integers as how we would like to
//identify the specific child windows
CreateWindow("edit", "", WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT, 0, 0, 150, 50, hwnd, (HMENU)CWE_INTEXT, Instance, NULL);
CreateWindow("edit", "", WS_CHILD | WS_BORDER | WS_VISIBLE | ES_LEFT | ES_READONLY, 0, 75, 150, 50, hwnd, (HMENU)CWE_OUTTEXT, Instance, NULL);
break;
}
case (WM_COMMAND) :
{//The WM_COMMAND message is a message from one of the child
//windows wParam has information on which child and what it
//wants to tell the main window
if (LOWORD(wParam) == CWE_INTEXT)
{//wParam holds two values, we can split the valuesout with
//the LOWORD() and HIWORD() macros. The LOWORD() value has
//the ID of the child that sent the message
if (HIWORD(wParam) == EN_CHANGE)
{//The HIWORD() has the type of message that was sent
SendDlgItemMessage(hwnd, CWE_INTEXT, WM_GETTEXT, 512, (LPARAM)String);
SetDlgItemText(hwnd, CWE_OUTTEXT, String);
}
}
break;
}
case (WM_DESTROY) :
{//The WM_DESTROY message is sent when the window is being
//destroyed by windows
//PostQuitMessage() sends a signal to the message loop that
//the window is closing and the loop can close to allow
//the application to quit
PostQuitMessage(0);
break;
}
default :
{//Windows sends many many types of events, any that we don't
//choose to process can be handled by a default routine
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
return 0;
}
Here's some background information, since Windows programs are far more complicated than command line programs. Generally how the WinMain() function works is to establish any top level windows that will exist by creating the window classes needed and then the windows themselves. Then the WinMain() function enters a message loop where it gets message from the operating system with GetMessage() and hands them off to the windows callback function with DispatchMessage(). Thats its main job, it can handle other tasks but they are probably beyond the beginner stage so nevermind them for now.