Windows API Tutorial: Create a Simple Window [C++]

Create a new empty project Add a cpp file. I’ll name mine “main”. Declare the “Window Procedure” function. This function handles the messages sent to your window Every window has its own “Window Procedure” An application is receiving thousands of messages while it runs, either from the user or the operating system Consider that every keystroke or mouse click generates a message A message is simply a numeric code. For example, if you press the left mouse button The window receives the following message code: 0x0201 (WM_LBUTTONDOWN) “hwnd” is a handle to the window “uMsg” is the message code. For example: the WM_LBUTTONDOWN message “wParam” and “lParam” contain additional data that pertains to the message If you’re not new to C++ then you know that every program must have a “main” function Same here: Every Windows program must have an entry-point function that is named “wWinMain”(or “WinMain”) hInstance is something called a “handle to an instance” or “handle to a module.” The operating system uses this value to identify the executable (EXE) when it is loaded in memory The second HINSTANCE is useless. It was used in 16-bit Windows, but is now always zero “nCmdLine” contains the command-line arguments as a Unicode string “nCmdShow” is a flag that says whether the main application window will be minimized, maximized, or shown normally Now we’ll register a window class. Every window must be associated with a window class “lpfnWndProc” is a pointer to the Window Procedure (WindowProc) “lpszClassName” is a string that identifies the window class “hInstance” is the handle to the application instance This function registers the window class with the operating system To create a new instance of a window, call the CreateWindowEx function The first parameter is for optional window styles (ex: transparent windows). Set it to 0 for default behaviours CLASS_NAME is the name of the window class. This defines the type of window you are creating Window Text. If the window has a title bar, the text is displayed in the title bar WS_OVERLAPPEDWINDOW give the window a title bar, a border, a system menu, and Minimize and Maximize buttons For position and size, the constant CW_USEDEFAULT means to use default values The next parameter sets a parent window or owner window for the new window This is not a child window (it’s a top-level window) so set this to NULL The second parameter (NULL) defines the menu for the window.My window does not use a menu, so the value is NULL The last parameter is a pointer to arbitrary data of type void*.You can use this value to pass a data structure to your window procedure To show the window, pass the window handle to the ShowWindow function Set nCmdShow to 1 to start the window in the normal state. Let’s go back to the messages. Let’s say your program has several windows,each with its own window procedure How does the program receive all of these messages and deliver them to the right window procedure? With a loop that gets the messages (using GetMessage function) and distpatches them to the correct windows The TranslateMessage function is related to keyboard input; it translates keystrokes (key down, key up) into characters The DispatchMessage function tells the operating system to call the window procedure of the window that is the target of the message When the window procedure returns, it returns back to DispatchMessage, which returns to the message loop for the next message Now let’s write the window procedure function A typical window procedure is simply a large switch statement that switches on the message code Add cases for each message that you want to handle Let’s handle the WM_DESTROY Message (Sent when a window is being destroyed) The “PostQuitMessage” function puts a WM_QUIT message This causes “GetMessage” to return zero, signaling the end of the message loop
and the end of the program “return 0” indicates that you handled the message When a window is about to be painted/repainted, the operating system sends a WM_PAINT message This is the best place to type your painting code Check the description for more information! Now I’ll handle the WM_CLOSE message (this is optional) The WM_CLOSE message gives you an opportunity to prompt the user before closing the window I’ll use the MessageBox function wich will show a modal dialog with 2 buttons: OK and CANCEL If you don’t handle a particular message in your window procedure, Pass the message parameters directly to the DefWindowProc function This function performs the default action for the message, which varies by message type Thanks for watching! SUBSCRIBE for more WINAPI tutorials! Music by Kevin MacLeod

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


Leave a Reply

Your email address will not be published. Required fields are marked *