본문 바로가기
Windows/MFC 강좌 & Tips

Dialog에서 툴바 생성

by 미티치 2017. 8. 23.


설명 출처 블로그에 들어가면 자세하게 나와있지만

MFC에서 ToolBar(도구모음)를 생성하는 방법은 Frame 기반 MFC 프로젝트에서와 Dialog 기반 MFC 프로젝트에서 다르다.


나는 다이얼로그 기반에서 ToolBar를 생성해서 붙여줬다.


툴바를 붙이고자하는 Dlg.cpp의 OnInitDialog에서 InitToolBar() 를 호출


BOOL CTest2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
InitToolBar();
return TRUE; // return TRUE unless you set the focus to a control
}



InitToolBar( ) 함수는 다음과 같다. 주석은 소스 출처 블로그에 잘 나와있음.

CTest2Dlg.h 에 

CToolBar m_wndToolBar;

void InitToolBar();

선언해줬고, IDR_TOOLBAR1은 리소스에서 ToolBar 추가하고, 이 툴바를 다이얼로그에 붙이기 위해 해당 툴바의 ID 값을 LoadToolBar로 로딩!


// 툴바 그려주는 함수
void CTest2Dlg::InitToolBar()
{
if(!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC | CBRS_GRIPPER | CBRS_BORDER_ANY)
|| !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1) )
{
EndDialog(IDCANCEL);
}
CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNow);
CPoint ptOffset(rcClientNow.left - rcClientStart.left, rcClientNow.top - rcClientStart.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while(pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
MoveWindow(rcWindow, FALSE);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
}





추가적으로,

툴바에 버튼을 클릭해서 처리를 해주고 싶다면


1) CTest2Dlg.h 에 함수 추가

afx_msg void OnTbClickedWindows();


2) Ctest2Dlg.cpp 의 메세지 맵(BEGIN_MESSAGE_MAP~ )에 메시지 처리 추가

ON_COMMAND(ID_BtnWindows, &CSTest2Dlg::OnTbClickedWindows)

- 여기서 ID_BtnWindows는 toolbar의 ID가 아니고, 툴바에 추가된 버튼 우클릭 > property에 설정한 ID값임!! 


3) OnTbClickedWindows() 구현

void CSpyTest2Dlg::OnTbClickedWindows()

{    MessageBox(L"test!" , MB_OK);    }







위와 같이 하면 툴바에 있는 버튼 클릭했을 때 메세지 박스가 뜨는 이벤트가 추가된다.






참고 출처

설명 : http://saack.tistory.com/66

소스 : https://m.blog.naver.com/PostView.nhn?blogId=marine150&logNo=140119744403&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F