Please help me so i can stop banging my head against the wall.....

Started by
12 comments, last by TheNerd Tk421 20 years, 8 months ago
I have been going thru the resource tutorial on this site Here... I dont know if it is this tutorial or me.. or what... cuase ne thing i do with this tutorial DONT WORK!!!!.... My resource file here:

#include "resource.h"

ICON_MAIN     ICON    myicon.ico
CURSOR_ARROW  CURSOR  arrow.cur


my header file here:      
#define ICON_MAIN        1000
#define CURSOR_ARROW     2000
My load icon/cursor statements
  
HICON LoadIcon(
    HINSTANCE hInstance,  // handle to application instance

    LPCTSTR lpIconName    // icon-name string or icon resource identifier

);

HCURSOR LoadCursor(
    HINSTANCE hInstance,   // handle to application instance

    LPCTSTR lpCursorName   // name string or cursor resource identifier

);
my main file(non edited ):
   #define WIN32_LEAN_AND_MEAN
#include <windows.h>
const char className[] = "Sample Class";
const char windowName[] = "Sample Window";

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(	HINSTANCE hinstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
               {
WNDCLASSEX sampleClass;
                MSG msg;	HWND hwnd;
               	sampleClass.cbSize        = sizeof(WNDCLASSEX);
               	sampleClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
               	sampleClass.lpfnWndProc   = MsgHandler;
                sampleClass.cbClsExtra    = 0;
                sampleClass.cbWndExtra    = 0;
                sampleClass.hInstance     = hinstance;
                sampleClass.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
                sampleClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
                sampleClass.hbrBackground = (HBRUSH)(GRAY_BRUSH);
                sampleClass.lpszMenuName  = NULL;
                sampleClass.lpszClassName = className;
                sampleClass.hIconSm       = LoadIcon(NULL, IDI_WINLOGO);
                  if(!RegisterClassEx(&sampleClass))
                  {
                  MessageBox(NULL, "Could not register window class.", "ERROR", MB_OK);
                  		return 0;
                    	}
 hwnd = CreateWindowEx(	0,
	            className,
               windowName,
    WS_POPUP | WS_VISIBLE,
           0, 0, 800, 600,
	                 NULL,
                     NULL,
                hinstance,
                     NULL);
 if(hwnd == NULL)
 {
 MessageBox(NULL, "Could not create window.", "ERROR", MB_OK);
 return 0;
 }
 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);
	while(1)
 {
	if(PeekMessage(&msg,
                   NULL,
                   0,
                   0,
 PM_REMOVE))
 {
 TranslateMessage(&msg);
	DispatchMessage(&msg);
}
else if(msg.message == WM_QUIT)
{
break;
}
}
UnregisterClass(className, hinstance);
return msg.wParam;
}
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
{
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
return 0;}
WTF is my problem... where do i put sum of this stuff.. cuase this dont tell me much(no i am not just a noob that copies and pastes code i read through it).... when i add all of it together:i get this:
 #define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "rsrc.rc"
const char className[] = "new class";
const char windowName[] = "new Window";

LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(	HINSTANCE hinstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
               {
WNDCLASSEX sampleClass;
                MSG msg;	HWND hwnd;
               	sampleClass.cbSize        = sizeof(WNDCLASSEX);
               	sampleClass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
sampleClass.lpfnWndProc   = MsgHandler;
sampleClass.cbClsExtra    = 0;
sampleClass.cbWndExtra    = 0;
sampleClass.hInstance     = hinstance;
sampleClass.hIcon = LoadIcon(hinstance, "ICON_MAIN");      
sampleClass.hCursor = LoadIcon(hinstance, "CUSOR_ARROW");
sampleClass.hbrBackground = (HBRUSH)(GRAY_BRUSH);
sampleClass.lpszMenuName  = NULL;
sampleClass.lpszClassName = className;
sampleClass.hIcon = LoadIcon(hinstance, "ICON_MAIN");

HICON LoadIcon(
HINSTANCE hInstance, // handle to application instance

LPCTSTR lpIconName // icon-name string or icon resource identifier

);

HCURSOR LoadCursor(
HINSTANCE hInstance, // handle to application instance

LPCTSTR lpCursorName // name string or cursor resource identifier

);

                  if(!RegisterClassEx(&sampleClass))
                  {
                  MessageBox(NULL, "Could not register window class.", "ERROR", MB_OK);
                  		return 0;
                    	}
 hwnd = CreateWindowEx(	0,
	            className,
               windowName,
    WS_POPUP | WS_VISIBLE,
           0, 0, 800, 600,
	                 NULL,
                     NULL,
                hinstance,
                     NULL);
 if(hwnd == NULL)
 {
 MessageBox(NULL, "Could not create window.", "ERROR", MB_OK);
 return 0;
 }
 ShowWindow(hwnd, nCmdShow);
 UpdateWindow(hwnd);
	while(1)
 {
	if(PeekMessage(&msg,
                   NULL,
                   0,
                   0,
 PM_REMOVE))
 {
 TranslateMessage(&msg);
	DispatchMessage(&msg);
}
else if(msg.message == WM_QUIT)
{
break;
}
}
UnregisterClass(className, hinstance);
return msg.wParam;
}
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch(msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
{
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
return 0;
}
[edited by - TheNerd Tk421 on August 13, 2003 4:35:19 PM]
Advertisement
"It don't work" is pretty vague. What errors are you getting? I assume you are using Visual Studio. Do you have your project set up correctly for a Win32 app? More info please.

Edit: Another thing, you don't need to make prototypes for LoadIcon and LoadCursor. Those functions are provided for you with the Win32 API and are already defined in windows.h

[edited by - CodeMunkie on August 13, 2003 4:29:29 PM]
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
3 new.cpp rsrc.rc:1: parse error before `500''
22 new.cpp `className'' undeclared (first use this function)
22 new.cpp (Each undeclared identifier is reported only once
22 new.cpp for each function it appears in.)
these are my errors
and my linker erros are:
g++: new.o: No such file or directory
g++: file path prefix `C:\DEV-C_~1\Bin\'' never used
.................
i am using Dev-C++ 4.0


I dont understand it... what ever i do dealing with C++... it doesnt work... i can bearly make VERY basic C++ console apps!...
i have been trying to learn for about a year now...

[edited by - TheNerd Tk421 on August 13, 2003 4:48:57 PM]
don''t #include a .rc file. that is a separate type of file which must be compiled using the resource compiler. if you''re using an IDE which supports projects, you should just have to add the .rc file to the project.

replace the #include "rsrc.rc" in your main.cpp with #include "resource.h"

the places where you have LoadIcon(hinstance, "ICON_MAIN". you need to replace the quoted string with the #define constant equivalent that was set up in the resource.h file. e.g., replace "ICON_MAIN" with ICON_MAIN and replace "CURSOR_ARROW" with CURSOR_ARROW.

as previously stated, get rid of those HICON LoadIcon( ... ) statements that are in the main.cpp. those are not executable statements but function prototypes and those prototypes have already been defined via the windows include headers.
Perhaps you shouldnt be starting with the windows api then, afterall there is very little c++ in the winapi.
-keyboard
oops, missed one. get rid of that HCURSOR LoadCursor( statement as well.
It works... i take the #include "rsrc.rc" off and it work....but then i think the tutorial said that...thanx
To reiterate much of what has already been said:

1) the Windows API is not C++

2) the problem you''re having is related to C concepts, not C++

3) the "load icon/cursor statements" are actually function declarations - these are already Windows API functions, you should not be declaring these functions.

4) you cannot declare functions in the middle of a function

Regards,
Jeff
I just looked over that tutorial and I guess I can see how it would be a little bit hard to follow (kinda wordy). Here is a guide straight from MSDN that I think is very clear:

Clickage


[edited by - CodeMunkie on August 13, 2003 5:01:00 PM]
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

This topic is closed to new replies.

Advertisement