Display child window on fixed position in MFC CScrollView - c++

I have a window inherited from CScrollView that handles WM_PAINT and displays a graph. This graph has elements of different types and type of element is marked by the shape of element.
I want to display some legend so that user knows what each shape means. Since user will not look at this legend often the idea is that in the upper right corner of the view control there will be a small icon. When user moves the mouse over this icon it will expand into small rectangle displaying the legend of shapes used in graph. When mouse is moved outside this rectangle it will collapse back to small icon.
This is what I have tried so far:
Draw the hotspot icon and legend rectangle using GDI in handler of WM_PAINT of view class.
Implement hotspot as a separate window that is created as a child of a scroll view.
Implement hotspot as a separate window but create it as a popup window with no parent.
Version 1 and 2 behave strangely if user scrolls the graph view.
In version 1 I can see artifacts (button is smeared across the view) while performing scroll I guess that during the scroll existing image is not invalidated but only moved.
In version 2 there is no smearing but I need to move the child window whenever I get VM_HSCROLL and WM_VSCROLL messages and these messages are not precise enough to be used this way. During scroll I can see my button moving couple of pixels and then jumping to correct position.
Version 3 is the most disturbing and because legend is not child of a graph view I get some strange behaviour where legend button is displayed even when graph view is not shown and I think that there are too many problems with this one to be viable development path to invest time in.
I think that the version 2 is the most promising but I need to find a way to make a child window stay at one place during scroll.
Did anyone have success in implementing something like this? Is there some other implementation I can investigate?

CScrollView has OnScrollBy virtual method that scrolls the entire client area of view control. You can override this method and implement your own scroll functionality.
::ScrollWindowEx has a parameter prcScroll that can specify only a portion of client area that will be scrolled.
New OnScrollBy splits client area into 4 stripes and calls ::ScrollWindowEx for each one of them.
These stripes are:
client area above icon (rectangle width across entire client area)
client area on the left of icon (rectangle height same as icon)
client area on the right of icon (rectangle height same as icon)
client area underneath icon (rectangle width across entire client area)
1 1 1 1 1
2 2 2 * 3
4 4 4 4 4
4 4 4 4 4
After scrolling all individual client rectangles, just invalidate client area where icon is located.

You may just simply use
CPoint GetScrollPosition( ) const;
method to obtain current scroll position of scroll view and recalculate your 'static' label offset accordingly.
BTW: Instead of WM_PAINT use CScrollView's method
virtual void OnDraw( CDC* pDC );
it's important

Related

How to clip the corner rectangle created by two scrollbar controls

Let's say you have a resizable window with child scrollbar controls, and the scrollbars come and go depending on whether the window contents are large enough to require scrolling.
When both scrollbars are present, a small rectangle is effectively created in the bottom right corner of the window, at their intersection. Is there a clean strategy for clipping that rectangle when drawing on the window, so that you don't paint on it?
I guess my current approach is to obtain the rectangles for each scrollbar, and if those rectangles are not null, then use the rectangles' locations to determine the rectangle that we want to clip. And then call ExcludeClipRect for that rectangle. I guess a similar approach could be used, except with GetSystemMetrics(SM_CXVSCROLL) and GetSystemMetrics(SM_CYVSCROLL) to get the rectangle dimensions.
But is there a more accepted way of doing this, perhaps using some helpful clipping API functions? Thank you for any input.

MFC - CPropertyPageEx and scaling (4K monitor)

I'm upgrading an old MFC app to support 4K monitors. According to what information I can find, CPropertySheetEx 'implements Wizard97 style functionality'. It's a dialog with Previous and Next buttons and a banner.
Now MFC has done a poor job of scaling this dialog and I'm not sure what control I have over it.
4K is usually 200% scaling. The banner height is unchanged at 59 pixels (so is too small on a 4K monitor). The rest of the dialog seems to have scaled to about 150% (width, height).
I've tried SetWindowPos on the banner. This doesn't resize it and causes other dialog issues.
There's this note in the code, which makes me think the banner is dynamically constructed with the property sheet.
// If the page has a header, we need to paint the area above the border.
// By inspection (Spy++), the border is a static control with ID 0x3027
CWnd* pTopBorder = GetDlgItem(0x3027);
Any suggestions or guides would be appreciated.
The app is DPI aware. Many MFC components scale properly (some need some work)
Question - How can I get CPropertyPageEX dialogs to support scaling?
Here's an image ![Scaling Issue]https://imgur.com/a/Ww8SLnU
Edit -
The icon and the text in the banner can be resized and repositioned, only the height of the banner seems stuck at 50 pixels.
CPropertyPageEx is defined as CPropertyPage in MFC 11. It's derived directly from CWnd.
Having a look at the sources for CPropertyPage (atlmfc\src\mfc\dlgprop.cpp) there's no constructor where it creates buttons or banners (so I may be looking in the wrong place).
A PropertySheet seems to consist of
a graphic (user supplied),
a horizontal bar (static control),
a dialog resource from the Property Page currently displayed
another horizontal bar
a series of buttons (Prev , Next etc)
Have a look at my high def screenshot https://imgur.com/a/yR97H96
The dialog and controls have rescaled
The vertical position of the horizontal bar and the property page are both unchanged from the unscaled version, leading to the ugly overlap you can see.
.

Roku-Create Selectable List

I am in the process of making a Roku channel. The idea is to have a full screen player going, if the user presses a particular button on remote, a small pop up menu will display in a corner of the screen with a list of available channels. I have all working with the following exception: I can't figure out how to populate the area where the menu displays. Currently I have a transparent roImageCanvas on layer 1, the menu box is drawn on layer 2. The problem is that roImageCanvas allows for a text element but only one Item. So if I have a list of 10 channels, I would have to create 10 items on the canvas. The roImageCanvas does not accept arrays. So there is no way to create the pop menu on the fly if the number of channels changes. The number of items on the canvas has to be hard coded as far as I can tell. Ideally the roListScreen is what I would like to pop up but from what I understand all screens are full screen all the time. Does anybody know of a way to populate the targetbox on the canvas or create a screen that is resizable? Thanks for any suggestions
A roImageCanvas layer is an array. There is no technical limitation to you adding >1 elements to a layer and so you can add as many separate text items as you want (not hard-coded!). It seems to me best to have 1 text element per 1 menu item, so you can use their bounding rectangles (or text color) to highlight the choice

Allow user to draw a drag rectangle in CStatic C++ MFC App

I have a MFC application where I have a Picture Control in the dialog. Eventually, I want to allow a user to draw a resizeable rectangle via mouse drag in the picture control over an image that I loaded.
I defined my own picture control class as a sub class of CStatic and am working with the mouse down, mouse up, and mouse move events but I can't seem to figure out how to allow the user to draw a rectangle. Any guidance on this would be appreciated.
Most of the examples I've looked at show me how to draw a rectangle in a CView:CWnd, but I'm not too familiar with MFC yet so I'm a bit lost. Thanks.
The usual technique for drawing a drag rect on top of the window contents is illustrated here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx
That is Win32 API coding instead of MFC coding but the differences are minimal. The basic idea is that by drawing with SetROP2(hdc, R2_NOTXORPEN); you invert the existing pixels, then drawing the same rect again re-inverts those pixels back to the original image.
When the user clicks the mouse button you need to record the mouse coordinates so you know where the rectangle starts. You should also set some type of flag to indicate that the user is dragging the mouse. When the user moves the mouse get the current mouse position and use DrawDragRect or similar function to draw the rectangle. When the user releases the mouse button clear the previously mentioned "flag" and you're done with that part of the process.
You will also need to handle other events such as the control and/or parent window losing focus so that you can cancel the drag/draw operation. Since you did not include any code in your question it's hard to say what else you will need to do but those are the basics.

Child Windows that does not occupy client area

In the Win32 API (pure win32), The Menu bar does not occupy any area from the client area of the window. Which means the origin coordinates of the client area is right under the menu bar to the left.
When we create child window controls using CreateWindow (or any other method), that window takes some area of the client-area.
eg:- Creating a button which is at (xPos = 0, yPos = 0) and (width=width_of_client_area, height=20).
After creating the button if you'll use a GDI function like this, it'll be drew below the button:
Rectangle(hdc, 0,0, 200, 200);
But when creating a menu bar, it doesn't occupy client area. (GDI will not be drew under menu).
FINAL QUESTION:
How can i create a control on my parent window like the menu bar ?
The menu is rendered in the non-client area of the window, which is driven by a completely different set of window messages. Keep in mind that you don't actually create child windows for these types of controls. You will need to provide all the rendering and event handling for the customization you want to add. This means that if you want to add a button or checkbox you will need to implement it yourself. You can get your started with a handful of steps but there may be other things that need to be done depending on your requirements.
The first step is to process the WM_NCCALCSIZE message. This will allow you to adjust the size of the non-client area so that you have more space to draw the custom control. Basically you will pass this message on to the default window proc then modify the location and dimensions (just drop the top down X pixels) before returning.
The next step is to handle WM_NCPAINT message. Pass the message on to the default window proc then draw your custom visuals in the area you have reserved. You will be working with a window DC so you can draw to the entire window. It's important to keep this in mind so you don't destroy anything in the client area.
The last item that needs to be done is to handle mouse messages like WM_NCMOUSEMOVE. If the mouse event occurs outside the area where your control is located pass the message to the default window proc, otherwise handle the message yourself and skip the default proc. you will need to manage some state data for keeping track of when a button is down or not so as not to cause conflicts with the default window proc.
Keep in mind that when you want to draw directly to the non-client area you need to call GetWindowDC() instead of GetDC() to acquire a device context.
[Note: A good Google dance will be something like "WinAPI non-client area"]