redraw after minimize

This is a discussion on redraw after minimize within the Windows Programming forums, part of the Platform Specific Boards category; I am making a simple drawing program using GDI and Win32 API. It lets user to draw shapes, text etc ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    11

    redraw after minimize

    I am making a simple drawing program using GDI and Win32 API. It lets user to draw shapes, text etc on Window. Problem is, when window is minimized, everything that was drawn is gone. I have searched google and msdn but I had no luck. If I could save whatever that is drawn in the memory somewhere before window is minimized and then restore it later ... but I am not being able to do so. Can someone tell me how to do it? Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    How many things (like WM_PAINT) does your program deal with?

    There are more events which happen when you do what you describe.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Hmmm.. If you're asking whether or not it deals with WM_PAINT. Nope it doesn't. I know after window is minimized WM_PAINT message is sent. If I knew which messege is sent before window is minimized and background is erased, I could created a new HBITMAP and HDC and use BitBlt to copy whatever that was drawn on the Window to HBITMAP and then in WM_PAINT, I could again use BitBlt to copy it back to Window.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by Prads View Post
    Hmmm.. If you're asking whether or not it deals with WM_PAINT. Nope it doesn't. I know after window is minimized WM_PAINT message is sent. If I knew which messege is sent before window is minimized and background is erased, I could created a new HBITMAP and HDC and use BitBlt to copy whatever that was drawn on the Window to HBITMAP and then in WM_PAINT, I could again use BitBlt to copy it back to Window.
    Handling WM_PAINT is the only way any app should ever be drawing the window contents. If you have trouble doing that, then by all means let us know and we'll help you out.

    Don't even think about figuring out some other workaround just to make it work the way you have it. It's wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Oh! I'll post some of my code. I have a draw class which has a virtual function called drawFunc(HWND hwnd) which basically draws the object on Window. There are three base class:

    Code:
    class drawSPen: public drawClass {
    public:
    	void drawFunc(HWND hwnd);
    };
    Code:
    class drawSEllipse: public drawClass {
    public:
    	void drawFunc(HWND hwnd);
    };
    And

    Code:
    class drawSText: public drawClass {
    public:
    	void drawFunc(HWND hwnd);
    };
    When User selects pen, here's how the program draws it on window:

    First the mouse position is kept in POINT variable when mouse left button is down:

    Code:
    case WM_LBUTTONDOWN:
    			if (!ConvBitmap) {
    				LPos.x = LOWORD(lParam);
    				LPos.y = HIWORD(lParam);
    			}
    			break;
    ConvBitmap is a bool variable which tells whether the user has selected drawing mode or the bitmap mode. When user selects bitmap mode, user won't be able to draw on window.

    Now when user moves the mouse, drawing happens like this:

    Code:
    case WM_MOUSEMOVE:
    		if (!ConvBitmap) {
    			if (wParam == MK_LBUTTON && drawWhat == DRAW_SPEN) {
    				CPos.x = LOWORD(lParam);
    				CPos.y = HIWORD(lParam);
    				PenLine.drawFunc(hwnd);
    			}
    		}
    		break;
    drawWhat is global unsigned short int variable. And the value of DRAW_SPEN is defined in resource.h file. CPos is POINT variable to store current mouse coordinates. And PenLine.drawFunc(hwnd) calls the drawFunc(hwnd) from drawSPen class.

    Code:
    void drawSPen::drawFunc(HWND hwnd) {
    	HPEN CPen = CreatePen(PS_SOLID, PenSize, PenColor);
    	
    	hdc = GetDC(hwnd);
    	SelectObject(hdc, CPen);
    	
    	MoveToEx(hdc, LPos.x, LPos.y, NULL);
    	LineTo(hdc, CPos.x, CPos.y);
    	LPos = CPos;
    	
    	SelectObject(hdc, DefPen);
    	DeleteObject(CPen);
    	ReleaseDC(hwnd, hdc);
    }
    This is how my program draws stuff on the window and I don't know if this is the right or the wrong way.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Try a search here for 'double buffering' etc, this comes up quite often.

    This is MFC but demonstrates the main idea.

    How to avoid flickering in mfc
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    Thanks. I finally got double buffering to work. I need to rest now, my eyes are swelling due to lack of rest and being too much infront of computer. Haha I will rewrite the drawing code and post them here after I get some rest.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to force window redraw when moving... ?
    By jekko in forum Windows Programming
    Replies: 2
    Last Post: 02-28-2004, 01:45 AM
  2. Returning from fullscreen mode, redraw desktop
    By darksaidin in forum Windows Programming
    Replies: 4
    Last Post: 08-23-2003, 06:02 PM
  3. Forcing Cursor Redraw
    By PJYelton in forum Windows Programming
    Replies: 4
    Last Post: 07-07-2003, 07:07 PM
  4. Dialog box redraw
    By SushiFugu in forum Windows Programming
    Replies: 6
    Last Post: 02-05-2002, 10:21 AM
  5. can we minimize a dialog based application
    By vicky in forum Windows Programming
    Replies: 6
    Last Post: 09-27-2001, 07:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21