Win32 problem

Started by
3 comments, last by Si0n 19 years, 3 months ago
I just started Win32 and I've run into a snag: Whenever do a tutorials (gamtutorials), For some reason my compiler gives me linker errors on every function in a header. For example:


#include <windows.h>									
#include <stdio.h>
#include <ctype.h>										
RECT windowRect = {100, 100, 400, 400};	
															
															
															
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);		
															
															
															
															
															
void printText(char, HWND);									
															
													//Excuse the sloppiness, this is for demonstration		
															
															
															
															
															
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)			
    {														
    HWND        hwnd;										
    MSG         msg;										
    WNDCLASSEX  wndclass;									//is it a good idea to memorize these variables?
    wndclass.cbSize        = sizeof (wndclass);				
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;		
    wndclass.lpfnWndProc   = WndProc;						
    wndclass.cbClsExtra    = 0;								
    wndclass.cbWndExtra    = 0;								
    wndclass.hInstance     = hInstance;						
    wndclass.hIcon         = LoadIcon (NULL, IDI_WINLOGO);	
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);	
															
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;							
    wndclass.lpszClassName = "Window Class 1";				
    wndclass.hIconSm       = LoadIcon (NULL, IDI_WINLOGO);	

	RegisterClassEx (&wndclass);					
															
													
													
    hwnd = CreateWindow ("Window Class 1",			
						 "My First Window",		  	
						 WS_OVERLAPPEDWINDOW,		
						 windowRect.left,	
						 windowRect.top,	
						 windowRect.right,	
						 windowRect.bottom,	 
						 NULL,								
						 NULL,								
						 hInstance,						    
						 NULL);								

    ShowWindow (hwnd, iCmdShow);							
    UpdateWindow (hwnd);									
															
	while (GetMessage (&msg, NULL, 0, 0))					
    {														
		TranslateMessage (&msg);							
		DispatchMessage (&msg);								
    }
    
	return msg.wParam ;										
}
														
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{															
	static TEXTMETRIC textInfo;								
	static int length=0;									
	PAINTSTRUCT paintStruct;								
	HDC  hdc=NULL;											
	int x, y;												
	char szHello[]="Hello World!";	
    char color;						

    switch (iMsg)											
    {														
		case WM_CREATE:										
			
			hdc = GetWindowDC(hwnd);						
															
			GetTextMetrics(hdc, &textInfo);					
			length = strlen(szHello);						
			ReleaseDC(hwnd, hdc);							
			break;											

		case WM_SIZE:										
			GetWindowRect(hwnd, &windowRect);				
															
			break;											
        case WM_CHAR:
             color = wParam;
            
            printText(color, hwnd);
            break;
		case WM_PAINT:										
		hdc = BeginPaint(hwnd, &paintStruct);			
			EndPaint(hwnd, &paintStruct);					
		    break;											

		case WM_DESTROY:									
			PostQuitMessage(0);								
															
			break;											
	}													

	return DefWindowProc (hwnd, iMsg, wParam, lParam);		
}															


void printText(char color, HWND hwnd)
{
    color = toupper(color);
    HDC hdc = GetDC(hwnd);
    
    if (color == 'R')
    {
       SetTextColor(hdc,RGB(255,0,0));
       TextOut(hdc, (windowRect.right/2), (windowRect.bottom/2), "Hello", 5);
   }
   if (color == 'G')
    {
       SetTextColor(hdc,RGB(0,255,0));
       TextOut(hdc, (windowRect.right/2), (windowRect.bottom/2), "Hello",  5);
   }
   if (color == 'B')
    {
       SetTextColor(hdc,RGB(0,0,255));
       TextOut(hdc, (windowRect.right/2), (windowRect.bottom/2), "Hello",  5);
   }
   ReleaseDC(hwnd, hdc);    
    return;
}    
 
I get problems ranging form undefined referance to GetStockObject to an undefined referance to SetTextColor. I'm running Dev-C++, in case that helps. Thanks, ~Dakar
Advertisement
Make sure you're linking to the appropriate windows libs. The Win32 docs for each function will tell you which lib it's in.
Where would I find these docs?
Okay, I figured it out after a quick search on MSDN. Apparently, I needed to include a certain lib file.

Thank you.
ITcoudl also be that you werent creating a Window project, but apparently you already solved the problem. ;)

ps - You only need to include <windows.h> for win32 fucntions...

This topic is closed to new replies.

Advertisement