The code below will show a window, this is how we get started. Try it.
Here is an entire windows program. It will open a window titled
WinBreakoutC++. There is not much to it, lots of the words are larger
than you maybe accustom to. Setting up an IDE or Integrated Development
Environment can be more difficult. If your not set up with an IDE
and the Win32 libraries like those that come with the Digital Mars compiler
contact me at the above link.
If your using Visual Studio open a new project with the application
wizard, the result should be very similar to what is shown below.
My efforts here are probably redundant considering Microsoft's
Learn to Program for Windows in C++ which I highly recommend but you're not likely
to send them an email and get help. Although if you try hard they have forums
and other methods of helping users.
The following code will need to be linked to the static libraries listed below
after compiling to create the executable.
/**
* main.cpp
*******************/
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
/** Declare the Windows procedure **/
/** this is a prototype of a function like any other prototype **/
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/** Make the class name into a global variable **/
/** note the TCHAR wide character for Unicode **/
TCHAR szClassName[ ] = _T("Student Project");
/** this is "main" like any other main in C/C++ **/
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nCmdShow)
{
HWND hwnd; // this is the handle for our window
MSG messages; // messages to the program are stored in this structure
WNDCLASSEX wincl; // structure for the windowclass
/** The Window structure here is where windows are customized **/
wincl.hInstance = hThisInstance; // from argument in WinMain
wincl.lpszClassName = szClassName; // the class name we created
wincl.lpfnWndProc = WindowProcedure; // this function is called by windows
// note the prototype above
wincl.style = CS_DBLCLKS; // catch double-clicks - google.com
wincl.cbSize = sizeof(WNDCLASSEX); // common memory management stuff
/** Use default icon and mouse-pointer **/
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); // when we define one
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); // again
wincl.hCursor = LoadCursor (NULL, IDC_ARROW); // stock windows arrow
wincl.lpszMenuName = NULL; // for when we define if ever, none for now
wincl.cbClsExtra = 0; // no extra bytes after the window class
wincl.cbWndExtra = 0; // no extra bytes after structure or the window instance
/** add a custom background color(inside our window) defaults is an ugly gray **/
wincl.hbrBackground = CreateSolidBrush(RGB(255, 255, 255)); // white
/** Register the window class, and if it fails quit the program **/
if(!RegisterClassEx (&wincl)) // catches mistake from above
return 0;
/** The class is registered, let's create the program window **/
hwnd = CreateWindowEx ( // more options
0, // extended possibilities for variation
szClassName, // Classname
_T(" WinBreakoutC++ "), // Title Text
WS_OVERLAPPEDWINDOW, // default window
CW_USEDEFAULT, // Windows decides the position
CW_USEDEFAULT, // where the window ends up on the screen
330, // The programs width
500, // and height in pixels
HWND_DESKTOP, // The window is a child-window to desktop
NULL, // No menu
hThisInstance, // Program Instance handler
NULL // No Window Creation data
);
/** Make the window visible on the screen **/
ShowWindow (hwnd, nCmdShow);
/** Run the MESSAGE LOOP it will run until GetMessage() returns 0 **/
/** the heart beat of a windows program leave it alone **/
while (GetMessage (&messages, NULL, 0, 0))
{
/** Translate virtual-key messages into character messages **/
TranslateMessage(&messages); // again google.com
/** Send message to WindowProcedure **/
DispatchMessage(&messages);
}
/** The program return-value is 0 - The value that PostQuitMessage() gave **/
return messages.wParam; // you are correct the MSG structure element wParam
// is zero
}
/** This function is called by the MESSAGE LOOP function DispatchMessage() above **/
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps; // define a structure variable object
HDC hdc; // define a device context handle
switch (message) // handle the messages
{
case WM_DESTROY: // our window has been closed
// put any future clean up code here
PostQuitMessage (0); // send a WM_QUIT to the message queue
break;
case WM_PAINT: // window contents need painting or repainting
hdc = BeginPaint (hwnd, &ps); // get device context for our window
/* output text to the window */
TextOut (hdc, // the handle to the device context
80, // start position from left of window
40, // start position from top of window
"this is the main window...", // the test to display
23); // number of character to display
EndPaint (hwnd, &ps); // release or free the hdc
break;
default: // for messages not trapped above
/* default window procedure to provide default processing for */
/* any window messages that we do not process */
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* any break reached in the above switch statement arrive here */
return 0;
}
download the above file main.cpp