View Full Version: im new and trying to make a dot move

C++ Learning Community > win32 api C++ programming > im new and trying to make a dot move


Title: im new and trying to make a dot move
Description: can aany one help


cloudncali - June 9, 2005 11:44 PM (GMT)
iv reading the book proggraming windows and im stuck i am tryinf to make my little dot move on the screen but it just stays there here is the code
CODE
/*------------------------------------------------------------
  HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                (c) Charles Petzold, 1998
 ------------------------------------------------------------*/

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT ("HelloWin");
    HWND         hwnd;
    MSG          msg;
    WNDCLASS     wndclass;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = szAppName;

    if (!RegisterClass (&wndclass))
    {
         MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                     szAppName, MB_ICONERROR);
         return 0;
    }
    hwnd = CreateWindow (szAppName,                  // window class name
                         TEXT ("The Hello Program"), // window caption
                         WS_OVERLAPPEDWINDOW,        // window style
                         CW_USEDEFAULT,              // initial x position
                         CW_USEDEFAULT,              // initial y position
                         CW_USEDEFAULT,              // initial x size
                         CW_USEDEFAULT,              // initial y size
                         NULL,                       // parent window handle
                         NULL,                       // window menu handle
                         hInstance,                  // program instance handle
                         NULL);                     // creation parameters
   
    ShowWindow (hwnd, iCmdShow);
    UpdateWindow (hwnd);
   
    while (GetMessage (&msg, NULL, 0, 0))
    {
         TranslateMessage (&msg);
         DispatchMessage (&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC         hdc;
    PAINTSTRUCT ps;
    RECT        rect;
    int xx=100;
    int yy=100;
    switch (message)
    {
    case WM_CREATE:
         
         return 0;
    case WM_KEYDOWN:
         switch (wParam)
         {
         case VK_UP:
              yy-=3;
              UpdateWindow (hwnd);
              break;

         case VK_DOWN:
              yy+=3;
              UpdateWindow (hwnd);
              break;
             
         case VK_LEFT:
              xx-=3;
              UpdateWindow (hwnd);
              break;
             
         case VK_RIGHT:
              xx+=3;
              UpdateWindow (hwnd);
              break;
         case VK_ESCAPE:
              PostQuitMessage (0);
              break;
         }
    case WM_PAINT:
         hdc = BeginPaint (hwnd, &ps);
         
         MoveToEx (hdc, xx, yy, NULL);
         LineTo (hdc, xx+1, yy+1);

                   
         EndPaint (hwnd, &ps);
         return 0;
         
    case WM_DESTROY:
         PostQuitMessage (0);
         return 0;
    }
    return DefWindowProc (hwnd, message, wParam, lParam);
}


can any one help me plz

ramirez - June 10, 2005 12:14 AM (GMT)
Everytime your WndProc is called, the xx and yy are reinitialized to 100.
To fix this, you can make them static for example, so they are only created when the function is called the first time:
CODE
static  int xx=100;
static int yy=100;

cloudncali - June 10, 2005 01:35 AM (GMT)
oooooooo so that is what stadic ment. ty i'll try that
edit:

still no worky

C00L - June 10, 2005 02:06 AM (GMT)
1. you did not put "return 0;" at the end of WM_KEYDOWN
2. replace "UpdateWindow (hwnd);" with "InvalidateRect(hwnd, NULL, true);" on all the key down events.
3. Also this doesn't really matter, but you could use SetPixel instead of using MoveToEx and LineTo

cloudncali - June 10, 2005 02:23 AM (GMT)
ok now its working ty ty ty B)




* Hosted for free by InvisionFree