简体   繁体   中英

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

Single Document, the simplest MFC app.

The idea is that default CDC pDC is edited(colored) in some way from onDraw() function. When a user clicks on a number I want it to be displayed using colors from pDC.

If I use the default handler function OnKeyDown for WM_KEYPRESS I don't get a pointer for my edited pDC.

My question is how to access the edited 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....

The usual and recommended way in MFC to do what I think you want would be to handle all drawing in onDraw() only.

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) . 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.

It is also possible to create a CPaintDC outside of onDraw() and draw on that. But as said, usually in MFC applications, all drawing is kept in one place. Windows may request your app to redraw at any time and it does that with a WM_PAINT message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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