JDT
09-16-2002, 12:21 AM
---------------------Creating windows with VC++ ----------
Skill level
If you can follow directions then somebody who
has never heard of VC++ can do this if they
know how to start VC++ :)
To do this is simple but to understand it I would
think an intermediate skill level is needed.
Part one readme
This is the minimum code needed to create
windows with VC++. All the fat (well almost)
has been cut out so you can see what is actually
needed to make a window. There are things
left out that really should be there but I left
it out so you could focus on the least amount of code.
If you want to see what was left out then follow
the instructions below to step 8. In step 8, select
A Typical Hello World Application, click ok and skip
to step 17. This will show you how MS did it. It's
very confusing if you have not studied it before.
I have built a very extensive class of Controls that take
just a line or two to create and use. They mimic VB's events,
methods, and properties. I will use that starting at part 3
of this tutorial. With a couple exceptions and the lack
of draging and dropping controls on a form, it is just
as easy to use as VB but with the power of C++. This tutorial
will be based on my methods of class building (starting part 3,)
if they are not the same as yours, you get what you pay for ;)
1. Start VC++
2. Go to the file menu and select new
3. In the projects tab select Win32 Application
4. In the Project name box enter "Tutorial" (do not enter the quotes)
5. Choose a location to save the work to. I chose
C:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\VC_TUTORIAL
6. Leave Win32 checked in the platforms section.
7. Now click OK
8. Select "An Empty Project and click finish
9. Now click OK to the next window.
10. Now go back to the file menu and select New again.
11. In the Files tab select C++ Source File
12. Name it "Tutorial" (do not add quotes)
13. Check the Add to Project Box
14. The Location should default to where the project was saved in the previous steps.
15. Click OK
16. Now paste this code in the file you just created (It should be shown now and have the focus).
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
#include <windows.h> //<--Include windows specific functions for the project
//Prototype of Window Process function
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Heres our main function. The program starts here
//**********************************************************************
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;//<---------Handle to the window
MSG msg;//<-----------Used to get messages from the thread's message queue
WNDCLASSEX wcex; //---Used to register window class information
wcex.cbSize = sizeof(WNDCLASSEX); //<-----Needs to know the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW;//<-Window will redraw if resized
wcex.lpfnWndProc = (WNDPROC)WndProc;//<--------Messages will be sent to this procedure
wcex.cbClsExtra = 0;//<-----------------------No extra bytes following class structure
wcex.cbWndExtra = 0;//<-----------------------No extra bytes following instance
wcex.hInstance = hInstance;//<---------------Handle to the instance that contains the window procedure.
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);//<---Default application icon.
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);//<------------Standard arrow cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW); //<----------------Gray back color for window
wcex.lpszMenuName = NULL;//<-----------------------------------No Menu for this window
wcex.lpszClassName = "TutorialClass";//<-------Name of the class to be registered
wcex.hIconSm = NULL;//-----------------------Find an appropriate small icon for us
//Class must be registered with the system before we can create the window
RegisterClassEx(&wcex);
//--------------------Lets create a window-----------
//"TutorialClass" - the name of the class we Registered
//"Tutorial" - This will be the name of the window displayed in the title bar
//WS_OVERLAPPEDWINDOW is a window with a title bar and a border
//CW_USEDEFAULT - Use the default horizontal position for the window
//CW_USEDEFAULT - Use the default vertical position for the window
//CW_USEDEFAULT - Use the default width for the window
//CW_USEDEFAULT - Use the default height for the window
//NULL - This window has no parent
//NULL - This window has no menu
//hInstance - The application instance
//NULL - Add no extra data for the WM_CREATE message
hWnd = CreateWindow("TutorialClass", "Tutorial", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInstance, NULL);
//Lets show the window we just created maximized
ShowWindow(hWnd, SW_MAXIMIZE);
//This is the message pump that retrieve all
//message from the calling thread's message queue
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);//Dispatches the message to our WndProc
}
return msg.wParam; //return exit code to the OS
}
//******************************************************************
//This is where all the messages will be processed
//******************************************************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)//<----------Lets find out what message was sent
{
case WM_DESTROY://<-------User wants to quit so lets process this message
PostQuitMessage(0);//This will send GetMessage (in WinMain above) a zero and
//will exit that loop and quit our program
break;
default:
//Send unprocessed messages to the default windows procedure
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//*******************************************************************
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
17. Now go to the menu bar and select Build
18. Select Execute Tutorial.exe
19. Select yes to the message box asking you if you want to build files.
20. Your done, thats the very basics to creating a window in VC++
In part two I will add a command button to interact with the window and display
a message box.
From there I will Wrap the code into a class object. The final
part of this tutorial should have code for a fully wrapped Window (form) and a
Command button. With default events, default wndprocs that map to user defined events,
built in subclassing and several of the popular methods and properties that
go with these types of windows. For those of you new to VC++, you'll be
surprised at how simple GUI programming can be once you build your own
reusable objects. Its not quite as simple as VB, but its not too far off.
BTW - I use VS6 SP5 on win2k and winME
Skill level
If you can follow directions then somebody who
has never heard of VC++ can do this if they
know how to start VC++ :)
To do this is simple but to understand it I would
think an intermediate skill level is needed.
Part one readme
This is the minimum code needed to create
windows with VC++. All the fat (well almost)
has been cut out so you can see what is actually
needed to make a window. There are things
left out that really should be there but I left
it out so you could focus on the least amount of code.
If you want to see what was left out then follow
the instructions below to step 8. In step 8, select
A Typical Hello World Application, click ok and skip
to step 17. This will show you how MS did it. It's
very confusing if you have not studied it before.
I have built a very extensive class of Controls that take
just a line or two to create and use. They mimic VB's events,
methods, and properties. I will use that starting at part 3
of this tutorial. With a couple exceptions and the lack
of draging and dropping controls on a form, it is just
as easy to use as VB but with the power of C++. This tutorial
will be based on my methods of class building (starting part 3,)
if they are not the same as yours, you get what you pay for ;)
1. Start VC++
2. Go to the file menu and select new
3. In the projects tab select Win32 Application
4. In the Project name box enter "Tutorial" (do not enter the quotes)
5. Choose a location to save the work to. I chose
C:\DOCUMENTS AND SETTINGS\ADMINISTRATOR\DESKTOP\VC_TUTORIAL
6. Leave Win32 checked in the platforms section.
7. Now click OK
8. Select "An Empty Project and click finish
9. Now click OK to the next window.
10. Now go back to the file menu and select New again.
11. In the Files tab select C++ Source File
12. Name it "Tutorial" (do not add quotes)
13. Check the Add to Project Box
14. The Location should default to where the project was saved in the previous steps.
15. Click OK
16. Now paste this code in the file you just created (It should be shown now and have the focus).
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
#include <windows.h> //<--Include windows specific functions for the project
//Prototype of Window Process function
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//Heres our main function. The program starts here
//**********************************************************************
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;//<---------Handle to the window
MSG msg;//<-----------Used to get messages from the thread's message queue
WNDCLASSEX wcex; //---Used to register window class information
wcex.cbSize = sizeof(WNDCLASSEX); //<-----Needs to know the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW;//<-Window will redraw if resized
wcex.lpfnWndProc = (WNDPROC)WndProc;//<--------Messages will be sent to this procedure
wcex.cbClsExtra = 0;//<-----------------------No extra bytes following class structure
wcex.cbWndExtra = 0;//<-----------------------No extra bytes following instance
wcex.hInstance = hInstance;//<---------------Handle to the instance that contains the window procedure.
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);//<---Default application icon.
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);//<------------Standard arrow cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW); //<----------------Gray back color for window
wcex.lpszMenuName = NULL;//<-----------------------------------No Menu for this window
wcex.lpszClassName = "TutorialClass";//<-------Name of the class to be registered
wcex.hIconSm = NULL;//-----------------------Find an appropriate small icon for us
//Class must be registered with the system before we can create the window
RegisterClassEx(&wcex);
//--------------------Lets create a window-----------
//"TutorialClass" - the name of the class we Registered
//"Tutorial" - This will be the name of the window displayed in the title bar
//WS_OVERLAPPEDWINDOW is a window with a title bar and a border
//CW_USEDEFAULT - Use the default horizontal position for the window
//CW_USEDEFAULT - Use the default vertical position for the window
//CW_USEDEFAULT - Use the default width for the window
//CW_USEDEFAULT - Use the default height for the window
//NULL - This window has no parent
//NULL - This window has no menu
//hInstance - The application instance
//NULL - Add no extra data for the WM_CREATE message
hWnd = CreateWindow("TutorialClass", "Tutorial", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hInstance, NULL);
//Lets show the window we just created maximized
ShowWindow(hWnd, SW_MAXIMIZE);
//This is the message pump that retrieve all
//message from the calling thread's message queue
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);//Dispatches the message to our WndProc
}
return msg.wParam; //return exit code to the OS
}
//******************************************************************
//This is where all the messages will be processed
//******************************************************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)//<----------Lets find out what message was sent
{
case WM_DESTROY://<-------User wants to quit so lets process this message
PostQuitMessage(0);//This will send GetMessage (in WinMain above) a zero and
//will exit that loop and quit our program
break;
default:
//Send unprocessed messages to the default windows procedure
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//*******************************************************************
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
17. Now go to the menu bar and select Build
18. Select Execute Tutorial.exe
19. Select yes to the message box asking you if you want to build files.
20. Your done, thats the very basics to creating a window in VC++
In part two I will add a command button to interact with the window and display
a message box.
From there I will Wrap the code into a class object. The final
part of this tutorial should have code for a fully wrapped Window (form) and a
Command button. With default events, default wndprocs that map to user defined events,
built in subclassing and several of the popular methods and properties that
go with these types of windows. For those of you new to VC++, you'll be
surprised at how simple GUI programming can be once you build your own
reusable objects. Its not quite as simple as VB, but its not too far off.
BTW - I use VS6 SP5 on win2k and winME