• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

redrawing text and graphics of a window

If I draw some graphics or text in a window using DC classes, I can't be able to regain the content after minmizing and maximizing or if another window overlaps, they are lost.  Pls tell me What I should do to redraw the contents.

  • 2
  • 2
  • 2
  • +5
2 Solutions
U should write ur code in the WM_PAINT message i.e override this message in the window class.
In OnPaint() of that window place ur code to draw grahics or to write text.
Hope that helps u.
use Ondraw(CDC *pDC) of your view class to draw inthe client area, this function is called for every WM_PAINT message.
venkataramananAuthor Commented:
i am having a static control in a dialog box and i like have to free hand drawing in that.  I am capturing the mousedown and mousemove events and setting pixels in that static control.  I want that drawing to be persistent.  what i have to do for this
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

AndyAinscowFreelance programmer / ConsultantCommented:
You need to store the mouse/keyboard actions somehow and then 'replay' this store everytime you need to repaint.  See the Scribble example that comes with VC++, if I remember correctly that does the same as you are doing just in a view.
Hi Venkataramanan ,
 you can store your drawings in a metafile and then eveytime you refresh(minimze or maximize) you will play back the graphics by calling the method
 PlayMetaFile(myfile)which should be called from the
N.B:The metafile can store DC class graphics.
Write a comment for me if you're interested and  want me to give you the whole code.
Good luck
Hi! I recommend you first CreateCompatibleDC() of your screen (GetDC() of your display), then CreateCompatibleBitmap() (And pass in the compatible DC).

Once you have this, all drawing, do it to the Bitmap instead (You can just get the DC of the Bitmap anytime to make changes to it). Then, when you have to display the bitmap on the screen, in the OnDraw(), take your Bitmap DC, the and CDC passed to you in the OnDraw, and do a pDC->BitBlt() passing in the bitmap's DC.

This is the simplest, fastest, and most easily understood method I know of.

Good luck, questions?
Dear Mazen,

I will be very much happy if u give me the code.

Thanks in advance.

Hi Venkataramanan, ok there goes the code:
first in the Doc.h write in the public area:
CMetaFileDC* pMetaFileDC;
and in the Doc constructor write this:
pMetaFileDC = new CMetaFileDC();
Now in the methods that are used to draw graphics you should mirror every drawing to the metafile like this, for example if you're drawing ellipse:
also write with it:
and so on...
and don't forget to declare the pointer pDoc to Doc using GetDocument() in these methods
Now to play the metafile again at refreshing,add the following code to the OnDraw() method:
HMETAFILE MetaFileHandle = pDoc->pMetaFileDC->Close();
// to play a metafile you must close it first
CMetaFileDC* replaceMetaFile = new CMetaFileDC();
// this is a back up metafile before deletion
delete pDoc->pMetaFileDC;
pDoc->pMetaFileDC = replaceMetaFile;

I hope this helps you
Best Regards
Try to use the metafile for storing drawing actions and in OnDraw method play that metafile content.
AndyAinscowFreelance programmer / ConsultantCommented:
My comment should have given him all he required.
I think most of ours did, one way or another.

The question is likely abandoned because it got answered satisfactorily. :\

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now