AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

用VC++实现应用程序窗口的任意分割

51自学网 2015-08-30 http://www.wanshiok.com

  二、编程步骤

  1、启动Visual C++6.0生成一个多文档应用程序Viewex,并添加支持分割的各个视图类;

  2、修改CViewExApp::InitInstance()函数,为应用程序添加多文档视图结构模板的支持;

  3、添加代码,编译运行程序。

  三、编程步骤

////////////////////////////////////////////////
BOOL CViewExApp::InitInstance()
{
 ……………………………
 // simple text output view
 AddDocTemplate(new CMultiDocTemplate(IDR_TEXTTYPE,
  RUNTIME_CLASS(CMainDoc),
  RUNTIME_CLASS(CMDIChildWnd),
  RUNTIME_CLASS(CTextView)));
 // simple color output view
 AddDocTemplate(new CMultiDocTemplate(IDR_COLORTYPE,
  RUNTIME_CLASS(CMainDoc),
  RUNTIME_CLASS(CMDIChildWnd),
  RUNTIME_CLASS(CColorView)));
 // form view with input
 AddDocTemplate(new CMultiDocTemplate(IDR_INPUTTYPE,
  RUNTIME_CLASS(CMainDoc),
  RUNTIME_CLASS(CMDIChildWnd),
  RUNTIME_CLASS(CInputView)));
 // splitter frame with both simple text output and form input view
 AddDocTemplate(new CMultiDocTemplate(IDR_SPLIT2TYPE,
  RUNTIME_CLASS(CMainDoc),
  RUNTIME_CLASS(CSplitterFrame),
  RUNTIME_CLASS(CTextView)));
 // 3-way splitter frame with form input, text output and color output views
 AddDocTemplate(new CMultiDocTemplate(IDR_SPLIT3TYPE,
  RUNTIME_CLASS(CMainDoc),
  RUNTIME_CLASS(C3WaySplitterFrame),
  RUNTIME_CLASS(CInputView)));
 CMDIFrameWnd* pMainFrame = new CMDIFrameWnd;
 if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  return FALSE;
 // Now finally show the main menu
 pMainFrame->ShowWindow(m_nCmdShow);
 pMainFrame->UpdateWindow();
 m_pMainWnd = pMainFrame;
 OnFileNew();
 return TRUE;
}

//////////////////////////////////////////CinputView类的头文件
class CInputView : public CFormView
{
 DECLARE_DYNCREATE(CInputView)
 protected:
  CInputView(); // protected constructor used by dynamic creation
  // Form Data
 public:
  //{{AFX_DATA(CInputView)
   enum { IDD = IDD_INPUTFORM };
   CString m_strData;
   int m_iColor;
  //}}AFX_DATA
  // Attributes
 public:
  CMainDoc* GetDocument()
  {
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMainDoc)));
   return (CMainDoc*) m_pDocument;
  }
  // Operations
 public:
  // Implementation
 protected:
  virtual ~CInputView();
  virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  virtual void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint);
  // Generated message map functions
  //{{AFX_MSG(CInputView)
   afx_msg void OnDataChange();
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////// CInputView类实现文件
#include "stdafx.h"
#include "viewex.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNCREATE(CInputView, CFormView)
CInputView::CInputView()
: CFormView(CInputView::IDD)
{
 //{{AFX_DATA_INIT(CInputView)
  m_strData = "";
  m_iColor = -1;
 //}}AFX_DATA_INIT
}

CInputView::~CInputView()
{}

void CInputView::OnUpdate(CView*, LPARAM, CObject*)
{
 CMainDoc* pDoc = GetDocument();
 m_strData = pDoc->m_strData;
 if (pDoc->m_colorData == RGB(255, 0, 0))
  m_iColor = 0;
 else if (pDoc->m_colorData == RGB(0, 255, 0))
  m_iColor = 1;
 else if (pDoc->m_colorData == RGB(0, 0, 255))
  m_iColor = 2;
 else
  m_iColor = -1;
 TRACE2("OnUpdate: m_iColor = %d ($%lx)/n", m_iColor, pDoc->m_colorData);
 UpdateData(FALSE); // set the data into the controls
}

void CInputView::DoDataExchange(CDataExchange* pDX)
{
 CFormView::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CInputView)
   DDX_Text(pDX, IDC_EDIT1, m_strData);
   DDX_Radio(pDX, IDC_RADIO1, m_iColor);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInputView, CFormView)
 //{{AFX_MSG_MAP(CInputView)
  ON_EN_CHANGE(IDC_EDIT1, OnDataChange)
  ON_BN_CLICKED(IDC_RADIO1, OnDataChange)
  ON_BN_CLICKED(IDC_RADIO2, OnDataChange)
  ON_BN_CLICKED(IDC_RADIO3, OnDataChange)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CInputView::OnDataChange()// CInputView message handlers
{
 if (!UpdateData())
  return;
 CMainDoc* pDoc = GetDocument();
 COLORREF color = RGB(255 * (m_iColor == 0),
 255 * (m_iColor == 1),
 255 * (m_iColor == 2));
 BOOL bUpdate = FALSE;
 if (m_strData != pDoc->m_strData)
 {
  pDoc->m_strData = m_strData;
  bUpdate = TRUE;
 }
 if (color != pDoc->m_colorData)
 {
  pDoc->m_colorData = color;
  bUpdate = TRUE;
 }
 if (bUpdate)
 {
  // if the document stored data then we would call SetModifiedFlag here
  pDoc->UpdateAllViews(this);
 }
}
/////////////////////////////////////////////////////simpvw.h文件
class CTextView : public CView
{
 protected: // create from serialization only
  CTextView();
  DECLARE_DYNCREATE(CTextView)
  // Attributes
 public:
  CMainDoc* GetDocument()
  {
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMainDoc)));
   return (CMainDoc*) m_pDocument;
  }
  // Operations
 public:
  // Implementation
 public:
  virtual ~CTextView();
  virtual void OnDraw(CDC* pDC); // overridden to draw this view
  // Generated message map functions
 protected:
  //{{AFX_MSG(CTextView)
   afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};
class CColorView : public CView
{
 protected: // create from serialization only
  CColorView();
  DECLARE_DYNCREATE(CColorView)
  // Attributes
 public:
  CMainDoc* GetDocument()
  {
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMainDoc)));
   return (CMainDoc*) m_pDocument;
  }

  // Operations
 public:
  // Implementation
 public:
  virtual ~CColorView();
  virtual void OnDraw(CDC* pDC); // overridden to draw this view
  virtual void OnActivateView(BOOL bActivate, CView* pActivateView,
  CView* pDeactiveView);
  // Generated message map functions
 protected:
  //{{AFX_MSG(CColorView)
   afx_msg int OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message);
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
////////////////////////////////simpvw.cpp文件;
#include "stdafx.h"
#include "viewex.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////CTextView
IMPLEMENT_DYNCREATE(CTextView, CView)
BEGIN_MESSAGE_MAP(CTextView, CView)
 //{{AFX_MSG_MAP(CTextView)
  ON_WM_MOUSEACTIVATE()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CTextView construction/destruction
CTextView::CTextView()
{}

CTextView::~CTextView()
{}

void CTextView::OnDraw(CDC* pDC)
{
 CMainDoc* pDoc = GetDocument();
 CRect rect;
 GetClientRect(rect);
 pDC->SetTextAlign(TA_BASELINE | TA_CENTER);
 pDC->SetTextColor(pDoc->m_colorData);
 pDC->SetBkMode(TRANSPARENT);
 // center in the window
 pDC->TextOut(rect.Width() / 2, rect.Height() / 2,
 pDoc->m_strData, pDoc->m_strData.GetLength());
}

int CTextView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
 // side-step CView's implementation since we don't want to activate
 // this view
 return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}
////////////////////////// CColorView
IMPLEMENT_DYNCREATE(CColorView, CView)
BEGIN_MESSAGE_MAP(CColorView, CView)
 //{{AFX_MSG_MAP(CColorView)
  ON_WM_MOUSEACTIVATE()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CColorView construction/destruction
CColorView::CColorView()
{}
CColorView::~CColorView()
{}

void CColorView::OnDraw(CDC* pDC)
{
 CMainDoc* pDoc = GetDocument();
 CRect rect;
 GetClientRect(rect);
 // fill the view with the specified color
 CBrush br(pDoc->m_colorData);
 pDC->FillRect(rect, &br);
}

int CColorView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
 // side-step CView's implementation since we don't want to activate
 // this view
 return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}

void CColorView::OnActivateView(BOOL, CView*, CView*)
{
 ASSERT(FALSE); // output only view - should never be active
}
///////////////////////////////////////// splitter.h文件;
// CSplitterFrame frame with splitter/wiper
class CSplitterFrame : public CMDIChildWnd
{
 DECLARE_DYNCREATE(CSplitterFrame)
 protected:
  CSplitterFrame(); // protected constructor used by dynamic creation
  // Attributes
 protected:
  CSplitterWnd m_wndSplitter;
  // Implementation
 public:
  virtual ~CSplitterFrame();
  virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
  // Generated message map functions
  //{{AFX_MSG(CSplitterFrame)
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

class CViewExSplitWnd : public CSplitterWnd
{
 DECLARE_DYNAMIC(CViewExSplitWnd)
 // Implementation
 public:
  CViewExSplitWnd();
  ~CViewExSplitWnd();
  CWnd* GetActivePane(int* pRow = NULL, int* pCol = NULL);
};
class C3WaySplitterFrame : public CMDIChildWnd
{
 DECLARE_DYNCREATE(C3WaySplitterFrame)
 protected:
  C3WaySplitterFrame(); // protected constructor used by dynamic creation
  // Attributes
 protected:
  CViewExSplitWnd m_wndSplitter;
  CViewExSplitWnd m_wndSplitter2; // embedded in the first
  // Implementation
 public:
  virtual ~C3WaySplitterFrame();
  virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
  // Generated message map functions
  //{{AFX_MSG(C3WaySplitterFrame)
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};
/////////////////////////////splitter.cpp文件;
#include "stdafx.h"
#include "viewex.h"
#include "splitter.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
// CSplitterFrame
// Create a splitter window which splits an output text view and an input view
// |
// TEXT VIEW (CTextView) | INPUT VIEW (CInputView)
// |
IMPLEMENT_DYNCREATE(CSplitterFrame, CMDIChildWnd)
CSplitterFrame::CSplitterFrame()
{}

CSplitterFrame::~CSplitterFrame()
{}
BOOL CSplitterFrame::OnCreateClient(LPCREATESTRUCT,
CCreateContext* pContext)
{
 // create a splitter with 1 row, 2 columns
 if (!m_wndSplitter.CreateStatic(this, 1, 2))
 {
  TRACE0("Failed to CreateStaticSplitter/n");
  return FALSE;
 }
 // add the first splitter pane - the default view in column 0
 if (!m_wndSplitter.CreateView(0, 0,pContext->m_pNewViewClass, CSize(130, 50), pContext))
 {
  TRACE0("Failed to create first pane/n");
  return FALSE;
 }
 // add the second splitter pane - an input view in column 1
 if (!m_wndSplitter.CreateView(0, 1,RUNTIME_CLASS(CInputView), CSize(0, 0), pContext))
 {
  TRACE0("Failed to create second pane/n");
  return FALSE;
 }
 // activate the input view
 SetActiveView((CView*)m_wndSplitter.GetPane(0,1));
 return TRUE;
}
BEGIN_MESSAGE_MAP(CSplitterFrame, CMDIChildWnd)
 //{{AFX_MSG_MAP(CSplitterFrame)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
// C3WaySplitterFrame - just like CSplitterFrame except the input view is
// the first pane, and there are two output views

// | Text View (CTextView)
// INPUT VIEW (CInputView) |------------------------
// | Color View (CColorView)
IMPLEMENT_DYNCREATE(C3WaySplitterFrame, CMDIChildWnd)
C3WaySplitterFrame::C3WaySplitterFrame()
{}

C3WaySplitterFrame::~C3WaySplitterFrame()
{}

BOOL C3WaySplitterFrame::OnCreateClient(LPCREATESTRUCT lpcs,CCreateContext* pContext)
{
 // create a splitter with 1 row, 2 columns
 if (!m_wndSplitter.CreateStatic(this, 1, 2))
 {
  TRACE0("Failed to CreateStaticSplitter/n");
  return FALSE;
 }
 // add the first splitter pane - the default view in column 0
 if (!m_wndSplitter.CreateView(0, 0,pContext->m_pNewViewClass, CSize(200, 50), pContext))
 {
  TRACE0("Failed to create first pane/n");
  return FALSE;
 }
 // add the second splitter pane - which is a nested splitter with 2 rows
 if (!m_wndSplitter2.CreateStatic(&m_wndSplitter, // our parent window is the first splitter
2, 1, // the new splitter is 2 rows, 1 column
WS_CHILD | WS_VISIBLE | WS_BORDER, // style, WS_BORDER is needed
m_wndSplitter.IdFromRowCol(0, 1) ))
// new splitter is in the first row, 2nd column of first splitter
{
 TRACE0("Failed to create nested splitter/n");
 return FALSE;
}
// now create the two views inside the nested splitter
int cyText = max(lpcs->cy - 70, 20); // height of text pane
if (!m_wndSplitter2.CreateView(0, 0,RUNTIME_CLASS(CTextView), CSize(0, cyText), pContext))
{
 TRACE0("Failed to create second pane/n");
 return FALSE;
}
if (!m_wndSplitter2.CreateView(1, 0,RUNTIME_CLASS(CColorView), CSize(0, 0), pContext))
{
 TRACE0("Failed to create third pane/n");
 return FALSE;
}
// it all worked, we now have two splitter windows which contain
// three different views
return TRUE;
}
BEGIN_MESSAGE_MAP(C3WaySplitterFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(C3WaySplitterFrame)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CViewExSplitWnd, CSplitterWnd)
CViewExSplitWnd::CViewExSplitWnd()
{}

CViewExSplitWnd::~CViewExSplitWnd()
{}

CWnd* CViewExSplitWnd::GetActivePane(int* pRow, int* pCol)
{
 ASSERT_VALID(this);
 // attempt to use active view of frame window
 CWnd* pView = NULL;
 CFrameWnd* pFrameWnd = GetParentFrame();
 ASSERT_VALID(pFrameWnd);
 pView = pFrameWnd->GetActiveView();
 // failing that, use the current focus
 if (pView == NULL)
  pView = GetFocus();
 return pView;
}


 

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:用VC++制作QQ自动登陆软件  下一篇:VC++实现显示透明的256色以上的图标