回 帖 发 新 帖 刷新版面

主题:关于用键盘控制矩形移动的问题~~~~~急

#include <windows.h>
        
#include <math.h>
        
#define NUM 1000
        
#define TWOPI      (2 * 3.14159)
        
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
        

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        
                   PSTR szCmdLine, int iCmdShow)
        
{
        
    static TCHAR szAppName[] = TEXT ("SineWave") ;
        
    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 ("Program requires Windows NT!"),
        
                   szAppName, MB_ICONERROR) ;
        
                   return 0 ;
        
    }
        
   
        
    hwnd = CreateWindow ( szAppName, TEXT ("Sine Wave Using Polyline"),
        
                          WS_OVERLAPPEDWINDOW,
        
                           CW_USEDEFAULT, CW_USEDEFAULT,
        
                           CW_USEDEFAULT, CW_USEDEFAULT,
        
                           NULL, NULL, hInstance, NULL) ;
        
    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)
        
{
        
    static int  cxClient, cyClient ;
        
    HDC         hdc ;
        
    int         i;
static    int x;
static    int    y;
        
    PAINTSTRUCT ps ;
        
    POINT       apt [NUM] ;
          
   
        
    switch (message)
        
    {
       


        
    case   WM_SIZE:
        
            cxClient = LOWORD (lParam) ;
        
           cyClient = HIWORD (lParam) ;
         
            return 0 ;
   case WM_KEYDOWN:
        
            switch (wParam)
        
    {
          
        
    case VK_UP:
        
             y=y-100 ;
        
            break ;
        

    case VK_DOWN:
        
           y=y+100;
        
            break ;
        
             
        
    case VK_LEFT:
        
            x=x-100;
        
            break ;
        
             
        
    case VK_RIGHT:
        
            x=x+100;
        
            break ;
        
            }
        
            return 0 ;

     
    case   WM_PAINT:
        
            hdc = BeginPaint (hwnd, &ps) ;
        
        
        Rectangle (hdc, x, y, 50+x, 40+y) ;


        
         EndPaint (hwnd, &ps) ;

            return 0 ;
   
   case   WM_DESTROY:
        
            PostQuitMessage (0) ;
        
            return 0 ;
        
    }           
    return DefWindowProc (hwnd, message, wParam, lParam) ;
        
}
  
这个程序我的目的是用键盘的上下左右键控制矩形的移动,但是不成功。我想问问这是什么问题呢?谢谢~~

回复列表 (共3个回复)

沙发

#include <windows.h>
        
#include <math.h>
        
#define NUM 1000
        
#define TWOPI      (2 * 3.14159)
        
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
        

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
        
                   PSTR szCmdLine, int iCmdShow)
        
{
        
    static TCHAR szAppName[] = TEXT ("SineWave") ;
        
    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 ("Program requires Windows NT!"),
        
                   szAppName, MB_ICONERROR) ;
        
                   return 0 ;
        
    }
        
   
        
    hwnd = CreateWindow ( szAppName, TEXT ("Sine Wave Using Polyline"),
        
                          WS_OVERLAPPEDWINDOW,
        
                           CW_USEDEFAULT, CW_USEDEFAULT,
        
                           CW_USEDEFAULT, CW_USEDEFAULT,
        
                           NULL, NULL, hInstance, NULL) ;
        
    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)
        
{
        
    static int  cxClient, cyClient ;
        
    HDC         hdc ;
        
    static    int x;
    static    int    y;
        
    PAINTSTRUCT ps ;
        
          
   
        
    switch (message)
        
    {
       


        
    case   WM_SIZE:
        
            cxClient = LOWORD (lParam) ;
        
           cyClient = HIWORD (lParam) ;
         
            return 0 ;
   case WM_KEYDOWN:
    switch (wParam)        
    {
    case VK_UP:
        
             y=y-100 ;
        
            break ;
        

    case VK_DOWN:
        
           y=y+100;
        
            break ;
        
             
        
    case VK_LEFT:
        
            x=x-100;
        
            break ;
        
             
        
    case VK_RIGHT:
        
            x=x+100;
        
            break ;
        
            }
            InvalidateRect(hwnd,NULL,TRUE);//添加
        
            return 0 ;

     
    case   WM_PAINT:
        
            hdc = BeginPaint (hwnd, &ps) ;
        
        
        Rectangle (hdc, x, y, 50+x, 40+y) ;


        
         EndPaint (hwnd, &ps) ;

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

板凳

InvalidateRect(hwnd,NULL,TRUE);//添加


这句话是不是使矩形无效化呢?
第二和第三个参数使代表什么呢?
因为我看的msdn那上面都是英文来的,我看不懂,所以希望你能帮忙解释一下,谢谢!~~

还有msdn有没有中文的说明呢?

3 楼

函数原型为:
BOOL InvalidateRect(HWND hWnd,CONST RECT *lpRect,BOOL bErase);

该函数向指定的窗口(由hWnd指定)添加一个无效区域(区域大小由指向RECT的指针lpRect指定),Windows向窗口(hWnd)发送一条WM_PAINT消息要求窗口重绘,无效矩形区域就是由lpRect指定的,参数bErase指出是否要求擦除背景。

在你的程序中第二个参数用NULL表示要求重绘整个窗口,第三个参数取TRUE表示要求
擦除背景,不然移动前的矩形还在,你可以试试。

我来回复

您尚未登录,请登录后再回复。点此登录或注册