---如何从CContolBar派生自己的CMyControlBar?

digerati 2004-03-24 11:49:08
俺的CMyControlBar重载了三个函数:
CMyControlBar:Public CControlBar
{
...
//

BOOL Create( CWnd* pParentWnd, DWORD dwStyle, UINT nID );
BOOL CreateEx(CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle, UINT nID);
void OnUpdateCmdUI(CFrameWnd *wnd,int n);
...

}

CMyControlBar.cpp里面:

BOOL CMyControlBar::CreateEx(CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle, UINT nID)
{
ASSERT_VALID(pParentWnd);

m_dwStyle = (dwStyle & CBRS_ALL);

// translate MFC style bits to windows style bits
dwStyle &= ~CBRS_ALL;
dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE;
if (pParentWnd->GetStyle() & WS_THICKFRAME)
dwStyle |= SBARS_SIZEGRIP;
dwStyle |= dwCtrlStyle;

CString str=AfxRegisterWndClass(CS_DBLCLKS,
NULL,(HBRUSH)(COLOR_3DFACE+1),NULL);
CRect rect;
rect.SetRectEmpty ();
return CWnd::Create(str, NULL, dwStyle, rect, pParentWnd, nID);
}


BOOL CMyControlBar::Create( CWnd* pParentWnd, DWORD dwStyle, UINT nID )
{
m_dwStyle = dwStyle;
return CreateEx (pParentWnd,0,m_dwStyle,nID);
}


然后,我在框架窗体中:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
......
//m_Bar是CMainFrame里面的一个成员:
//CMyControlBar m_Bar;
....
m_Bar.Create(this,WS_CHILD | WS_VISIBLE | CBRS_RIGHT
| CBRS_GRIPPER | CBRS_FLYBY | CBRS_SIZE_DYNAMIC,AFX_IDW_CONTROLBAR_LAST );
.....
}

预想的效果是Frame右边会出现一个MyControlBar,结果什么都没有:(
...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
guigui98 2004-03-24
  • 打赏
  • 举报
回复
还没有完呢,程序太长呢,如果再要,发邮件guiguizk@163.com
digerati 2004-03-24
  • 打赏
  • 举报
回复
非常感谢楼上两位。。。我试试
guigui98 2004-03-24
  • 打赏
  • 举报
回复
接上:
CSize CCoolDialogBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
CRect rc;

m_pDockSite->GetControlBar(AFX_IDW_DOCKBAR_TOP)->GetWindowRect(rc);
int nHorzDockBarWidth = bStretch ? 32767 : rc.Width() + 4;
m_pDockSite->GetControlBar(AFX_IDW_DOCKBAR_LEFT)->GetWindowRect(rc);
int nVertDockBarHeight = bStretch ? 32767 : rc.Height() + 4;

if(IsFloating())
return m_sizeFloat;
else if (bHorz)
return CSize(nHorzDockBarWidth, m_sizeHorz.cy);
else
return CSize(m_sizeVert.cx, nVertDockBarHeight);
}

CSize CCoolDialogBar::CalcDynamicLayout(int nLength, DWORD dwMode)
{
// Rob Wolpov 10/15/98 Added support for diagonal sizing
if (IsFloating())
{
// Enable diagonal arrow cursor for resizing
GetParent()->GetParent()->ModifyStyle(MFS_4THICKFRAME,0);
}
if (dwMode & (LM_HORZDOCK | LM_VERTDOCK))
{
SetWindowPos(NULL, 0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER |
SWP_NOACTIVATE | SWP_FRAMECHANGED);
m_pDockSite->RecalcLayout();
return CControlBar::CalcDynamicLayout(nLength,dwMode);
}

if (dwMode & LM_MRUWIDTH)
return m_sizeFloat;

if (dwMode & LM_COMMIT)
{
m_sizeFloat.cx = nLength;
return m_sizeFloat;
}

// Rob Wolpov 10/15/98 Added support for diagonal sizing
if (IsFloating())
{
RECT window_rect;
POINT cursor_pt;

GetCursorPos(&cursor_pt);
GetParent()->GetParent()->GetWindowRect(&window_rect);

switch (m_pDockContext->m_nHitTest)
{
case HTTOPLEFT:
m_sizeFloat.cx = max(window_rect.right - cursor_pt.x,
m_cMinWidth) - m_cxBorder;
m_sizeFloat.cy = max(window_rect.bottom - m_cCaptionSize -
cursor_pt.y,m_cMinHeight) - 1;
m_pDockContext->m_rectFrameDragHorz.top = min(cursor_pt.y,
window_rect.bottom - m_cCaptionSize - m_cMinHeight) -
m_cyBorder;
m_pDockContext->m_rectFrameDragHorz.left = min(cursor_pt.x,
window_rect.right - m_cMinWidth) - 1;
return m_sizeFloat;

case HTTOPRIGHT:
m_sizeFloat.cx = max(cursor_pt.x - window_rect.left,
m_cMinWidth);
m_sizeFloat.cy = max(window_rect.bottom - m_cCaptionSize -
cursor_pt.y,m_cMinHeight) - 1;
m_pDockContext->m_rectFrameDragHorz.top = min(cursor_pt.y,
window_rect.bottom - m_cCaptionSize - m_cMinHeight) -
m_cyBorder;
return m_sizeFloat;

case HTBOTTOMLEFT:
m_sizeFloat.cx = max(window_rect.right - cursor_pt.x,
m_cMinWidth) - m_cxBorder;
m_sizeFloat.cy = max(cursor_pt.y - window_rect.top -
m_cCaptionSize,m_cMinHeight);
m_pDockContext->m_rectFrameDragHorz.left = min(cursor_pt.x,
window_rect.right - m_cMinWidth) - 1;
return m_sizeFloat;

case HTBOTTOMRIGHT:
m_sizeFloat.cx = max(cursor_pt.x - window_rect.left,
m_cMinWidth);
m_sizeFloat.cy = max(cursor_pt.y - window_rect.top -
m_cCaptionSize,m_cMinHeight);
return m_sizeFloat;
}
}

if (dwMode & LM_LENGTHY)
return CSize(m_sizeFloat.cx,
m_sizeFloat.cy = max(m_sizeMin.cy, nLength));
else
return CSize(max(m_sizeMin.cx, nLength), m_sizeFloat.cy);
}

guigui98 2004-03-24
  • 打赏
  • 举报
回复
2.CoolDialogbar.cpp:
// CoolDialogBar.cpp : implementation file
//

#include "stdafx.h"
#include "afxpriv.h" // for CDockContext
#include "CoolDialogBar.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CCoolDialogBar

CCoolDialogBar::CCoolDialogBar() :
m_clrBtnHilight(::GetSysColor(COLOR_BTNHILIGHT)),
m_clrBtnShadow(::GetSysColor(COLOR_BTNSHADOW))
{
m_sizeMin = CSize(32, 32);
m_sizeHorz = CSize(200, 200);
m_sizeVert = CSize(200, 200);
m_sizeFloat = CSize(200, 200);
m_bTracking = FALSE;
m_bInRecalcNC = FALSE;
m_cxEdge = 6;
m_cxBorder = 3;
m_cxGripper = 20;
m_pCtrlWnd = NULL;
m_brushBkgd.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));

// Rob Wolpov 10/15/98 Added support for diagonal resizing
m_cyBorder = 3;
m_cCaptionSize = GetSystemMetrics(SM_CYSMCAPTION);
m_cMinWidth = GetSystemMetrics(SM_CXMIN);
m_cMinHeight = GetSystemMetrics(SM_CYMIN);
}

CCoolDialogBar::~CCoolDialogBar()
{
}

IMPLEMENT_DYNAMIC(CCoolDialogBar, CControlBar)

BEGIN_MESSAGE_MAP(CCoolDialogBar, CControlBar)
//{{AFX_MSG_MAP(CCoolDialogBar)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_WM_WINDOWPOSCHANGED()
ON_WM_NCPAINT()
ON_WM_NCLBUTTONDOWN()
ON_WM_NCHITTEST()
ON_WM_NCCALCSIZE()
ON_WM_LBUTTONDOWN()
ON_WM_CAPTURECHANGED()
ON_WM_LBUTTONDBLCLK()
ON_WM_NCLBUTTONDBLCLK()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCoolDialogBar message handlers

void CCoolDialogBar::OnUpdateCmdUI(class CFrameWnd *pTarget, int bDisableIfNoHndler)
{
UpdateDialogControls(pTarget, bDisableIfNoHndler);
}

BOOL CCoolDialogBar::Create(CWnd* pParentWnd, LPCTSTR pTitle, DWORD dwStyle)
{
ASSERT_VALID(pParentWnd); // must have a parent
ASSERT (!((dwStyle & CBRS_SIZE_FIXED) && (dwStyle & CBRS_SIZE_DYNAMIC)));

// save the style -- AMENDED by Holger Thiele - Thankyou
m_dwStyle = dwStyle & CBRS_ALL;

// create the base window
CString wndclass = AfxRegisterWndClass(CS_DBLCLKS, LoadCursor(NULL, IDC_ARROW),
m_brushBkgd, 0);
if (!CWnd::Create(wndclass, pTitle, dwStyle, CRect(0,0,0,0),
pParentWnd, 0))
return FALSE;

// create the child dialog
// m_pCtrlWnd = pCtrlWnd;
// m_pCtrlWnd->Create(nID, this);

// use the dialog dimensions as default base dimensions
// CRect rc;
// m_pCtrlWnd->GetWindowRect(rc);
// m_sizeHorz = m_sizeVert = m_sizeFloat = CSizerc.Size();
m_sizeHorz.cy += m_cxEdge + m_cxBorder;
m_sizeVert.cx += m_cxEdge + m_cxBorder;
//测试
//if (!m_cTreeCtrl.Create(WS_VISIBLE|WS_BORDER|CBRS_SIZE_DYNAMIC,CRect(0,0,200,200),this,0x1008))
//{
// TRACE("ERRor");
// return -1;
//}
return TRUE;
}

guigui98 2004-03-24
  • 打赏
  • 举报
回复
请参看以下代码:CCoolDialogbar.h 和CoolDialogBar.cpp
1。CCoolDialogbar.h:
class CCoolDialogBar : public CControlBar
{
const COLORREF m_clrBtnHilight;
const COLORREF m_clrBtnShadow;

DECLARE_DYNAMIC(CCoolDialogBar);
// Construction / destruction
public:
CCoolDialogBar();
CTreeCtrl m_cTreeCtrl; //测试的
// Attributes
public:
BOOL IsHorz() const;

// Operations
public:

// Overridables
virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler);

// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCoolDialogBar)
public:
virtual BOOL Create(CWnd* pParentWnd, LPCTSTR pTitle, DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_LEFT);
virtual CSize CalcFixedLayout( BOOL bStretch, BOOL bHorz );
virtual CSize CalcDynamicLayout( int nLength, DWORD dwMode );

//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CCoolDialogBar();
void StartTracking();
void StopTracking(BOOL bAccept);
void OnInvertTracker(const CRect& rect);

// implementation helpers
CPoint& ClientToWnd(CPoint& point);

protected:
void DrawGripper(CDC &dc);

CSize m_sizeMin;
CSize m_sizeHorz;
CSize m_sizeVert;
CSize m_sizeFloat;
CRect m_rectBorder;
CRect m_rectTracker;
UINT m_nDockBarID;
CPoint m_ptOld;
BOOL m_bTracking;
BOOL m_bInRecalcNC;
int m_cxEdge;
CRect m_rectUndock;
CRect m_rectClose;
CRect m_rectGripper;
int m_cxGripper;
int m_cxBorder;
CWnd* m_pCtrlWnd;
CBrush m_brushBkgd;

// Rob Wolpov 10/15/98 Added support for diagonal resizing
int m_cyBorder;
int m_cMinWidth;
int m_cMinHeight;
int m_cCaptionSize;

// Generated message map functions
protected:
//{{AFX_MSG(CCoolDialogBar)
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnWindowPosChanged(WINDOWPOS FAR* lpwndpos);
afx_msg void OnNcPaint();
afx_msg void OnNcLButtonDown(UINT nHitTest, CPoint point);
afx_msg UINT OnNcHitTest(CPoint point);
afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnCaptureChanged(CWnd *pWnd);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnNcLButtonDblClk(UINT nHitTest, CPoint point);
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};
会思考的草 2004-03-24
  • 打赏
  • 举报
回复
你还是要重载CalcDynamicLayout,当停靠的时候会调用此函数以查询该bar的size。你先return CSize(100,100)试试看。

一、Temple Method模式说明
定义一个抽象类,抽象类中定义一组通用的接口,从抽象类派生的类中定义通用接口的不完全相同的实现,使得使用此类的客户以相同的方式使用这一组类,而不去考虑其实现上的差别。
二、Temple Method 模式实现统一风格窗体(MFC

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧