Archive

Posts Tagged ‘visualc++’

C++ Programming: Visual Windows

April 24th, 2009 admin No comments

I am primarily a programmer for *nix platforms and some cross compatibility for the DOS command prompt as many of you know. However, I also program Windows in the visual sense (how most end-users envision their product). So I’m going to write up a quick tutorial for you guys today!

I am compiling the example with Microsoft’s Visual C++. I am not normally an advocate for most things made by Microsoft, but in this case there is no doubt that this is the best compiler for compiling Windows programs. The best part about it is (unexpected, I know; it is Microsoft after all) that the compiler is FREE and comes with a very nice and clean GUI.

Now be mindful, the example has small functionality, but it is more or less the basis to ALL Windows programs! As we go through, we’ll explain the importance of each part. So let’s begin with the main file so you can see what’s going on.

main.cpp

/**
* Windows Programming Tutorial by Dennis M.
*
* main.cpp
*
*/

// Basic includes :D
#include <windows.h> // Win32 Lib
#include “menu.h” // Our menu – we’ll explain this later ;)
// more commonly named “resource.h,” but for
// explanation purposes this is easier :)

// Main WindowProcedure Definition
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
const char szClassName[] = “MicrosonicTestProgram”; // Our program’s name

int WINAPI
// Our main class to make the Window
WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArg,int WindowStyle)
{
// Pointer definitons
HWND hwnd;
MSG msgs;
WNDCLASSEX wcl;

// Standard window properties
wcl.hInstance = hThisInstance;
wcl.lpszClassName = szClassName;
wcl.lpfnWndProc = WindowProcedure;
wcl.style = CS_DBLCLKS;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcl.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wcl.hCursor = LoadCursor(NULL,IDC_ARROW);
wcl.lpszMenuName = “MAIN_MENU”;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

if(!RegisterClassEx(&wcl)){
return 0;
}

hwnd = CreateWindowEx(
0,
szClassName,
“Microsonic.org Test Program”,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
640,
480,
HWND_DESKTOP,
LoadMenu(hThisInstance,”MAIN_MENU”),
hThisInstance,
NULL
);

// This is where we see the window.. ShowWindow()… duh!
ShowWindow(hwnd,WindowStyle);
while(GetMessage(&msgs,NULL,0,0)){
TranslateMessage(&msgs);
DispatchMessage(&msgs);
}
return msgs.wParam;
}

// Here we process all the commands etc.
LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
// Process commands sent to the program
// In this case, from the menu items
case WM_COMMAND:
switch(wParam){
case MM_FILE_NEW:
MessageBox(hwnd,”MDI is more complex! Another tutorial soon perhaps on it! This is just the basics to Windows programming!\r\nSorry!”,
“Feature Not Available”,MB_OK);
return 0;
break;
case MM_FILE_EXIT:
PostQuitMessage(0);
return 0;
break;
case MM_HELP_ABOUT:
MessageBox(hwnd,”\tMicrosonic.org test program!\r\n\r\n\tFor more tutorials visit:\r\n\r\n\thttp://microsonic.org! \r\n”,
“Microsonic.org Test Program”,MB_OK);
return 0;
break;
case MM_HELP_VISIT:
ShellExecute(hwnd,”open”,”http://microsonic.org”,NULL,NULL,0);
return 0;
break;
}
break;
// If they hit the “X” button
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
// Default loop
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
break;
}
return 0;
}

Some basic points I’d like to touch upon. Since this is just the basics to the Window, I have not included MDI (allowing text, etc. through the new button) but, this is important for the generalization of the program. If you understand how this works, Windows programming becomes very similar to all other sorts of programming with the assistance of the MSDN Library. Now, realize that the structure file for the menu is never directly included into any C++ file, but rather automatically linked. Make sure you keep it this way and don’t make the mistake of including a .rc file. Also, as I peer through the code, it is important to realize that many of the function names are static. For the basics, all Windows programs should look as some sort of variation to this code simply by definition of a Windows program.

Now, let’s continue to see the header file. This purely holds definitions for the resource file holding the menu structure. If they are not defined, they will not work properly.

menu.h

/**
* Windows Programming Tutorial by Dennis M.
*
* menu.h
*
* Contains all our menu definitions
*
*/

// Give values to menu items
#define MM_FILE_NEW 2001
#define MM_FILE_EXIT 2002
#define MM_HELP_ABOUT 3001
#define MM_HELP_VISIT 3002

As you can see, each menu item is defined. These numbers are more or less arbitrary, you just want to make sure their definitions don’t conflict with each other or any other aspect of your program. I personally like to keep menu items grouped together, so I defined the “File” items in the 2000‘s and the “Help” items in the 3000‘s.

Finally, the structure of our menu. A resource file that is never really included.

menu.rc

/**
* Windows Programming Tutorial by Dennis M.
*
* menu.rc
*
* Resource file which contains the menu structure!
* To edit the file with Free Visual C++
* Right click and “View Code”
* Again, more commonly named “resource.rc”
*
*/
// Menu definitions
#include “menu.h”

// Should be able to get the hang of the menu ;)
MAIN_MENU MENU
BEGIN
POPUP “&File”,
BEGIN
// Format: MENUITEM “Title”, COMMAND_TO_SEND
MENUITEM “N&ew”, MM_FILE_NEW
MENUITEM SEPARATOR
MENUITEM “E&xit”, MM_FILE_EXIT
END
POPUP “&Help”
BEGIN
MENUITEM “A&bout”, MM_HELP_ABOUT
MENUITEM SEPARATOR
MENUITEM “G&oto Microsonic.org”, MM_HELP_VISIT
END
END

The structure is fairly straight forward, so I can probably leave it without much explanation. The only thing I would like to say is that you should realize how usable menu items are presented.
MENUITEM “Display Text”, DEFINED_FUNCTION.
The defined function is what is sent to the main loop of the program and is processed in the “switch.” Just remember this when you’re creating your programs!

As usual, I will provide complete source WITH the VC++9 Project file!

If you choose to make your own project, please make sure you go to the “General” tab and create an empty project. Compiler options in other Visual C++ presets do not work properly for many reasons. A major error one could run into (if using another setting) is compiling UNICODE which would break “const char szClassName[]” by definiton. You would then have to declare it as _T string.

Simple Win32 API Tutorial

Regards,
Dennis M.