Showing Window Contents While Resizing the Splitter | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 12, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.