Showing Window Contents While Resizing the Splitter

Environment: Visual C++

The CSplitterWnd class of MFC does not draw panes while resizing. My class allows you to show a window’s contents while you are resizing the splitter pane. It supports only one row and two columns per splitter window now, but you can contribute additional code and develop it.

The CSplitterWnd class does not handle WM_SIZING because the window itself does not resize — neither do child windows — because the splitter does not notify them until the sizing finishes. I have used the WM_MOUSEMOVE handler with the MK_LBUTTON flag; this means that the user is dragging the splitter bar while moving the mouse. I have overriden the base class’ handler and put in my code to resize and paint panes while the user is dragging. I have also used two values to handle drag’s start coordinates; you may use it as POINT or CPoint, instead of two variables.

class CMySplitter : public CSplitterWnd
{
public:
  CMySplitter();
  virtual ~CMySplitter();
  // Overrides
  // ClassWizard generated virtual function overrides
  // {{AFX_VIRTUAL(CMySplitter)
  // }}AFX_VIRTUAL
protected:
  int moveX;  // move start X
  int moveY;  // move start Y
  afx_msg void OnMouseMove(UINT nFlags, CPoint pt);
  afx_msg void OnLButtonDown(UINT nFlags, CPoint pt);
  DECLARE_MESSAGE_MAP()
};

In implementation:

void CMySplitter::OnLButtonDown(UINT nFlags, CPoint pt)
{
  moveX=pt.x; // move start X position
  moveY=pt.y; // move start Y position
  CSplitterWnd::OnLButtonDown(nFlags, pt);
}

void CMySplitter::OnMouseMove(UINT nFlags, CPoint pt)
{
  if (nFlags==MK_LBUTTON) // Left button pressed
  {
    static int resized=0; // we will check if pane is resizing
    if (!resized)
    {
      int cxCur=0, cxMin=0;
      GetColumnInfo(0,cxCur, cxMin); // Get col's size before resizing
      if (moveY!=pt.y)               // If mouse moved vertically
      moveY=pt.y;                    // discard it
      if (pt.y==moveY)               // Are we ready to resize?
      {
        if (pt.x<moveX) // if pane 0 is resizing to the left side
                          // i.e. getting smaller
        {
          if (cxCur+(pt.x<moveX?pt.x-moveX:1)>=0)
          // will new size be greater than, or equal to, 0?
          {

          SetColumnInfo(0,cxCur+(pt.x<moveX?pt.x-moveX:1),cxMin);
          // set it
          moveX=pt.x; // set move start point to current position
          }
      }
      if (pt.x>moveX) // if pane 0 is resizing to the right side
                         // i.e. getting larger
      {
      if (cxCur+(pt.x>moveX?pt.x-moveX:-1)>=0)
      // check for new size, again
      {
      SetColumnInfo(0,cxCur+(pt.x>moveX?pt.x-moveX:-1), cxMin);
      // set it
      moveX=pt.x; // set move start point to current position
      }
    }
  }
  else
    if (pt.x!=moveX)   // hmm. mouse moved in vertical
      moveY=pt.y;      // anyway, set it to current Y coordinate
    RecalcLayout();    // recalculate pane sizes, and redraw
    GetColumnInfo(0,cxCur, cxMin); // get pane info
    moveX=cxCur;       // set current start position to pane's width
    resized=1;         // yes, we are resized
    return;
  }
  if (resized)         // we have resized pane
    resized=0;         // reset to resize in next time
  }
  CSplitterWnd::OnMouseMove(nFlags, pt);
  // if not leftbutton down, do the default thing.
}

Add this class into your project and create the splitter as you were creating it before.

There might be programming errors on the preceding code. I wrote it hurriedly because I need it. I run the code without any exceptions.

I am open for suggestions and corrections. Please feel free to write.

Downloads

Download demo project – 35 Kb

Download source – 42 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read