HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Nov 12, 2007 8:31 pm Post subject: My First Tut, Basic Win32 c++ |
|
|
Win32 Starting guide.
I decided to make this guide for all the c++ users that can't make gui's using win32 but are trying to learn how.
This is the code to create a window, i will describe it more.
| Code: | #include <windows.h>
//prototypes
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//global variables
static char sClassName[] = "MyClass";
static HINSTANCE zhInstance = NULL;
//Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow) {
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
zhInstance = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = zhInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = sClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass)) {
MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_STATICEDGE, sClassName, "Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT,
200, 200, NULL, NULL, zhInstance, NULL);
if(hwnd == NULL) {
MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
|
You may look at this and get scared, but don't, it's fairly straightforward. Also, all you have to do in other projects to create a window is copy paste this and edit it as you please.
This is the include file needed for all win32 programs. It contains all the functions.
| Code: | //prototypes
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//global variables
static char sClassName[] = "MyClass";
static HINSTANCE zhInstance = NULL; |
This is simply the declaration of variables on functions. WndProc is the function that contains the messages of a Win32 program.
| Code: | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow) {
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
zhInstance = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = NULL;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = zhInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = sClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass)) {
MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_STATICEDGE, sClassName, "Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT,
500, 500, NULL, NULL, zhInstance, NULL);
if(hwnd == NULL) {
MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
|
This is a big section, but don't be scared. WinMain is the Entry point of a Win32 program. Its just like int main or void main in console apps. WndClass is a WNDCLASSX type structure and contains the info to make a window.
cbSize holds the size of the struct WNDCLASSEX
style stores the class style and can be usually set to 0.
lpfnWndProc is a long pointer to the Class used to make the window, set to WndClass.
cbClasExtra is the extra memory that can be allocated to each class used to create the window, usually set to 0.
cbWndExtra is the extra memory that can be allocated to each Window, usually set to 0.
hInstance is a handle to the instance of the application.
hIcon is the windows 32x32 icon. Don't worry about it for now.
hCursor is the windows cursor. Don't worry about it for now.
hbrBackground is the windows bg color. You can set it to any color. Leave it for now.
lpszMenuName is a long pointer to the menu of the window, don't worry for now.
lpszClassName is the class name of the window. sClassName is a char of MyClass.
hIconSm is the pointer to the icon to be displayed in the top left. Leave it for now.
We then attempt to register the class and if it fails, we report a messagebox.
We use CreateWindowEx to create the window using all the information. I will link to the function on msdn at the bottom.
| Code: |
while(GetMessage(&Msg, NULL, 0, 0)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
|
Messages are at the heart of a Windows App. This handles messages when they are being sent. You must have this or nothing will happen in your program. This is like the brain of your program.
| Code: |
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
|
This is the function that tells the program what to do when a specific message is encountered. For now its just WM_CLOSE and WM_DESTROY and the default. These 2 cases destory stuff and deallocates memory. If you want to destroy an object do it in WM_DESTROY.
But now your asking i have a window, but its empty. so lets add some windows(controls).
You will add windows in the WndProc of a window like this.
define the id of a control at the top of your program with
| Code: |
#define hEdit 1
#define hButton 2
|
now
| Code: |
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND Edit1, Button1;
switch (message) /* handle the messages */
{
case WM_CREATE:
Edit1 = CreateWindow("edit", "Edit",
WS_VISIBLE | WS_CHILD | WS_BORDER,
0, 0, 130, 30, hwnd, (HMENU) hEdit, NULL, NULL);
Button1 = CreateWindow("button", "button",
WS_VISIBLE | WS_CHILD | WS_BORDER,
0, 40, 100, 30, hwnd, (HMENU)hButton, NULL, NULL);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
|
You declared variables with type HWND. They are windows so you need to use HWND.
Then you use WM_CREATE case and in this you use CreateWindow to declare the 2 new windows.
CreateWindowEx:
http://msdn2.microsoft.com/en-us/library/ms632680.aspx _________________
Last edited by HomerSexual on Tue Nov 13, 2007 5:44 am; edited 2 times in total |
|