Results 1 to 2 of 2

Thread: threads in windows

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    threads in windows

    i can't get threads to work in my windows app...why?
    Code:
    //Steve Mack
    //Air Hockey
    
    #include <windows.h>
    #include <conio.h>
    
    HWND ghWnd_Main;
    
    HINSTANCE ghInst;
    
    MSG msg;
    
    HWND Clear;
    
    int puckx = 100;
    int pucky = 100;
    
    HANDLE HThread;
    unsigned long ThreadH;
    unsigned long _stdcall getchthread();
    char test = 'x';
    
    //function prototypes
    void SetFont(HWND hWnd, int iPointSize, const char *pcFontName);  //(obviously) sets fonts of controls
    void Draw();
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(uMsg) {
    		case WM_CLOSE:
    			TerminateThread(HThread,0);
    			DestroyWindow(ghWnd_Main); // Destroy the main window
    			return 0;
    
    		case WM_DESTROY:
    			TerminateThread(HThread,0);
    			PostQuitMessage(0); // Quit the application
    			return 0;
    
    		case WM_COMMAND:
    			//if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == OPENABOUT) {
    
    			return 0;
    	}
    	return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
    }
    
    //==================WINDOW CODE=========================================================
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    	ghInst = hInstance;
    	
    	WNDCLASSEX wcxMyClass;
    
    	wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
    	wcxMyClass.style = 0; // Extra style information
    	wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
    	wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
    	wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
    	wcxMyClass.hInstance = hInstance; // Instance handle for the class
    	wcxMyClass.hIcon = NULL; // Default icon - use system
    	wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
    	wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
    	wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
    	wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
    	wcxMyClass.hIconSm = NULL; // Small icon
    	
    	RegisterClassEx(&wcxMyClass);  // Register the class
    
    	ghWnd_Main = CreateWindow("TESTCLASS", "Air Hockey - Steve Mack", \
    		                      WS_SYSMENU | WS_MINIMIZEBOX, 100, 100, 550, 350, \
    							  NULL, NULL, hInstance, NULL);
    	if(ghWnd_Main) {
    		Clear = CreateWindow("Static", "", WS_CHILD | /*WS_DLGFRAME |*/ WS_VISIBLE, 0, 0, 500, 500, ghWnd_Main, NULL, hInstance, NULL);
    		ShowWindow(Clear,0);
    		ShowWindow(ghWnd_Main, nCmdShow); // Show using the specific show style
    	}
    
    	getchthread(); 
    	test = getch();
    	TerminateThread(HThread,0);
    
    	//=================== Handle message loop ==================================================
    	while(GetMessage(&msg, NULL, 0, 0)) {
    		TranslateMessage(&msg); // Translate keycode messages
    		DispatchMessage(&msg); // Send to the window
    	}
    
    	UnregisterClass("TESTCLASS", ghInst); // Unregister our window class
    	return msg.wParam; // msg.wParam contains the code passed to PostQuitMessage()
    
    }
    
    //=========================FUNCTIONS===============================
    void SetFont(HWND hWnd, int iPointSize, const char *pcFontName) {  //(obviously) sets fonts of controls
        
        HFONT hTheFont;
        HDC hDC = GetDC(hWnd);
        int nHeight = -MulDiv(iPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    
        hTheFont = CreateFont(nHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pcFontName);
    
        ReleaseDC(hWnd, hDC);
    
        SendMessage(hWnd, WM_SETFONT, (WPARAM)hTheFont, TRUE);
    }
    
    void Draw()
    {
    	HDC myDC = GetDC(ghWnd_Main);
    	Ellipse(myDC, puckx-10,pucky-10,puckx+10,pucky+10);
    }
    
    unsigned long _stdcall getchthread() 
    {
    	int i = 1;
    	char buffer[10];
    	while(test=='x') 
    	{
    		SetWindowText(Clear, itoa(i, buffer, 10));
    		i++;
    	}
        return 0;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You don't create the thread...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width