Thread: new to winapi

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    new to winapi

    I am trying to learn how to program windows and I am having trouble compiling the attached code. The code if from chapter 10 of "Windows Programming with C++ by Henning Hansen". I am using VC++ 6 and i get the following errors.

    --------------------Configuration: 10_threads - Win32 Debug--------------------
    Compiling...
    10_threads.cpp
    D:\programming\tmp\10_threads\10_threads.cpp(62) : error C2065: 'hWindow' : undeclared identifier
    D:\programming\tmp\10_threads\10_threads.cpp(63) : error C2065: 'border' : undeclared identifier
    Error executing cl.exe.

    10_threads.exe - 2 error(s), 0 warning(s)

    Am I missing something in my typing of it or is there a problem with the code in the book.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: new to winapi

    The first one's easy...hWindow is defined in WinMain...and so is not useable in ThreadProc unless you declare it globally outside all functions (there is another way....but this is the simplest method)

    Next, in ThreadProc you have a function "border()" which you use in the Rectangle() function...this isnt defined, and as you are passing no paramaters the return will probably always be the same...and as you are defining 4 sides of a rect, that's not too helpful!

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    6
    ok. I've changed the code. "hWindow" is now global and I have removed "border()%" because there is no border() in the sorce code int the book. The app now compiles just fine but the program dosn't draw any rectagles as the book says it will. Am I correct in assuming that was what the border() function was for but it just wasn't included in the book?

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Ok...here's a few quick ideas (based on your original code)

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);
    DWORD WINAPI ThreadProc(LPVOID);
    
    volatile HWND hWindow;
    
    int APIENTRY WinMain(HINSTANCE hInstance,
    	HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    	WNDCLASS WndClass;
    	WndClass.style = 0;
    	WndClass.cbClsExtra = 0;
    	WndClass.cbWndExtra = 0;
    	WndClass.lpfnWndProc = WndProc;
    	WndClass.hInstance = hInstance;
    	WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    	WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    	WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    	WndClass.lpszMenuName = 0;
    	WndClass.lpszClassName = "WinProg";
    
    	RegisterClass(&WndClass);
    	
    	HWND hButton;
    
    	hWindow = CreateWindow("WinProg","Window",
    		WS_OVERLAPPEDWINDOW,0,0,400,400,NULL,NULL,
    		hInstance,NULL);
    
    	hButton = CreateWindow("BUTTON","Start Thread 2",
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    		10,200,200,20,hWindow,(HMENU)1,hInstance,NULL);
    
    	hButton = CreateWindow("BUTTON","End Thread 2",
    		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    		10,240,200,20,hWindow,(HMENU)2,hInstance,NULL);
    
    	ShowWindow(hWindow, nCmdShow);
    
    	UpdateWindow(hWindow);
    
    	MSG Message;
    	while(GetMessage(&Message, NULL,0,0))
    	{
    		DispatchMessage(&Message);
    	}
    	return (Message.wParam);
    }
    
    DWORD WINAPI ThreadProc(LPVOID pvoid)
    {
    	int value = 2;
    	RECT border;
    	GetClientRect(hWindow,&border);
    	while(value == 2)
    	{
    		HDC hdc1;		
    		hdc1 = GetDC(hWindow);
    		Rectangle(hdc1,border.left+50,border.top +50,
    				  border.right - 50,border.bottom - 200);
    		ReleaseDC(hWindow,hdc1);
    	}
    
    	return 1;
    }
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage,
    						  WPARAM wParam, LPARAM lParam)
    {
    	static HANDLE hThread;
    
    
    
    	switch(uiMessage)
    	{
    	case WM_COMMAND:
    		if(HIWORD(wParam) == BN_CLICKED)
    		{
    			if(LOWORD(wParam) == 1)
    			{
    				DWORD dwThreadParam = 1;
    				DWORD dwThreadID;
    				hThread = CreateThread(NULL,0,ThreadProc,
    				   &dwThreadParam,0, &dwThreadID);
    			}
    
    			if(LOWORD(wParam) == 2)
    			{
    				TerminateThread(hThread,0);
    				WaitForSingleObject(hThread,INFINITE);
    				InvalidateRect(hWnd,0,TRUE);
    				UpdateWindow(hWnd);
    			}
    		}
    		return 0;
    		
    	case WM_DESTROY:  
    		PostQuitMessage(0);
    		TerminateThread(hThread,0);
    		return 0;
    	default:	 
    		return DefWindowProc(hWnd,uiMessage,
    		  wParam, lParam);
    	}
    }
    Notice that border is now a rect of the client are of the window...I then size this a little smaller to create the rect to draw...

    Also, I make the global HWND volatile.....this isnt really needed in this example as the HWND never changes, but if you are sharing variables beween threads, its sometimes safer to do (volatile is a c++ keyword that makes sure that the code uses the value in the variable and not just take it once as an optomization)

    Also, when you end the thread, you need to redraw the client area (otherwise the rect stays there - and you have no idea if the thread is destroyed or not). I can call InvalidateRect and UpdateWindow to do this, but as I found that the thread took a little time to destroy, and so was still drawing after the update had happened. To make sure the thread was dead before proceeding, I used a call to WaitForSingleObject to stop the code until the thread was well and truely destroyed

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    6
    Thank you for your time. The book dosn't go into enough detail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinAPI & threading
    By @nthony in forum Windows Programming
    Replies: 17
    Last Post: 10-15-2007, 04:41 PM
  2. do i still need winAPI
    By datainjector in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2003, 01:43 AM
  3. references for the winapi
    By stallion in forum Windows Programming
    Replies: 9
    Last Post: 01-28-2003, 02:56 AM
  4. WINAPI: Meaning of HDC ?
    By Mecnels in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2002, 10:06 AM
  5. C++ and WinAPI?
    By Matt2u in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2002, 12:57 AM