"Isolating" drawing of child windows / sub-elements in Win32 API (GDI)


I’m attempting to make a GUI layout that looks something like this:

+         +                 +       +
+         +                 +   3   +
+         +                 +       +
+    1    +        2        +++++++++
+         +                 +       +
+         +                 +   4   +
+         +                 +       +

All of these areas/elements will be fully custom drawn, and I have already written most of the painting functions for these 4 individual elements.

Initially, I had a child window that was merely an implementation of (2) above, that simply occupied the entire main window.

Then I decided to do a bit of an overhaul when I realized (whoa!) that I could just do all the custom drawing directly in the main window instead of in a child window.

I have recently begun implementing drawing (3) and (4), and currently draw everything using the same window/procedure (WM_PAINT handler). I have one memory DC (for double-buffering) and one bitmap for the entire client area that I draw with/to. In the process of implementing these 2 elements, one thing I’ve been a bit peeved about, especially since I draw everything onto a single window/bitmap, is that I always need to add the respective offsets for the start of each element’s rectangle each time I want to paint a inside of one particular element (+ strictly ensure the drawn contents won’t crash/overlap with the other elements), rather than using plain offsets relative to the elements themselves.

For instance, (2) contains a waveform that’s drawn using Polyline, and does something like PolylinePoints[N].x = waveformRectangle->x + sample_x_coord instead of simply PolylinePoints[N].x = sample_x_coord. And mind you, this is in a loop of often 1000+ iterations, so it adds up to a lot of instructions.

Would it be ill-advised to allocate 3 more DCs and/or bitmaps only to "isolate" the drawing into 4 different contexts? Or is there an even better way to accomplish what I want without so many resources? Is it crazy altogether to draw this kind of UI using only one window/process? Or would it mostly be a waste of time to make an overhaul to use child windows once again? Keep in mind that I also intend to draw (3) and (4) as one single element (i.e. "combined") in some cases.

Appreciate any tips or best practice advise!


I am blind so can’t see exactly what you hope to accomplish but why not change the coordinate frame with SetViewportOrgEx/SetViewportExtEx for each part of the window (that is, changing where 0, 0 lands)? That way you can use coordinates that are more convenient for a particular sub-region of the window without going through the trouble of creating memory DCs and blitting back into the main DC.

Answered By – SoronelHaetir

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published