main
function as its starting point, every Windows desktop application must have a WinMain
function. WinMain
has the following syntax.CALLBACK
, or HINSTANCE
, or _In_
? The traditional Windows API uses typedefs and preprocessor macros extensively to abstract away some of the details of types and platform-specific code, such as calling conventions, __declspec declarations, and compiler pragmas. In Visual Studio, you can use the IntelliSense Quick Info feature to see what these typedefs and macros define. Hover your mouse over the word of interest, or select it and press Ctrl+K, Ctrl+I for a small pop-up window that contains the definition. For more information, see Using IntelliSense. Parameters and return types often use SAL Annotations to help you catch programming errors. For more information, see Using SAL Annotations to Reduce C/C++ Code Defects.TCHAR
macro, which resolves ultimately to wchar_t if the UNICODE symbol is defined in your project, otherwise it resolves to char. If you always build with UNICODE enabled, you don't need TCHAR and can just use wchar_t directly.WinMain
function, every Windows desktop application must also have a window-procedure function. This function is typically named WndProc
, but you can name it whatever you like. WndProc
has the following syntax.WndProc
function that does whatever work is appropriate. It's called handling an event. You only handle the events that are relevant for your application.WinMain
function, you populate a structure of type WNDCLASSEX. The structure contains information about the window: the application icon, the background color of the window, the name to display in the title bar, among other things. Importantly, it contains a function pointer to your window procedure. The following example shows a typical WNDCLASSEX
structure.WNDCLASSEX
with Windows so that it knows about your window and how to send messages to it. Use the RegisterClassEx function and pass the window class structure as an argument. The _T
macro is used because we use the TCHAR
type.HWND
, which is a handle to a window. A handle is somewhat like a pointer that Windows uses to keep track of open windows. For more information, see Windows Data Types.WndProc
function. In other words, the application isn't yet handling the messages that Windows is now sending to it.WndProc
function to be handled. The message loop resembles the following code.WinMain
function should resemble the following code.WndProc
function to handle the messages that the application receives, implement a switch statement.WM_PAINT
message when part of its displayed window must be updated. The event can occur when a user moves a window in front of your window, then moves it away again. Your application doesn't know when these events occur. Only Windows knows, so it notifies your app with a WM_PAINT
message. When the window is first displayed, all of it must be updated.WM_PAINT
message, first call BeginPaint, then handle all the logic to lay out the text, buttons, and other controls in the window, and then call EndPaint. For the application, the logic between the beginning call and the ending call is to display the string 'Hello, Windows desktop!' in the window. In the following code, notice that the TextOut function is used to display the string.HDC
in the code is a handle to a device context, which is a data structure that Windows uses to enable your application to communicate with the graphics subsystem. The BeginPaint
and EndPaint
functions make your application behave like a good citizen and doesn't use the device context for longer than it needs to. The functions help make the graphics subsystem is available for use by other applications.WndProc
function.