-
November 22nd, 2008, 12:11 AM
#1
My First Win32 API Program! Non-Sense Errors!
I have VC++ Express 2008
my main program
Code:
#include<windows.h>
// LESSON TWO
// specify a tag name of our main window (called window class name) , it will be registered in WNDCLASSEX.
const char mainWindowClassName[] = "MainWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UNIT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam,lParam);
}
// Typically if you handle a message in the WndProc, you return zero.
return 0;
}
// Start program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Windows class structure is used to register our window class name
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
// Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX); // the size of the structure.
wc.style = CS_HREDRAW | CS_VREDRAW; // look at GameTutorial for explaination.
wc.lpfnWndProc = WndProc; // assign pointer to the window procedure for this window class.
wc.cbClsExtra = 0; // amount of extra data allocated for this class in memory. Usually 0.
wc.cbWndExtra = 0; // amount of extra data allocated in memory per window of this type. Usually 0.
wc.hInstance = hInstance; // assign our hInstance to our window.
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // large (usually 32x32) icon shown when the user presses Alt+Tab.
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor that will be displayed over our window.
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // background Brush to set the color of our window.
wc.lpszMenuName = NULL; // name of a menu resource to use for the windows with this class.
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.
if(!RegisterClassEX(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
exit(-1);
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, mainWindowClassName, "The title of my window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
exit(-1);
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
UnregisterClass(mainWindowClassName, hInstance);
return Msg.wParam;
}
These are the errors:
------ Rebuild All started: Project: test, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'test', configuration 'Debug|Win32'
Compiling...
test.cpp
c:\documents and settings\fahmi\desktop\test\test\test.cpp(6) : error C2061: syntax error : identifier 'UNIT'
c:\documents and settings\fahmi\desktop\test\test\test.cpp(8) : error C2065: 'msg' : undeclared identifier
c:\documents and settings\fahmi\desktop\test\test\test.cpp(8) : error C2050: switch expression not integral
c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'msg' : undeclared identifier
c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'wParam' : undeclared identifier
c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'lParam' : undeclared identifier
c:\documents and settings\fahmi\desktop\test\test\test.cpp(35) : error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND)' to 'WNDPROC'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
c:\documents and settings\fahmi\desktop\test\test\test.cpp(45) : error C3861: 'RegisterClassEX': identifier not found
c:\documents and settings\fahmi\desktop\test\test\test.cpp(47) : error C3861: '_T': identifier not found
Build log was saved at "file://c:\Documents and Settings\Fahmi\Desktop\test\test\Debug\BuildLog.htm"
test - 9 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Note: i set character to Multi-Byte in config. box
Can you help please?!
-
November 22nd, 2008, 12:15 AM
#2
Re: My First Win32 API Program! Non-Sense Errors!
What is a "UNIT"? The Windows constant name is UINT, not UNIT.
Regards,
Paul McKenzie
-
November 22nd, 2008, 01:15 AM
#3
Re: My First Win32 API Program! Non-Sense Errors!
Thanks, i wasn't aware about it....!
Compiles, but the program breaks - unexpected error occurs while debugging, i copied this code from a tutorial.
Anyway, i will double check it.
-
November 22nd, 2008, 05:28 AM
#4
Re: My First Win32 API Program! Non-Sense Errors!
Originally Posted by f.ben.isaac
i copied this code from a tutorial.
It seems to be a tutorial with some (typing) errors.
Beside that Paul already discovered, change also RegisterClassEX with RegisterClassEx (in C/C++ identifiers are case-sensitive), then add this line before call it
Code:
wc.lpszClassName = mainWindowClassName;
[ Redirected thread ]
Last edited by ovidiucucu; November 22nd, 2008 at 05:36 AM.
-
November 22nd, 2008, 12:12 PM
#5
Re: My First Win32 API Program! Non-Sense Errors!
-
November 22nd, 2008, 07:22 PM
#6
Re: My First Win32 API Program! Non-Sense Errors!
two suggestions:
1) Dev-C++ IDE has a "template" win32 project that is a simple-but-complete program that compiles.
2) I'm not sure what tutorial you are using but when i was learning, i found this one to be the best: http://www.winprog.org/tutorial/
also, Charles Petzold's "Programming Windows" is an invaluable book, and also the online MSDN library is an invaluable resource for windows programming.
regards.
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
|