Thread: Windows CALLBACK

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Norfolk, England
    Posts
    2

    Windows CALLBACK

    Having returned to dabbling in programming after some time, I am writing a simple text editor as refresh exercise.
    Using Visual Studio 2019 to produce Windows programme
    When I enter a character via the keyboard, WM_CHAR picks it up and displays it, but if I resize the window it is erased from the screen. If I put the TextOut function in WM_PAINT as well it works OK, BUT If I put the TextOut only in WM_PAINT the character is not displayed until the window is resized.
    Relevant Code below

    Code:
    static struct NumEditStructure {    // structure for input fields
        TCHAR StrCha[SLEN];		    // characters in string
        int  StrCou;		    // total number of characters in string
    } EditStru, * PtrES;	
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        int lmId, lmEvent;
    
        PAINTSTRUCT ps;
        HDC hdc;
        TCHAR greeting[] = _T("Hello, Windows desktop!");
    
        switch (message){
        case WM_PAINT:                 // message    15
            hdc = BeginPaint(hWnd, &ps);
    
            // Here your application is laid out.
            // For this introduction, we just print out "Hello, Windows desktop!"
            // in the top left corner.
    
            hdc = BeginPaint(hWnd, &ps);
            TextOut(hdc,
                5, 5,
                greeting, _tcslen(greeting));
    
            //       Needed when paint called from ReSize etc
            TextOut(hdc,
                60, 60,
                EditStru.StrCha, _tcslen(EditStru.StrCha));
    
            EndPaint(hWnd, &ps);
            break;
            
        case WM_CHAR:                 // message VaLUE 0x0102
            hdc = GetDC(hWnd);
    
            wmId =      LOWORD(wParam);
            wmEvent =   HIWORD(wParam);
            lmId =      LOWORD(lParam);
            lmEvent =   HIWORD(lParam);
    
            EditStru.StrCha[EditStru.StrCou ] = wmId;
            EditStru.StrCha[EditStru.StrCou+1 ] = 0x00;
            EditStru.StrCou += 1;
    
    //      Needed to display entry at initial entry, before paint called from ReSize etc   
            TextOut(hdc,
                60, 60,
                EditStru.StrCha, _tcslen(EditStru.StrCha));
    
            SendMessage(hWnd, WM_PAINT, 0, 0);
            break;
            
        case WM_DESTROY:                 // message 
            PostQuitMessage(0);
            break;
        default:                        // message 
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }
    
        return 0;
    }

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    629
    Remove the GetDC and replace the TextOut and SendMessage in WM_CHAR with InvalidateRect(hWnd, NULL, TRUE). That'll properly generate a WM_PAINT to redraw the window.

    Also
    Code:
    wmId =      LOWORD(wParam);
            wmEvent =   HIWORD(wParam);
            lmId =      LOWORD(lParam);
            lmEvent =   HIWORD(lParam);
    This is only valid for WM_COMMAND (at least the wParam ones). Different messages put different things in these values - you need to check your reference or MSDN for what they mean for each message you're interested in
    Last edited by adeyblue; 2 Weeks Ago at 01:35 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Norfolk, England
    Posts
    2
    Thank you adeyblue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Callback Functions
    By valaris in forum C Programming
    Replies: 11
    Last Post: 07-31-2008, 09:20 PM
  2. Callback fuction
    By aamirsherkhan in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2008, 08:27 AM
  3. callback in a class, what do I do?
    By Yarin in forum C++ Programming
    Replies: 5
    Last Post: 08-17-2007, 11:11 AM
  4. CallBack Functions
    By manofsteel972 in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2004, 04:04 PM
  5. help!! callback in dll....
    By kimcs in forum Windows Programming
    Replies: 4
    Last Post: 06-30-2003, 07:47 AM

Tags for this Thread