|
-
Mar 21st, 2001, 09:15 PM
#1
Thread Starter
Lively Member
In Visual C++ 6, could someone send me some code to create a main window using the Win32 Application Project. I hate the MFC appwizard stuff. If you could write somecode that will run if i just paste it into a .cpp file with lots of detailed comments it would be much appreciated.....thanx.
-
Mar 22nd, 2001, 02:05 PM
#2
Frenzied Member
1. open visual c++
2. File > New
3. Click Projects tab
4. click Win32 Application and give it a name
5. click finish
6. click the File View Tab on TreeView
7. go to File > New
8. in the first tab Click C++ Source File and name it
9. Copy and paste the code in the new file
10. right click the Header folder and add Windows.h
11. click execute
Code:
#include <windows.h>
LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
char szWinNamw[] = "MyWin";
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
LPSTR kposzArgs, int nWinMode)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wcl;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.hInstance = hThisInst;
wcl.lpszClassName = "My Window";
wcl.lpfnWndProc = WindowFunc;
wcl.style = 0;
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
if(!RegisterClassEx(&wcl)) return 0;
hwnd = CreateWindow(
"My Window",
"Skeleton Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
hThisInst,
NULL
);
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
-
Mar 22nd, 2001, 03:17 PM
#3
Thread Starter
Lively Member
Thanks......It worked.....but i would really appreciate it if you could put a comment beside each line and tell me what it does.....Then i can understand how it works a bit better.
-
Mar 22nd, 2001, 04:10 PM
#4
Frenzied Member
Dont u dare tell me parksie about any mistakes :D
#include <windows.h> //Standard Windows Header
#include <stdio.h>
HWND g_hWnd_main; //Holds The Handle To The Application
HINSTANCE g_hInstance; //Instance Of The Application
char g_szAppName[] = "wndExample"; //Name Of The Application And Main Window Class
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE: //When The Apps Quitted
DestroyWindow(g_hWnd_main); //Destroy The Window
break;
case WM_DESTROY: //When The Windows Destroyed
PostQuitMessage(0); //Quit The Application
break;
case WM_SIZE: //When The Window Is Resized
break;
case WM_KEYDOWN: //When A Key Is Pressed
break;
case WM_KEYUP: //When A Key Is Released
break;
case WM_MOVE: //When A Window Is Moved
break;
case WM_COMMAND: //When A Button/Control Is Had Someting Done To It
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); //Pass The Messages To The Default Handler
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg; //Message Holder For The Application
WNDCLASSEX WndClassEx; //Window Class
g_hInstance = hInstance; //Pass The Instance To The Global Variable
WndClassEx.cbClsExtra = 0; //Extra Bytes In Class Definition
WndClassEx.cbSize = sizeof(WNDCLASSEX); //The Size Of The Class Data
WndClassEx.cbWndExtra = 0; //Extra Bytes For Each Window
WndClassEx.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); //Color For The Window
WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW); //Default System Arrow
WndClassEx.hIcon = NULL; //Main Application Icon
WndClassEx.hIconSm = NULL; //Small Application Icon
WndClassEx.hInstance = hInstance; //Pass The Instance To The Windows Class
WndClassEx.lpfnWndProc = (WNDPROC) WndProc; //The Message Handler Function
WndClassEx.lpszClassName = g_szAppName; //Class Name For The Window
WndClassEx.lpszMenuName = NULL; //Menu Bar Resource
WndClassEx.style = 0; //Extra Style Information
if (!RegisterClassEx(&WndClassEx)) //Register The Class
{
MessageBox(NULL, "Error - Failed To Register Class", "Error", MB_OK | MB_SYSTEMMODAL | MB_ICONEXCLAMATION);
return 0; //Quit The Function And Return FALSE
}
g_hWnd_main = CreateWindow( g_szAppName, "Window Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, hInstance, NULL); //Create The Window
if (!g_hWnd_main)
{
MessageBox(NULL, "Error - Failed To Create Window", "Error", MB_OK | MB_SYSTEMMODAL | MB_ICONEXCLAMATION);
return 0; //Quit The Function And Return FALSE
}
else
{
//Create Controls Here Or Under WM_CREATE Message
}
ShowWindow(g_hWnd_main, SW_SHOW); //Show The Window
UpdateWindow(g_hWnd_main); //Update The Window
SetFocus(g_hWnd_main); //Give The Window Focus
while (GetMessage(&Msg, NULL, 0, 0)) //While There Are Messages
{
TranslateMessage(&Msg); //Translate Them
DispatchMessage(&Msg); //Dispatch Them, Pass To The Message Handler Function
}
UnregisterClass(g_szAppName, hInstance); //Unregister The Class
return Msg.wParam; //Return The Code Passed To PostQuitMessage()
}
-
Mar 22nd, 2001, 04:44 PM
#5
Thread Starter
Lively Member
thanks very much.......i just needed to understand how it worked.....I've got another question now. How do I add controls and other items to the window?
- The more examples I see the more I understand and learn it.
-
Mar 22nd, 2001, 04:48 PM
#6
Frenzied Member
Use the CreateWindow API. TO learn how to do this and a lot of other stuff read this tutorial
http://www.winprog.org/tutorial
-
Mar 23rd, 2001, 01:59 PM
#7
Monday Morning Lunatic
Mistakes? I'm sure you're right so I didn't check.
One thing though -- there's a mistake in the tutorial. When it uses GetMessage, don't use their code as it won't handle failure properly, use this:
Code:
MSG msg;
int iTemp = 1;
for(;;) {
iTemp = GetMessage(&msg, NULL, 0, 0);
if(iTemp <= 0) break;
if(!IsDialogMessage(msg.hwnd, &msg)) {
TranslateMessage(&msg); // Translate keycode messages
DispatchMessage(&msg); // Send to the window
}
}
iTemp contains the failure code.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|