向 WM_PAINT 发送消息 [英] Sending message to WM_PAINT

查看:47
本文介绍了向 WM_PAINT 发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,用箭头键控制盒子的移动.我的代码有问题.是否可以通过 SendMessage 向 WM_PAINT 发送消息以在按 LEFT 键时绘制新矩形?以下是我的代码

I am trying to make a program for controlling the movement of a box with arrow key.I have a problem in my code.Is it possible to send a message through SendMessage to WM_PAINT to draw a new rectangle on pressing LEFT key? Following is my code

case WM_KEYDOWN:
    switch(wParam)
    {
       case VK_LEFT:                              //LEFT MOVEMENT


    SendMessage(hwnd,WM_PAINT,VK_LEFT,0);
        break;



       case VK_ESCAPE: 
           //FOR ENDING THE GAME WITH ESCAPE KEY
           SendMessage(hwnd,WM_DESTROY,VK_ESCAPE,0);
        break;
    }


case WM_PAINT:
    hdc=BeginPaint(hwnd,&ps);


      Rectangle(hdc,x,600,x1,700);
    if(wParam==VK_LEFT)
    { Rectangle(hdc,x-50,600,x1-50,700);

    }
    EndPaint(hwnd,&ps);
    return 0;
case WM_DESTROY:

    PostQuitMessage(0);
    return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
where 

         x1=550;
          x=500

当按下左键时,控件会转到 WM_PAINT

When left key is pressed then does the control goes to WM_PAINT

因为当我写

if(wParam==VK_LEFT)
        { Rectangle(hdc,x-50,600,x1-50,700);

        }

什么都没有发生,但是当我写的时候

nothing happens but when I write

if(wParam==VK_ESCAPE)
    PostQuitMessage(0);
    return 0;

窗口退出并带有转义键它不是最终代码.我只是想了解为什么 VK_LEFT 不执行?

The window exits with escape key Its not the final code .I am just trying to understand that why VK_LEFT doesn't executes?

推荐答案

你从不发送 WM_PAINT.系统负责这样做.相反,您调用 InvalidateRect 指定您希望重新绘制的矩形.系统会将该矩形标记为无效,并在下一个绘制周期发生时,重新绘制该矩形.

You never send WM_PAINT. The system is in charge of doing that. Instead, you call InvalidateRect specifying the rectangle that you wish to be re-painted. The system will mark that rectangle as invalid, and when the next paint cycle occurs, that rectangle will be re-painted.

如果您想使更复杂的区域无效,则使用 InvalidateRgn.

If you want to invalidate more complicated regions there is InvalidateRgn.

如果您只是希望整个窗口无效,请将 NULL 传递给 InvalidateRectlpRect 参数.

If you just wish for the entire window to be invalidated, pass NULL to the lpRect parameter of InvalidateRect.

这篇关于向 WM_PAINT 发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆