원본 : http://www.debuglab.com/knowledge/toolbarcontrol.html
1.요약
Font종류나 확대/축소 비율을 정할 때 Toolbar에 ComboBox를 넣어서 많이 씁니다. 이처럼 자기가 원하는 Control를 Toolbar안에 어떻게 삽입하는지 설명하겠습니다.
2.본문
ComboBox를 생성하는 것을 예로 들었습니다.
CToolBarEx를 상속받는 CCombobar를 생성한다.
CCombobar에서 다음과 같이 구현해 준다.
/////////////////////
// combobar.cpp
BEGIN_MESSAGE_MAP(CComboBar, CToolBarEx)
//{{AFX_MSG_MAP(CComboBar)
ON_CBN_SELENDOK(IDW_TOOLCOMBO, OnSelEndOk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// 이안에서 Toolbar도 생성하고 ComboBox도 생성한다.
BOOL CComboBar::Create(CFrameWnd * pParent, UINT nID, UINT nComboID,
const int nWidth,
const int nHeight)
{
// Create the toolbar as CMainFrame::OnCreate() would do
if (!CToolBar::Create(pParent) ||
!LoadToolBar(nID))
{
TRACE0("Falied to create the toolbar.\n");
return FALSE; // fail to create
}
// set the size of combo-control
CRect rect(-nWidth, -nHeight, 0, 0);
// ComboxBox가 들어갈 위치를 TBBS_SEPARATOR속성으로 바꾸고
// 넓이를 지정해 준다.
ASSERT(CommandToIndex(nComboID) >= 0); // make sure the id is valid
SetButtonInfo(CommandToIndex(nComboID), nComboID, TBBS_SEPARATOR, nWidth);
// ComboBox를 생성한다.
if (!m_pWndBox.Create(WS_CHILD | CBS_DROPDOWN |
CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS, rect, this,
nComboID))
{
TRACE("Failed to create the combo-box %p .\n", ComboID);
return FALSE;
}
// ComboBox가 들어갈 위치의 크기를 가져온다.
// 그 크기에 따라 ComboBox의 크기와 위치를 조절해 준다.
GetItemRect(CommandToIndex(nComboID), &rect);
m_pWndBox.SetWindowPos(0, rect.left, rect.top, 0, 0,
SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCOPYBITS);
m_pWndBox.SetFont(&m_GuiFont);
m_pWndBox.ShowWindow(SW_SHOW);
if (!OnInitialCreate())
{
TRACE("Failed to add strings to %p\n", nComboID);
return FALSE;
}
return TRUE;
}
// ComboBox의 데이터를 초기화한다.
BOOL CComboBar::OnInitialCreate()
{
if(m_pWndBox.m_hWnd != NULL)
{
//This is where you add your strings
m_pWndBox.AddString("Page Width");
m_pWndBox.AddString("150 %");
m_pWndBox.AddString("125 %");
m_pWndBox.AddString("100 %");
m_pWndBox.AddString("75 %");
m_pWndBox.AddString("50 %");
m_pWndBox.AddString("25 %");
//Don't forget the initial position.
m_pWndBox.SetCurSel(0);
return TRUE;
}
return FALSE;
}
int CComboBar::m_cbIndex = 0;
void CComboBar::OnSelEndOk()
{
m_cbIndex = m_pWndBox.GetCurSel();
}
/////////////////////////
// combobar.h
class CComboBar : public CToolBarEx
{
// Construction
public:
CComboBar();
CComboBox m_pWndBox;
static int m_cbIndex;
// Attributes
public:
// Operations
public:
BOOL OnInitialCreate();
// is the toolbar-resource to load
// is the ID of the button, that shall be used as the
combo-control
BOOL Create(CFrameWnd * pParent, UINT nID, UINT nComboID, const int
nWidth,
const int nHeight);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CComboBar)
public:
virtual void OnFinalRelease();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CComboBar();
// Generated message map functions
protected:
//{{AFX_MSG(CComboBar)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(CComboBar)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg void OnSelEndOk();
//}}AFX_DISPATCH
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
};
CMainFrame에서 CCombobar를 선언해서 일반 툴바와 같은 방법으로 사용한다.
- 2001.08.13 Smile Seo -
"MFC" 카테고리의 다른 글
- Explorer처럼 툴바에 Text 넣기 (0)2007/02/22
- 문자열 검색 API (0)2007/02/22
- Toolbar안에 다른 Control넣기 (0)2007/02/22
- Winamp 처럼 벽에 붙이기 (0)2007/02/22
- 윈앰프처럼 TaskBar와 SystemTray 마음대로 주무루기 (0)2007/02/22

수안이의 컴퓨터 연구실



Leave your greetings.