⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custsplt.cpp

📁 VC分割窗口源程序
💻 CPP
字号:
#include "stdafx.h"
#include "custsplt.h"




BEGIN_MESSAGE_MAP(CCustSplitterWnd, CSplitterWnd)
        //{{AFX_MSG_MAP(CCustSplitterWnd)
    ON_WM_LBUTTONDOWN()
    ON_WM_KEYDOWN()
    ON_WM_LBUTTONUP()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

IMPLEMENT_DYNAMIC(CCustSplitterWnd, CSplitterWnd);









BOOL CCustSplitterWnd::Create( CWnd* pParentWnd, int nMaxRows, int nMaxCols, SIZE sizeMin, CCreateContext* pContext, DWORD dwStyle /*= WS_CHILD | WS_VISIBLE |WS_HSCROLL | WS_VSCROLL | SPLS_DYNAMIC_SPLIT*/, UINT nID /*= AFX_IDW_PANE_FIRST*/ )
{
ASSERT (dwStyle&SPLS_DYNAMIC_SPLIT);//You don't think you can remove splitter windows on static splitter windows, do you?

if (m_bSplit)
    dwStyle |= WS_HSCROLL|WS_VSCROLL;
else
    dwStyle &= ~(WS_HSCROLL|WS_VSCROLL);

return CSplitterWnd::Create(pParentWnd,nMaxRows, nMaxCols,sizeMin, pContext,dwStyle);
}








BOOL CCustSplitterWnd::DoKeyboardSplit( )
{
SetScrollStyle( WS_HSCROLL | WS_VSCROLL  );

m_bSplit=TRUE;

BOOL bReturn=CSplitterWnd::DoKeyboardSplit( );

return bReturn;
} 




#ifndef _WIN32
void CCustSplitterWnd::SetScrollStyle( DWORD dwStyle  )
{
    int iRows=GetRowCount(), iColumns=GetColumnCount();           
    CWnd *pScrollBar;
    
    dwStyle &= (WS_HSCROLL|WS_VSCROLL);

    // update to new state
    m_bHasHScroll = (dwStyle & WS_HSCROLL) != 0;
    m_bHasVScroll = (dwStyle & WS_VSCROLL) != 0;


    if (!m_bHasHScroll)
        {//no horizontal scrolling, destroy all horizontal scrollbars
        for (int col = 0; col < iColumns; col++)
            {
            pScrollBar = GetDlgItem(AFX_IDW_HSCROLL_FIRST + col);
            if (pScrollBar != NULL)
                pScrollBar->DestroyWindow();
            }
        }
    // show/hide all the shared vert scroll bars
    if (!m_bHasVScroll)
        {//no vertical scrolling, destroy all vertical scrollbars
        for (int row = 0; row < iRows; row++)
            {
            pScrollBar = GetDlgItem(AFX_IDW_VSCROLL_FIRST + row);
            if (pScrollBar != NULL)
                pScrollBar->DestroyWindow();
            }
        }
    if (!m_bHasVScroll && !m_bHasHScroll)
        {   
        pScrollBar = GetDlgItem(AFX_IDW_SIZE_BOX);
        if (pScrollBar != NULL)
            pScrollBar->DestroyWindow();
        }


    if (m_bHasHScroll)
        for (int col = 0; col < m_nCols; col++)
            {                                               
            if (!GetDlgItem(AFX_IDW_HSCROLL_FIRST + col))
                {
                if (!CreateScrollBarCtrl(SBS_HORZ, AFX_IDW_HSCROLL_FIRST + col))
                    AfxThrowResourceException();
                }   
            }   
                
    if (m_bHasVScroll)
        for (int row = 0; row < m_nRows; row++)
            {
            if (!GetDlgItem(AFX_IDW_VSCROLL_FIRST + row))
                {
                if (!CreateScrollBarCtrl(SBS_VERT, AFX_IDW_VSCROLL_FIRST + row))
                    AfxThrowResourceException();
                }
            }
            
    if (m_bHasHScroll && m_bHasVScroll)
        {
        if (!GetDlgItem(AFX_IDW_SIZE_BOX))
            {
            if (!CreateScrollBarCtrl(SBS_SIZEBOX|WS_DISABLED, AFX_IDW_SIZE_BOX))
                AfxThrowResourceException();
            }   
        }   

    RecalcLayout();
}           
#endif






void CCustSplitterWnd::OnSplit() 
{
CWnd *lpParent=GetParent();
ASSERT (lpParent!=NULL);
ASSERT (lpParent->IsKindOf(RUNTIME_CLASS(CFrameWnd)));



SetScrollStyle(m_bSplit?0:WS_HSCROLL|WS_VSCROLL);
CWnd* pScrollBar=NULL;

CView *pView=((CFrameWnd *)lpParent)->GetActiveView();

if (m_bSplit)
    {
    int iRows=GetRowCount(), iColumns=GetColumnCount();           
#ifdef _WIN32
    SetActivePane(0,0);
#else
    CView *pViewNew=(CView *)GetPane(0,0);
    ASSERT(pViewNew);
    ((CFrameWnd *)lpParent)->SetActiveView(pViewNew);
#endif    
    if (iRows>1)
        DeleteRow(1);
    if (iColumns>1)
        DeleteColumn(1);
    RecalcLayout();


    for (int col = 0; col < iColumns; col++)
    {
        pScrollBar = GetDlgItem(AFX_IDW_HSCROLL_FIRST + col);
        if (pScrollBar != NULL)
            pScrollBar->DestroyWindow();
    }

    // show/hide all the shared vert scroll bars
    for (int row = 0; row < iRows; row++)
    {
        pScrollBar = GetDlgItem(AFX_IDW_VSCROLL_FIRST + row);
        if (pScrollBar != NULL)
            pScrollBar->DestroyWindow();
    }

#ifdef _WIN32
    pView = (CView *)GetActivePane();
#else
    pView = ((CFrameWnd *)lpParent)->GetActiveView();
    //can now be another pane because the previously active view might be destroyed now.
#endif    
    if (pView && pView->IsKindOf(RUNTIME_CLASS(CScrollView)))
        {
        int nMapMode;
        SIZE sizeTotal, sizePage, sizeLine;
        ((CScrollView *)pView)->GetDeviceScrollSizes( nMapMode, sizeTotal, sizePage, sizeLine);
        //store the scrolling information in stack variables to be able to
        //restore them appropriately.
        
        

        WINDOWPLACEMENT wp;
        lpParent->GetWindowPlacement(&wp);
        lpParent->LockWindowUpdate();
        



        ((CScrollView *)pView)->SetScrollSizes(nMapMode, sizeTotal, sizePage, sizeLine);
        //now restore the scrolling information 

#ifdef _WIN32
        if (wp.showCmd!=SW_SHOWMAXIMIZED)
            ((CScrollView *)pView)->ResizeParentToFit(TRUE);
#endif

        lpParent->SetWindowPlacement(&wp);

#ifdef _WIN32
        lpParent->UnlockWindowUpdate();
#else                                        
        ::LockWindowUpdate(NULL);
#endif
        }
    m_bSplit=FALSE; 
    }   
else
    {
    // show/hide all the shared horz scroll bars
    if (pView && pView->IsKindOf(RUNTIME_CLASS(CScrollView)))
        {
        ((CScrollView *)pView)->GetDeviceScrollSizes( m_nMapMode, m_sizeTotal, m_sizePage, m_sizeLine);
        ((CScrollView *)pView)->SetScrollSizes(m_nMapMode, CSize(0,0), m_sizePage, m_sizeLine);        
        //This removes some weird scrollbars inside the splitter wnds that already have
        //scrollbars. Note that these are different effects on Win16 and Win32 but are both solved
        //through this line. On Win16 there are weird scrollbars inside scrollbars during tracking, 
        //whereas on Win32 there are double scrollbars in the first pane (top left) after tracking .
        }
    DoKeyboardSplit();
    m_bSplit=TRUE;  
    }   
    
}

void CCustSplitterWnd::OnUpdateSplit(CCmdUI* pCmdUI) 
{
pCmdUI->SetCheck(m_bSplit); 
}





void CCustSplitterWnd::OnLButtonDown(UINT nFlags, CPoint point) 
{
ASSERT (GetParent()!=NULL);

ASSERT (GetParent()->IsKindOf(RUNTIME_CLASS(CFrameWnd)));

CView *pActiveView=((CFrameWnd *)GetParent())->GetActiveView();
ASSERT(pActiveView);
if (pActiveView->IsKindOf(RUNTIME_CLASS(CScrollView)) && m_bTracking)
    ((CScrollView *)pActiveView)->SetScrollSizes(m_nMapMode,m_sizeTotal,m_sizePage,m_sizeLine);
    //This makes the scrollbars have the correct size again.

CSplitterWnd::OnLButtonDown(nFlags, point);
}


void CCustSplitterWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
ASSERT (GetParent()!=NULL);

ASSERT (GetParent()->IsKindOf(RUNTIME_CLASS(CFrameWnd)));

CView *pActiveView=((CFrameWnd *)GetParent())->GetActiveView();
ASSERT(pActiveView);
if ( pActiveView->IsKindOf(RUNTIME_CLASS(CScrollView)) && m_bTracking )
    {
    if (nChar == VK_RETURN || nChar == VK_ESCAPE)
        {
        ((CScrollView *)pActiveView)->SetScrollSizes(m_nMapMode,m_sizeTotal,m_sizePage,m_sizeLine);
        //This makes the scrollbars have the correct size again.
        RecalcLayout();
        //if panes would be too small, no additional panes are created. This RecalcLayout() is necessary in
        //this case to show the scrollbars correctly. Note that in case of splitting with a left mouse click this
        //RecalcLayout is done in the WM_LEFTBUTTONUP handler *after* the default implementation that must invert 
        //the tracker.
        }
    }
CSplitterWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}



void CCustSplitterWnd::OnLButtonUp(UINT nFlags, CPoint point) 
{
    BOOL bTracking = m_bTracking;

    CSplitterWnd::OnLButtonUp(nFlags, point);//let the default implementation invert the tracker and split.
        
    if (bTracking )
    {
        RecalcLayout();
        //if panes would be too small, no additional panes are created. This RecalcLayout() is necessary in
        //this case to show the scrollbars correctly. Note that this RecalcLayout is done *after* the default 
        //implementation that must invert the tracker.
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -