Thread: Usage of functions

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Usage of functions

    Hello, this one is really ridiculous one. There are no problems at all with compilation but there is still a problem with functions.
    I have no idea why does not the programme call my function but the code is right (I hope) if I copy the code straight to the WM_PAINT command it works normally. But if I try to call a function with the same content as before it suddenly fails to work...

    Can you chceck please the code?

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       PSTR szCmdLine, int iCmdShow)
    {
        TCHAR szAppName[] = TEXT("Default window");           // Header
        HWND hWnd;
        MSG msg;
        WNDCLASSEX wc;
        
        wc.cbSize = sizeof(wc);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);            // Ico
        wc.hIconSm = NULL;                                     // Ico_sm
        wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+0);           // Background
        wc.lpszMenuName = NULL;
        wc.lpszClassName = szAppName;
        
        RegisterClassEx(&wc);
        
        hWnd = CreateWindowEx(0,szAppName,
                            szAppName,
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT,                    // Margin from left
                            CW_USEDEFAULT,                    // Margin from top
                            CW_USEDEFAULT,                    // Widht
                            CW_USEDEFAULT,                    // Height
                            NULL,
                            NULL,
                            hInstance,NULL);
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
        
        while(GetMessage(&msg, NULL, 0, 0))
        {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    void OnWM_PAINT()
    {
        HWND         hWnd;
    	HDC			hDC;
    	PAINTSTRUCT ps;
    	RECT		rect;
    	GetClientRect(hWnd, &rect);
        hDC = BeginPaint(hWnd, &ps);
    	DrawText(hDC, "Text text", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    	EndPaint(hWnd, &ps);
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
            switch(message)
            {
              case WM_PAINT:
    	OnWM_PAINT();
    	break;
              case WM_DESTROY:
              PostQuitMessage(0);
              return 0;
            }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    MS VC++
    Dev-C++

    Thank you for support

  2. #2

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    because your function defines hWnd but in WndProc it's an argument. You need to use the hWnd that is passed as argument and not define one yourself.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by KONI View Post
    because your function defines hWnd but in WndProc it's an argument. You need to use the hWnd that is passed as argument and not define one yourself.
    So I deleted the line with HWND definition, now the function is:

    Code:
    void OnWM_PAINT()
    {
    	HDC			hDC;
    	PAINTSTRUCT ps;
    	RECT		rect;
    	GetClientRect(hWnd, &rect);
        hDC = BeginPaint(hWnd, &ps);
    	DrawText(hDC, "Text text", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    	EndPaint(hWnd, &ps);
    }
    (The rest of the code was left just the same)

    But there is an error in compilation:
    53 c:\docume~1\gordon\desktop\window\win.cpp `hWnd' undeclared (first use this function)

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    And how do you think could your function possibly know about hWnd if you don't pass it as argument ?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You didn't follow instructions.

    Code:
    void OnWM_PAINT(HWND hWnd)
    {
    	HDChDC;
    	PAINTSTRUCT ps;
    	RECTrect;
    
    	GetClientRect(hWnd, &rect);
    	hDC = BeginPaint(hWnd, &ps);
    	DrawText(hDC, "Text text", -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    	EndPaint(hWnd, &ps);
    }
    Your callback function:

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    		case WM_PAINT:
    			OnWM_PAINT(hWnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Ok cool now it works...

    Thx to everybody

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If you want to do the work in a separate function, you can use HANDLE_MSG

    Code:
    switch(message)
    {
         HANDLE_MSG(hWnd, WM_PAINT, App_OnPaint);
         ...
    Then your function MUST be defined with these parameters and return value:
    Code:
    void App_OnPaint(HWND hWnd)
    {
         // This function will be called everytime you get a WM_PAINT message
    }
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM