bombardier
bombardier

Reputation: 915

Display child window on fixed position in MFC CScrollView

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:

  1. Draw the hotspot icon and legend rectangle using GDI in handler of WM_PAINT of view class.
  2. Implement hotspot as a separate window that is created as a child of a scroll view.
  3. 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?

Upvotes: 1

Views: 1537

Answers (2)

bombardier
bombardier

Reputation: 915

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:

  1. client area above icon (rectangle width across entire client area)
  2. client area on the left of icon (rectangle height same as icon)
  3. client area on the right of icon (rectangle height same as icon)
  4. 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.

Upvotes: 1

vasylz
vasylz

Reputation: 177

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

Upvotes: 0

Related Questions