none
How to prevent the client area of the window from being cleared when the window is maximized, minimized or the window is resized? RRS feed

  • Question

  • Hello, dear visitors of the forum. I am writing a Win 32 API application in C ++ using Visual Studio 2017 Community. I have the following question. How to prevent the client area of the window from being cleared when the window is maximized, minimized or the window is resized? For example, in the client area of the window of my application is the following picture.

    But, for example, if I reduce the size of the window using the mouse for this, the picture will disappear.

    (By the way, the progress bar also "creeps" to the right, but this is a topic for a separate post.) 

    The same thing (the disappearance of the picture) happens when I maximize the window or minimize it, and then open it and look. Please tell me how to prevent the picture from disappearing when resizing the window? Are there any special features in the Win 32 API for this? Or will I have to (at the first drawing of mesh) save the coordinates of the edges of the triangulation mesh in any collection, and then, after the window size has changed, actually redraw the triangulation mesh again using the data from the collection mentioned above? In MSDN, I, unfortunately, did not find any article about this (which surprised me a little). Help me, please, tell me how to solve the problem and make the picture does not disappear when the window is resized. I will be very grateful.




    Saturday, May 23, 2020 1:42 PM

Answers

  • Hi,

    Based on the MSDN article you provided, I can reproduce the problem.

    When I change the window from minimized to maximized, the window will have a short blank period, and then the image is redrawn.

    After your window is resized or maximized, will the image be redrawn after a short blank period?

    In addition, you need to notice that the blank period is generated by the for loop in the code, see the following code:

    for (UINT row = 0u; row < height; ++row)
    	{
    		// Obtain a pointer to the bitmap bits for the current row.
    		int *destPixel = bits + (row * width);
    
    		// Convert from image coordinate to coordinate on the complex plane.
    		double y0 = im_max - (row * im_factor);
    
    		for (UINT col = 0u; col < width; ++col)
    		{
    			// Convert from image coordinate to coordinate on the complex plane.
    			double x0 = re_min + col * re_factor;
    
    			double x = x0;
    			double y = y0;
    
    			UINT iter = 0;
    			double x_sq, y_sq;
    			while (iter < max_iterations && ((x_sq = x * x) + (y_sq = y * y) < 4))
    			{
    				double temp = x_sq - y_sq + x0;
    				y = 2 * x * y + y0;
    				x = temp;
    				++iter;
    			}
    
    			// If the point is in the set (or approximately close to it), color
    			// the pixel black.
    			if (iter == max_iterations)
    			{
    				*destPixel = 0;
    			}
    			// Otherwise, select a color that is based on the current iteration.
    			else
    			{
    				BYTE red = static_cast<BYTE>((iter % 64) * 4);
    				*destPixel = red << 16;
    			}
    
    			// Move to the next point.
    			++destPixel;
    		}
    	}
    

    This is the code snippet in the example. Every time after the window is redrawn, there are two for loops, which is why there is a blank period.

    Best regards,

    Strive


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Purple_Apple Monday, May 25, 2020 6:54 PM
    Monday, May 25, 2020 8:06 AM

All replies

  • You must draw in WM_PAINT and it won't disappear...
    Saturday, May 23, 2020 1:52 PM
  • Hello, Castorix. I draw it in WM_PAINT in the following way: I draw a triangulation mesh in BitMap in the background thread. And when the picture is ready, I add it to the image queue and in the OnPaint function I call the DrawImage function, which is a member of the Graphics class in GDI +. And the OnPaint function itself is called from WM_PAINT. Then I'll see what I have to change in my program. Thanks.

    P.S. I did the drawing of a triangulation mesh in the manner described in the MSDN article https://docs.microsoft.com/en-us/cpp/parallel/concrt/walkthrough-removing-work-from-a-user-interface-thread?view=vs-2019 of course, not with one hundred percent compliance, but adhered to the order described there.


    Saturday, May 23, 2020 2:21 PM
  • Windows doesn't try hard to save the contents of the window, especially when the area of the window has been invalidated. Basically your WM_PAINT handler should be ready to redraw the entire client area since your window can require redrawing at any time.

    For example, someone can be a pain and just randomly call InvalidateRect(hYourWindowHandle, NULL, TRUE) and this will force your window to redraw itself. What's more, moving another window in front of your own traditionally also caused a redraw due to the covered portion of the Window being invalidated. DWM stops that from happening, but I don't think Microsoft made that a contractual thing.

    So if your WM_PAINT handler isn't able to restore your window to what it should be, then it is an issue in your code.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Saturday, May 23, 2020 5:17 PM
  • Hi,

    Based on the MSDN article you provided, I can reproduce the problem.

    When I change the window from minimized to maximized, the window will have a short blank period, and then the image is redrawn.

    After your window is resized or maximized, will the image be redrawn after a short blank period?

    In addition, you need to notice that the blank period is generated by the for loop in the code, see the following code:

    for (UINT row = 0u; row < height; ++row)
    	{
    		// Obtain a pointer to the bitmap bits for the current row.
    		int *destPixel = bits + (row * width);
    
    		// Convert from image coordinate to coordinate on the complex plane.
    		double y0 = im_max - (row * im_factor);
    
    		for (UINT col = 0u; col < width; ++col)
    		{
    			// Convert from image coordinate to coordinate on the complex plane.
    			double x0 = re_min + col * re_factor;
    
    			double x = x0;
    			double y = y0;
    
    			UINT iter = 0;
    			double x_sq, y_sq;
    			while (iter < max_iterations && ((x_sq = x * x) + (y_sq = y * y) < 4))
    			{
    				double temp = x_sq - y_sq + x0;
    				y = 2 * x * y + y0;
    				x = temp;
    				++iter;
    			}
    
    			// If the point is in the set (or approximately close to it), color
    			// the pixel black.
    			if (iter == max_iterations)
    			{
    				*destPixel = 0;
    			}
    			// Otherwise, select a color that is based on the current iteration.
    			else
    			{
    				BYTE red = static_cast<BYTE>((iter % 64) * 4);
    				*destPixel = red << 16;
    			}
    
    			// Move to the next point.
    			++destPixel;
    		}
    	}
    

    This is the code snippet in the example. Every time after the window is redrawn, there are two for loops, which is why there is a blank period.

    Best regards,

    Strive


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Purple_Apple Monday, May 25, 2020 6:54 PM
    Monday, May 25, 2020 8:06 AM
  • True, I redraw Graphics there from GDI +, which I create in BitMap, but your answer, Strive Sun-MSFT, solved my problem. Thank you all very much for your help and support.
    Monday, May 25, 2020 6:56 PM