본문 바로가기

Language/MFC

4대클래스 자동생성 소스 분석[CMain Frame]


CMainFrame클래스

Mainfrm.cpp를 열어보면 앞의 CWinApp에서 다루어 본 BEGIN_MESSAGE_MAP을 볼 수 있을 것이다.

설명했으니 그냥 넘어간다.

BEGIN_MESSAGE_MAP(CMainFrame. CFrameWnd)

//{{AFX_MSG_MAP(CMainFrame)

// NOTE - the Classwizard will add and remove

mapping macros here.

// DO NOT EDIT what you see in these blocks

of generated code !

ON_WM_CREATE()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

아래의 indicators는 상태창에 나타나는 string의 ID값이다.

ID_SEPARATOR는 라인을 그어서 분리시키고 ID_INDICATOR_CAPS는 CapsLock키를, ID_INDICATOR_NUM

은 NumLock키를, ID_INDICATOR_SCRLS는 SrollLock키를 각각 ON/OFF 상태를 확인하는 ID이다.

프로그램 실행시 키들을 눌러보면 상태창 오른쪽에 나타날 것이다.

static UNIT indicators[] =

{

ID_SEPARATOR, //status line indicator

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};

메인 프레임의 생성자입니다. 현재는 비어 있는 상태이다.

CMainFrame::CMainFrame()
{

// TODO: add member initialization code here

}

메인프레임의 소멸자다.

CMainFrame::~CMainFrame()
{

}

 

OnCreate 함수는 메인 프레임이 만들어질 때 실행된다. 아버지 클래스인 CFrameWnd것을 사용

하지 않고 자기 것, 즉 virtual로 사용한다는 것.

int CMainFrame::OnCreate(LPCREATESTRUCT 1pCreateStruct)

//아버지 클래스의 OnCreate를 먼저 실행하고

if (CFrameWnd:: OnCreate(1pCreateStruct) == -1)

return -1;

//도구바를 만들고

if (!m_WndToolBar.Create(this) || !m_WndToolBar.LoadToolBar(IDR_MAINFRAME))  

{

TRACEO("Failed to create toolbar\n");

return -1; // fail to create
}

//상태창을 만들고

if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UNIT)))
{

TRACEO("Failed to create status bar\n");

return -1; // fail to create

 }

// TODO: Delete these there three lines if you don't want the toolbar to

//br dockable

 

아래의 코드는 메뉴바를 도킹 가능한 메뉴바로 만드는 코드이다. 이 또한 이 프로젝트 AppWizard

m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

EnableDocking(CBRS_ALIGN_ANY);

DockControlBar(&m_wndToolBar);

m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |

CBRS_TOOLTIPS | CBRS_FLYBY);

 

return 0;

 

}

CWinApp클래스에서 CMainFrame 프로그램이 실행될 수 있도록 프레임을 로드하였다.

CMainFrame의 OnCreate 함수는 CWinApp의 Initinstance 함수의 if(!pMainFrame->LoadFrame(IDR_MAINFRAME)) 가 실행되면 후에 수행되는 함수입니다. OnCreate 함수는 실행될 때 필요한 여러 가지 일을 수행 하는 함수이다. 위의 내용은 메인 프레임에 있는 메뉴와 도구바 그리고 상태창 등을 메인 프레임에 등록하는 것이다. 지금까지 내용으로 CWinApp 클래스에서 메인 프레임을 만들고 CMainFrame에 도구바와 메뉴와 상 태바를 등록했다.