简体   繁体   English

MFC C++ 从 WM_KEYDOWN 处理程序获取在 OnDraw() 中编辑的 CDC pDC 的句柄

[英]MFC C++ Getting handle to CDC pDC edited in OnDraw() from WM_KEYDOWN handler

Single Document, the simplest MFC app. Single Document,最简单的 MFC 应用程序。

The idea is that default CDC pDC is edited(colored) in some way from onDraw() function.这个想法是从 onDraw() 函数以某种方式编辑(着色)默认的 CDC pDC。 When a user clicks on a number I want it to be displayed using colors from pDC.当用户单击一个数字时,我希望它使用来自 pDC 的颜色显示。

If I use the default handler function OnKeyDown for WM_KEYPRESS I don't get a pointer for my edited pDC.如果我对 WM_KEYPRESS 使用默认处理函数 OnKeyDown,我不会得到我编辑的 pDC 的指针。

My question is how to access the edited pDC?我的问题是如何访问编辑后的 ​​pDC? I am sure that there is a simple solution that I am missing, please help.我确定我缺少一个简单的解决方案,请帮忙。

I am not 100% sure I understand the question correctly, but let me try....我不是 100% 确定我正确理解了这个问题,但让我试试......

The usual and recommended way in MFC to do what I think you want would be to handle all drawing in onDraw() only. MFC 中通常和推荐的方法是只处理onDraw()所有绘图。

So, in the onKeyDown() handler, you would store the pressed key to a member variable (or maybe even push it to a vector or list of keys to be drawn) and then call Invalidate(false) .因此,在onKeyDown()处理程序中,您会将按下的键存储到成员变量(或者甚至可能将其推送到要绘制的vector或键list ),然后调用Invalidate(false) That causes Windows to generate a WM_PAINT message to your window, which ends up being handled in onDraw() , where you can now draw the correct things based on your current member variable values.这会导致 Windows 向您的窗口生成WM_PAINT消息,最终在onDraw()处理,您现在可以根据当前成员变量值绘制正确的东西。

It is also possible to create a CPaintDC outside of onDraw() and draw on that.也可以在onDraw()之外创建一个CPaintDC并在其上绘制。 But as said, usually in MFC applications, all drawing is kept in one place.但如前所述,通常在 MFC 应用程序中,所有绘图都保存在一个地方。 Windows may request your app to redraw at any time and it does that with a WM_PAINT message. Windows 可能会随时请求您的应用程序重绘,并通过WM_PAINT消息执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM