MFC基础知识(六)——停靠窗口中添加控件

本文主要介绍:在MFC创建的停靠窗口中添加一些控件,浮动窗口中可以添加MFC自身的控件,也可以添加对话框。
一、创建对话框
对话框属性做以下修改:

二、窗口中添加控件
ObjectWnd.h文件:

#pragma once
#include "afxdockablepane.h"
#include "ObjectDlg.h"
#include "afxwin.h"

class CObjectWnd : public CDockablePane {
public:
    CObjectWnd(void);
    ~CObjectWnd(void);
    DECLARE_MESSAGE_MAP()
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnDestroy();


    CEdit m_edit;
    CStatic m_static;
    CObjectDlg m_objectDlg; //对话框类

    void AdjustLayout();

};

ObjectWnd.cpp文件:

#include "stdafx.h"
#include "ObjectWnd.h"
#include "resource.h"

CObjectWnd::CObjectWnd(void)
{
}


CObjectWnd::~CObjectWnd(void)
{
}

BEGIN_MESSAGE_MAP(CObjectWnd, CDockablePane)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_DESTROY()
END_MESSAGE_MAP()


void CObjectWnd::AdjustLayout()
{
    if (GetSafeHwnd () == NULL || (AfxGetMainWnd() != NULL && AfxGetMainWnd()->IsIconic())) { return; } CRect rectClient; GetClientRect(rectClient); int height = rectClient.Height()/3; //控件在窗口中所占空间大小 m_edit.SetWindowPos(this,rectClient.left,rectClient.top,rectClient.Width(),rectClient.Height()/3,SWP_NOACTIVATE | SWP_NOZORDER); m_static.SetWindowPos(this,rectClient.left,rectClient.top+height,rectClient.Width(),rectClient.Height()/3,SWP_NOACTIVATE | SWP_NOZORDER); m_objectDlg.SetWindowPos(this,rectClient.left,rectClient.top+height*2,rectClient.Width(),rectClient.Height()/3,SWP_NOACTIVATE | SWP_NOZORDER); } int CObjectWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDockablePane::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty(); // 创建组合: const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; if (!m_edit.Create(dwViewStyle, rectDummy, this, 1)) { TRACE0("未能创建CEdit控件 \n"); return -1; // 未能创建 } if(!m_static.Create(NULL,dwViewStyle,rectDummy,this,6)) { TRACE0("未能创建CStatic控件\n"); return -1; // 未能创建 } // 创建对话框窗口: if (!m_objectDlg.Create(IDD_ObjectDlg,this)) { TRACE0("未能创建对话框窗口\n"); return -1; // 未能创建 } m_objectDlg.ShowWindow(SW_SHOW); AdjustLayout(); //m_edit.SetWindowText(_T("CEdit控件")); //m_static.SetWindowText(_T("CStatic控件")); return 0; } void CObjectWnd::OnSize(UINT nType, int cx, int cy) { CDockablePane::OnSize(nType, cx, cy); AdjustLayout(); } void CObjectWnd::OnDestroy() { CDockablePane::OnDestroy(); m_objectDlg.DestroyWindow(); // TODO: 在此处添加消息处理程序代码 } 

三、添加完成,运行如下:

你可能感兴趣的:(mfc,添加控件,停靠窗口,停靠视图)