Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change textout() when it's already drawn?

I’m trying to move static TCHAR greeting[] = _T("123"); by using arrows, but it doesn’t move at all. MessageBox is used as a confirmation of getting keyboard input.

void move(HWND hWnd, WPARAM wParam, LPARAM lParam, unsigned int *x, unsigned int * y) {
    RECT rt;
    if (wParam == VK_LEFT) {
        GetClientRect(hWnd, &rt);
        x = x - 5;
        InvalidateRect(hWnd, &rt, FALSE);
        MessageBox(hWnd, LPCSTR("123"), LPCSTR("123"), MB_OK);
        
    }
    if (wParam == VK_DOWN) {
        GetClientRect(hWnd, &rt);
        y = y - 5;
        InvalidateRect(hWnd, &rt, FALSE);
        

    }
    if (wParam == VK_UP) {
        GetClientRect(hWnd, &rt);
        y = y + 5;
        InvalidateRect(hWnd, &rt, FALSE);
        
    }
    if (wParam == VK_RIGHT) {
        GetClientRect(hWnd, &rt);
        x = x + 5;
        InvalidateRect(hWnd, &rt, FALSE);
        
    }
}
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
        PAINTSTRUCT ps;
        HDC hdc;
        static TCHAR greeting[] = _T("123");
        unsigned int x = 50;
        unsigned int y = 50;

        switch (message)
        {
        case WM_PAINT: {
            hdc = BeginPaint(hWnd, &ps);
            
            TextOut(hdc,
                x, y,
                greeting, 3);


            EndPaint(hWnd, &ps);
            break;
        }

        case WM_KEYDOWN:
            move(hWnd, wParam, lParam, &x, &y);
            break;
        case WM_DESTROY: {
            PostQuitMessage(0);
            break;
        default: {
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }

        }
                       return 0;
        }
}

Can somebody explain, what is wrong here? I’m still trying to learn about <Windows.h> so pls don’t try to make the code much more complicated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The x and y variables you are using to draw the text with are local to WndProc and are always reset to the initial values whenever a new message is received. You need to move their declarations outside of WndProc() (or at least make them static) so that their values can persist between messages. You can then update their values in your WM_KEYDOWN handler, and use their current values to draw the text in your WM_PAINT handler.

Also, your move() function is updating the pointers that are pointing at the x and y variables, it is not updating the values of the x and y variables themselves.

Try this instead:

void move(HWND hWnd, WPARAM wParam, LPARAM lParam, unsigned int *x, unsigned int * y) {
    switch (wParam) {
        case VK_LEFT:
            *x -= 5;
            InvalidateRect(hWnd, NULL, FALSE);
            break;

        case VK_DOWN:
            *y -= 5;
            InvalidateRect(hWnd, NULL, FALSE);
            break;

        case VK_UP:
            *y += 5;
            InvalidateRect(hWnd, NULL, FALSE);
            break;

        case VK_RIGHT:
            *x += 5;
            InvalidateRect(hWnd, NULL, FALSE);
            break;
    }
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    static TCHAR greeting[] = _T("123");
    static unsigned int x = 50;
    static unsigned int y = 50;

    switch (message) {
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            
            TextOut(hdc,
                x, y,
                greeting, 3);

            EndPaint(hWnd, &ps);
            break;
        }

        case WM_KEYDOWN:
            move(hWnd, wParam, lParam, &x, &y);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading