10 Lessons About C++ You Need To Learn to Become a Master Programmer
Andrew Webb
A BERRY PUBLISHING HOUSE BOOK First published in the UK by Berry Publishing House 7 Kew Lodge 227 Kew Road Richmond Surrey TW9 3LQ Copyright Andrew Webb 2015 - Cover Getty Images (Rights Purchased) All rights reserved. No part of this publication may be reproduced in any material form (including printing or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright owner, except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency, 90 Tottenham Court Road, London W1P 9HE. Applications for the copyright owners written permission to reproduce any part of this publication should be addressed to the publisher.
Your Free Gift
As a way of saying thank you for your purchase, Im offering an exclusive free copy of all the coding scripts in full colour and indented that are included within this book. This enables readers to copy, paste and alter the coding to suit their own needs and also make the exact applications contained in the book fully functional without spending hours debugging the syntax, as I know how annoying and time consuming this is.
Chapter 1: Windows Programing
1.1 Getting Started: A simple window with event handling
The problem with teaching Windows programming -- especially to those wishing to become master programmers -- is that it is a vast subject, one that deserves a book or several books in its own right.
Chapter 1: Windows Programing
1.1 Getting Started: A simple window with event handling
The problem with teaching Windows programming -- especially to those wishing to become master programmers -- is that it is a vast subject, one that deserves a book or several books in its own right.
In order to give an introduction I have been very selective, focusing on a few important basics with the intention that the reader can use these to build his or her own understanding. The gist of Windows programming is this: the user initiates an event such as pressing a key or clicking a mouse, the Windows operating system recognizes and intercepts this event, preprocesses it, and dispatches it to the appropriate user program. When the user event is received by the program, it can decide on how it will act upon it, or otherwise leave it alone and let Windows handle it via its default processing. There are a few things needed to get even the simplest of Windows applications up and running. Programmers who wish to create even a minimalist application will need to use the data types, classes, constants, functions and structures contained in the Microsoft Win32 base library. I have always preferred to try things out and learn about them in more depth later, so I will do the same here.
Let's start with a bare bones Windows application with explanations to follow. Every Windows program has an entry-point function that is named either WinMain or wWinMain : int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
- hInstance - a handle to a module the Windows operating system uses to identify the executable (.exe) when loaded into memory. This handle instance is required for certain Windows operations such as loading icons and bitmaps.
- hPrevInstance - is relevant to 16-bit Windows applications but is no longer used. Now it is always set to zero.
- nCmdShow - a flag indicating whether the main application window is to be minimized, maximized or displayed normally.
When creating a basic window programmatically the four minimum steps necessary are as follows: 1. When creating a basic window programmatically the four minimum steps necessary are as follows: 1.
Create the window class used to store information about things such as the type of Window being used, the Windows Procedure function used to control the window, icons, colours and other parameters. This is held in the WNDCLASSEX structure. WNDCLASSEX windowClass;windowClass.cbSize = sizeof( WNDCLASSEX );windowClass.style = CS_HREDRAW | CS_VREDRAW;windowClass.lpfnWndProc = WndProc;windowClass.cbClsExtra = 0;windowClass.cbWndExtra = 0;windowClass.hInstance = hInstance;windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );windowClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );windowClass.lpszMenuName = NULL;windowClass.lpszClassName = "MyClass";windowClass.hIconSm = LoadIcon( NULL, IDI_WINLOGO ); 2. Register the Windows class. Whenever a Windows program starts, it registers the windows class with the system, and gives the system a pointer to a callback function called the windows procedure. This windows procedure is called each time Windows wants to pass an event message back to the program.
You need do this only once before using it as many times as necessary. The window attributes defined in step 1 can be changed at any time if desired. Use RegisterClassEx to do the registering. RegisterClassEx(&windowClass); 3. Create the window object using the CreateWindowEx function (See MSDN documentation for an explanation of CreateWindowEx and its parameters: https://msdn.microsoft.com/en-us/library/ms908193.aspx) CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"The title of my window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,240,120,NULL,NULL,hInstance,NULL); See MSDN documentation for an explanation of CreateWindowEx and its parameters: https://msdn.microsoft.com/en-us/library/ms908193.aspx 4. Enter the main message loop, which is where all event messages will get sent for processing.
A bare minimum example is shown below: LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){switch(msg){case WM_CLOSE:DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, msg, wParam, lParam);}return 0;} A mouse click, for example, gets dispatched to the program that created the window over which the mouse was positioned and clicked. There are literally dozens of possible messages that will get handled by Windows every time you resize, minimize, maximize, press a key, and so forth. Then the program responsible for that window will get a chance to intercept and process the message for that event in whatever way the programmer chooses. This example shows you not only how to create a basic window programmatically, but also add the capability to respond to mouse event notifications. Hardly earth-shattering, but it does help you to get the hang of handling messages. When the program is first run we are presented with the simple window: The current mouse cursor position is tracked every time it moves, so that when the left mouse button has been clicked, a yellow circle is painted at the location of the mouse click: The full code listing is as follows: #include#include#define WIN32_LEAN_AND_MEANconst unsigned radius = 3;int xPos = 0;