Click here to Skip to main content
16,021,810 members
Articles / Desktop Programming / MFC

How to Prevent Resizing of Views in a Splitter Window

Rate me:
Please Sign up or sign in to vote.
4.94/5 (35 votes)
24 Feb 2004CPOL2 min read 126.2K   2.4K   44   15
Disabling mouse input to a splitter window

Introduction

I suspect this may be the most trivial class ever presented on CodeProject.

In an MFC application I'm writing, I wanted an explorer style interface with a tree control on the left and a view class on the right. But I wanted to prevent the user being able to grab the divider and resize the views.

The application was created using the AppWizard, selecting MFC, Explorer style and a splitter. The resulting project contains the standard classes one would expect, CMainFrame, a CDocument derivative, a CLeftView derived from CTreeView and another view class for the right hand side of the application.

The two views are managed by an instance of CSplitterWnd embedded in the CMainFrame object. All of this is MFC 101 material.

Given that I wanted to prevent resizing of the views via the splitter, it made sense to look at the MSDN documentation for CSplitterWnd. There are a bunch of functions in that class but not one to let you disable mouse operations on the splitter. Hmmm... A quick check on CodeProject under the Splitter Windows[^] topic offered nothing obvious. And then the penny dropped. A CSplitterWnd is a CWnd with some extra functionality.

Since CSplitterWnd is derived from CWnd, it's easy to intercept Windows messages. In this case, the windows message I was interested in is WM_NCHITTEST which is Windows way of asking a window, is the mouse over something special? In the case of a splitter window, the answer is yes if the mouse is over the splitter. If we change that answer to no, the extra logic in CSplitterWnd won't kick in.

The CSplitOverride Class

Here's the declaration. It's an extremely simple class containing only a constructor, a message map and a single message map entry.

C++
class CSplitOverride : public CSplitterWnd
{
    DECLARE_DYNAMIC(CSplitOverride)
public:
                    CSplitOverride() { }

protected:
    DECLARE_MESSAGE_MAP()
    afx_msg UINT    OnNcHitTest(CPoint point);
}

And here's the implementation:

C++
IMPLEMENT_DYNAMIC(CSplitOverride, CSplitterWnd)

BEGIN_MESSAGE_MAP(CSplitOverride, CSplitterWnd)
    ON_WM_NCHITTEST()
END_MESSAGE_MAP()

// CSplitOverride message handlers
afx_msg UINT CSplitOverride::OnNcHitTest(CPoint /*point*/)
{
    return HTNOWHERE;
}

Hey, I did say it was a trivial class! The override for the WM_NCHITTEST message handler simply returns HTNOWHERE which tells Windows there's nothing special about the location of the mouse pointer.

Using the Code

To use the class, add the two files in the download to your project and then, in the header for your CMainFrame class, change the CSplitterWnd variable type to CSplitOverride. Bingo! Your app will now ignore mouse operations on the splitter window. Don't forget to disable any menu or keyboard access to splitter resizing.

History

  • 25th February, 2004 - Initial version (and probably the last)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been programming for 35 years - started in machine language on the National Semiconductor SC/MP chip, moved via the 8080 to the Z80 - graduated through HP Rocky Mountain Basic and HPL - then to C and C++ and now C#.

I used (30 or so years ago when I worked for Hewlett Packard) to repair HP Oscilloscopes and Spectrum Analysers - for a while there I was the one repairing DC to daylight SpecAns in the Asia Pacific area.

Afterward I was the fourth team member added to the Australia Post EPOS project at Unisys Australia. We grew to become an A$400 million project. I wrote a few device drivers for the project under Microsoft OS/2 v 1.3 - did hardware qualification and was part of the rollout team dealing directly with the customer.

Born and bred in Melbourne Australia, now living in Scottsdale Arizona USA, became a US Citizen on September 29th, 2006.

I work for a medical insurance broker, learning how to create ASP.NET websites in VB.Net and C#. It's all good.

Oh, I'm also a Kentucky Colonel. http://www.kycolonels.org

Comments and Discussions

 
Questionwtl CSplitterWindow Pin
Member 1174856831-Jan-17 20:57
Member 1174856831-Jan-17 20:57 
GeneralA little enhancement Pin
wct8-Jan-09 16:47
wct8-Jan-09 16:47 
GeneralReally useful! Pin
nightbat200731-May-07 0:01
nightbat200731-May-07 0:01 
GeneralGood stuff Pin
neilsolent31-Jan-07 0:43
neilsolent31-Jan-07 0:43 
Good simple code.
Do you know any simple way of hiding the splitter itself ?


cheers,
Neil

GeneralExcellent! Pin
Aryana12-Apr-05 21:40
Aryana12-Apr-05 21:40 
Generalm_wndSplitter.EnableWindow(FALSE); Pin
PunkFloyd30-Dec-04 10:35
PunkFloyd30-Dec-04 10:35 
GeneralRe: m_wndSplitter.EnableWindow(FALSE); Pin
darkbrat7-May-05 2:17
darkbrat7-May-05 2:17 
Generalanother thing wrong with MFC Pin
jtorjo15-Jul-04 8:58
jtorjo15-Jul-04 8:58 
QuestionHow to resize views in a splitter window Pin
Priyank Bolia13-May-04 1:12
Priyank Bolia13-May-04 1:12 
GeneralI always felt there must be an easy solution for this Pin
Qibono6-Mar-04 4:44
Qibono6-Mar-04 4:44 
GeneralRe: I always felt there must be an easy solution for this Pin
Rob Manderson6-Mar-04 22:59
protectorRob Manderson6-Mar-04 22:59 
GeneralExcellent Work!!! Pin
AntMan25-Feb-04 10:57
AntMan25-Feb-04 10:57 
GeneralRe: Excellent Work!!! Pin
Vadim Tabakman25-Feb-04 17:05
Vadim Tabakman25-Feb-04 17:05 
GeneralAlready implemented Pin
Esko Lakso25-Feb-04 5:21
Esko Lakso25-Feb-04 5:21 
Generalfunny... Pin
Vadim Tabakman25-Feb-04 1:54
Vadim Tabakman25-Feb-04 1:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.