본문 바로가기

Visual C++/Status Bar, MFC

MFC, Status Bar

MFC 기반 프로그래밍에서 Status Bar (상태바)에 문자열 출력하기


MainFrm.cpp 파일의 상부에서 다음과 같은 시스템이 자동으로 생성한 부분을 확인할 수 있다

static UINT indicators[] =

{

ID_SEPARATOR,           // 상태 줄 표시기, 개발자가 제어하여 임의의 문자열을 출력할 수 있는 상태바 영역(Pane)

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};


상태바 상에서 텍스트를 출력할 부분(Pane)을 4개  부분으로 설정하고 있고, 대/소문자 키눌림 상태정보 등을 출력하도록 디폴트로 설정되어 있는 것을 볼 수 있다.

디폴트로 4개의 구획(Pane)으로 구분되어 있지만 필요에 따라서 구획을 더 추가할 수도 있고 필요없는 구획을 제거할 수도 있다


디폴트로 설정된 상태바 구획을 그대로 유지하고 위에 한개의 구획을 더 추가한다면 다음과 같다

static UINT indicators[] =

{

ID_SEPARATOR,           // 상태 줄 표시기, 개발자가 제어하여 임의의 문자열을 출력할 수 있는 상태바 영역(Pane)

        ID_SEPARATOR,           // 개발자가 임의로 추가한 상태바 영역

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};



MainFrm.cpp 파일의 OnCreate() 함수 안에서 다음과 같이 상태바를 생성하고 설정하는 부분을 찾을 수 있다

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

     .......   .......

     if (!m_wndStatusBar.Create(this)) 

     { 

         TRACE0("상태 표시줄을 만들지 못했습니다.\n"); 

         return -1; // 만들지 못했습니다. 

     }

     m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

     ....................
     ...............

     .................

}



자동으로 생성된 위의  코드를 보면, 상태바를 제어하기 위해서는 MFC 프레임워크에서 자동으로 제공하는 m_wndStatusBar 를 사용하면 될 것이다.

m_wndStatusBar 변수는 CStatusBar 클래스의 인스턴스이므로 CStatusBar 클래스의 멤버 메소드를 이용하면 상태바를 제어할 수 있다.


- 상태바의 스타일 제어 :                     CStatusBar::SetPaneInfo()

- 상태바의 특정 Pane에 문자열 출력 :   CStatusBar::SetPaneText()



사용 예

CMainFrame::OnCreate() 함수 안에 상태바의 스타일과 폭을 조정하는 코드를 넣는다

m_wndStatusBar.SetPaneInfo(0, ID_SEPARATOR, SBPS_NORMAL, 200); // 순번 인덱스, Pane 아이디, 폭

m_wndStatusBar.SetPaneInfo(1, ID_SEPARATOR, SBPS_NORMAL, 100);


프로그램의 전체 영역에서 상태바에 문자열을 출력하는 코드를 사용할 수 있다( m_wndStatusBar 변수를 publiic 으로 설정해야 한다)

m_wndStatusBar.SetPaneText(0, "현재 폴더: "); // 순번 인덱스, 출력 문자열

m_wndStatusBar.SetPaneText(1, "진행량: ");



CMainFrame 클래스의 protected 멤버변수인 m_wndStatusBar를 public 변수로 설정하면 프로그램 코드 상의 어떤 영역에서든지 접근할 수 있으므로 특정 이벤트 핸들러 안에서 상태바에 문자열을 출력하는데 문제가 없다


MSDN 참조

CStatusBar::SetPaneInfo

Visual Studio 2012

Sets the specified indicator pane to a new ID, style, and width.

void SetPaneInfo(
   int nIndex,
   UINT nID,
   UINT nStyle,
   int cxWidth 
);
nIndex

Index of the indicator pane whose style is to be set.

nID

New ID for the indicator pane.

nStyle

New style for the indicator pane.

cxWidth

New width for the indicator pane.

The following indicator styles are supported:

  • SBPS_NOBORDERS   No 3-D border around the pane.

  • SBPS_POPOUT   Reverse border so that text "pops out."

  • SBPS_DISABLED   Do not draw text.

  • SBPS_STRETCH   Stretch pane to fill unused space. Only one pane per status bar can have this style.

  • SBPS_NORMAL   No stretch, borders, or pop-out.

Header: afxext.h


CStatusBar::SetPaneText

Visual Studio 2012

Call this member function to set the pane text to the string pointed to by lpszNewText.

BOOL SetPaneText(
   int nIndex,
   LPCTSTR lpszNewText,
   BOOL bUpdate = TRUE 
);
nIndex

Index of the pane whose text is to be set.

lpszNewText

Pointer to the new pane text.

bUpdate

If TRUE, the pane is invalidated after the text is set.

Nonzero if successful; otherwise 0.

After you call SetPaneText, you must add a UI update handler to display the new text in the status bar.

//Sets and displays text for pane index 3 and id ID_INDICATOR_SCRL
m_wndStatusBar.SetPaneText(3, _T("My New Status Bar Text"), TRUE);


//UI handler in the message map updates the status bar text:
ON_UPDATE_COMMAND_UI(ID_INDICATOR_SCRL, &CMainFrame::OnUpdatePane)


void CMainFrame::OnUpdatePane(CCmdUI* pCmdUI)
{
   pCmdUI->Enable();
}


Header: afxext.h