Thread: Responding to user input | WM_KEYDOWN | Virtual Key

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    7

    Responding to user input | WM_KEYDOWN | Virtual Key

    Following Window Procedure draws a specific pattern using WM_PAINT message (executes correctly). Now when user hit the UP arrow key, I want this pattern to move from position x=500, y=250 (declared as global variables) to position x=500, y=y+5.

    I tried to implement this using WM_KEYDOWN message, but with no luck. Any help would be highly appreciated...
    Code:
    LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
            case WM_CLOSE:
                if(MessageBox(hwnd, "Really quit?", "My application", MB_OKCANCEL)==IDOK)
                DestroyWindow(hwnd);
                break;
    
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            case WM_PAINT:
                DrawPixels(hwnd);
                break;
            case WM_KEYDOWN:
                {
                    switch(wParam)
                    {
                        case VK_UP:
                        y = y + 5;
                        break;
                        default:
                        break;
                    }
                }
                break;
            default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        return 0;
    }
    Code for DrawPixels function is also enclosed for reference purpose:
    Code:
    void DrawPixels(HWND hwnd)
    {
         PAINTSTRUCT ps;
         RECT r;
         GetClientRect(hwnd, &r);
         HDC hdc = BeginPaint(hwnd, &ps);
         SetPixel(hdc, -5 + x,      y, RGB(0,0,0));
         SetPixel(hdc, -4 + x,      y, RGB(0,0,0));
         SetPixel(hdc, -3 + x,      y, RGB(0,0,0));
         SetPixel(hdc,  3 + x,      y, RGB(0,0,0));
         SetPixel(hdc,  4 + x,      y, RGB(0,0,0));
         SetPixel(hdc,  5 + x,      y, RGB(0,0,0));
         SetPixel(hdc,      x, -5 + y, RGB(0,0,0));
         SetPixel(hdc,      x, -4 + y, RGB(0,0,0));
         SetPixel(hdc,      x, -3 + y, RGB(0,0,0));
         SetPixel(hdc,      x,  3 + y, RGB(0,0,0));
         SetPixel(hdc,      x,  4 + y, RGB(0,0,0));
         SetPixel(hdc,      x,  5 + y, RGB(0,0,0));
         EndPaint(hwnd, &ps);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,194
    Well you need to send yourself a redraw message - or something.
    WM_SETREDRAW message (Winuser.h) - Win32 apps | Microsoft Docs
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    7
    Quote Originally Posted by Salem View Post
    Well you need to send yourself a redraw message - or something.
    WM_SETREDRAW message (Winuser.h) - Win32 apps | Microsoft Docs
    Thanks for the pointer man... will study this & surely update... but it might take some time (as i am bit slow )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual memory in user space (Operting Systems)
    By Elysia in forum Tech Board
    Replies: 21
    Last Post: 05-07-2011, 04:09 PM
  2. user input
    By 182 in forum C++ Programming
    Replies: 14
    Last Post: 02-16-2006, 02:03 PM
  3. strcpy() Not Responding to Input
    By Kenji Miyamoto in forum C Programming
    Replies: 2
    Last Post: 12-06-2005, 01:53 AM
  4. using user input as a var.
    By nubi in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2003, 12:27 PM
  5. User input?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 07:13 PM

Tags for this Thread