分享
 
 
 

StatusBar on Dialogs-为作为主窗口的对话框添加状态栏的一种方法

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

感谢CSDN论坛上的Featured(我握着爱情的门票静静排队……) 提供了一个演示程序的下载链接:http://www.bypro.net/PostAttachment.aspx?PostID=21994&AttachmentID=1834

感谢CSDN论坛上的Featured(我握着爱情的门票静静排队……) 提供了本文的链接

http://www.codeguru.com/Cpp/W-D/dislog/toolbarsandstatusbars/article.php/c1955/

Girish Pandit (view profile)

July 28, 1999

MFC allows you to easily add status bars to CFrameWnd-derived windows. However, if you want to add a status bar to a dialog, you're going to find the going just a bit more difficult. Basically, it turned out that I had to dig very deep into the MFC documentation in order to find anything to help me out. One example I found is by ZEKSER Cyril. His techniques works fine, but (IMHO) is not very "clean" since you have to place an invisible static object on the dialog as a kind of placeholder for the status bar. However, I do want to thank him very much for showing me the light at the end of the tunnel. The technique I came up with works like this: First, you need to develop your dialog (and define its CDialog-based class). Nothing new here so far. Then, insert the following code into the CDialog::OnInitDialog function (the m_StatBar variable is of type CStatusBarCtrl). BOOL CMyDlg::OnInitDialog(){CDialog::OnInitDialog();// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE);// Set big iconSetIcon(m_hIcon, FALSE);// Set small icon/************************************************************************//*Adding STATUSBAR*//************************************************************************/int nTotWide;// total width of status barCRect rect; this->GetWindowRect(&rect); rect.top = rect.bottom- 25;m_bRvStatOk = m_StatBar.Create(WS_CHILD | WS_BORDER | WS_VISIBLE ,rect,this, IDC_STATUSBAR); if (m_bRvStatOk == NULL){ AfxMessageBox ("Status Bar not created!", NULL, MB_OK );}// //get size of window, use to configure the status //bar with four separate parts // CRect rWin; this->GetWindowRect(&rWin); nTotWide = rWin.right-rWin.left;//// Make each part 1/4 of the total width of the window.// m_Widths[0] = nTotWide / 4; m_Widths[1] = nTotWide / 2; m_Widths[2] = nTotWide - m_Widths[0]; m_Widths[3] = -1;m_StatBar.SetMinHeight(25);m_StatBar.SetParts( 4, m_Widths);//// now lets put some text, different styles into parts of status bar//m_StatBar.SetText("WITH BORDER.", 0,0);m_StatBar.SetText("WITHOUT BORDER.",1,SBT_NOBORDERS);m_StatBar.SetText("POPUP.",2,SBT_POPOUT); ////make the last part owner-drawn, add a bitmap//m_StatBar.SetText(NULL,3, SBT_OWNERDRAW);//hBmp is a Global Variable of type HBITMAP hBmp = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));/************************************************************************//*End STATUSBAR*//************************************************************************/// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control}The fourth pane of the status bar is owner drawn because it is used to display a bitmap. In order to do this, simply add a message handler for the dialog's WM_DRAWITEM message. Once you've added that function, update it so that when finished it looks like the following. void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct){//// Draw bitmap in status bar//HDC hdcMem;// device context for memory HGDIOBJ hbmOld; // old bitmap area we're over-writing BITMAP bm;// bitmap we're using// // Create a compatible DC in memory//hdcMem = CreateCompatibleDC(lpDrawItemStruct->hDC); // Select the "logo.bmp" bitmap into the DC. hbmOld = ::SelectObject(hdcMem, hBmp); // Get the size of the bitmap ::GetObject(hBmp, sizeof(bm), &bm); // Blt the bitmap to the part. BitBlt(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.top, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0,SRCCOPY);//// Add some text..1st. get bounding rectangle, then position & display text//char szText[16];RECT rText; // text rectanglerText.left = lpDrawItemStruct->rcItem.left+24;rText.top = lpDrawItemStruct->rcItem.top;rText.right = lpDrawItemStruct->rcItem.right-20;rText.bottom = lpDrawItemStruct->rcItem.bottom; // // add some text after the logo bitmap here //memset(szText,0,sizeof(szText));strcpy(szText,"LOGO");// text to drawSelectObject( lpDrawItemStruct->hDC, GetStockObject( ANSI_VAR_FONT ) );::SetBkColor(lpDrawItemStruct->hDC, 0x00c0c0c0); // set background color ExtTextOut(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+24, lpDrawItemStruct->rcItem.top+4, ETO_OPAQUE, &rText, szText, strlen(szText),NULL );// draw the text in the rectangle rText//// End adding text. Reselect the original object into the DC.// SelectObject(hdcMem, hbmOld); // Delete the compatible DC. DeleteDC(hdcMem);}Make the following changes to the dialog's header file. class CMyDlg : public CDialog{// Constructionpublic:CMyDlg(CWnd* pParent = NULL);// standard constructorCStatusBarCtrlm_StatBar;..............................................................................................................}CStatusBarCtrlm_StatBar;..............................................................................................................}Finally, make the following changes to the resource.h file. //{{NO_DEPENDENCIES}}// Microsoft Developer Studio generated include file.//#define IDM_ABOUTBOX0x0010#define IDC_STATUSBAR 32500..............................................................................................................That's it! You now have a status bar at the bottom of your dialog!

MFC allows you to easily add status bars to CFrameWnd-derived windows. However, if you want to add a status bar to a dialog, you're going to find the going just a bit more difficult.

Basically, it turned out that I had to dig very deep into the MFC documentation in order to find anything to help me out. One example I found is by ZEKSER Cyril. His techniques works fine, but (IMHO) is not very "clean" since you have to place an invisible static object on the dialog as a kind of placeholder for the status bar. However, I do want to thank him very much for showing me the light at the end of the tunnel.

The technique I came up with works like this: First, you need to develop your dialog (and define its CDialog-based class). Nothing new here so far.

Then, insert the following code into the CDialog::OnInitDialog function (the m_StatBar variable is of type CStatusBarCtrl).

BOOL CMyDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// 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

/************************************************************************/

/*Adding STATUSBAR*/

/************************************************************************/

int nTotWide;// total width of status bar

CRect rect;

this->GetWindowRect(&rect);

rect.top = rect.bottom- 25;

m_bRvStatOk = m_StatBar.Create(WS_CHILD | WS_BORDER | WS_VISIBLE ,rect,this,

IDC_STATUSBAR);

if (m_bRvStatOk == NULL)

{

AfxMessageBox ("Status Bar not created!", NULL, MB_OK );

}

//

//get size of window, use to configure the status

//bar with four separate parts

//

CRect rWin;

this->GetWindowRect(&rWin);

nTotWide = rWin.right-rWin.left;

//

// Make each part 1/4 of the total width of the window.

//

m_Widths[0] = nTotWide / 4;

m_Widths[1] = nTotWide / 2;

m_Widths[2] = nTotWide - m_Widths[0];

m_Widths[3] = -1;

m_StatBar.SetMinHeight(25);

m_StatBar.SetParts( 4, m_Widths);

//

// now lets put some text, different styles into parts of status bar

//

m_StatBar.SetText("WITH BORDER.", 0,0);

m_StatBar.SetText("WITHOUT BORDER.",1,SBT_NOBORDERS);

m_StatBar.SetText("POPUP.",2,SBT_POPOUT);

//

//make the last part owner-drawn, add a bitmap

//

m_StatBar.SetText(NULL,3, SBT_OWNERDRAW);

//hBmp is a Global Variable of type HBITMAP

hBmp = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));

/************************************************************************/

/*End STATUSBAR*/

/************************************************************************/

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control

}

The fourth pane of the status bar is owner drawn because it is used to display a bitmap. In order to do this, simply add a message handler for the dialog's WM_DRAWITEM message. Once you've added that function, update it so that when finished it looks like the following.

void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)

{

//

// Draw bitmap in status bar

//

HDC hdcMem;// device context for memory

HGDIOBJ hbmOld; // old bitmap area we're over-writing

BITMAP bm;// bitmap we're using

//

// Create a compatible DC in memory

//

hdcMem = CreateCompatibleDC(lpDrawItemStruct->hDC);

// Select the "logo.bmp" bitmap into the DC.

hbmOld = ::SelectObject(hdcMem, hBmp);

// Get the size of the bitmap

::GetObject(hBmp, sizeof(bm), &bm);

// Blt the bitmap to the part.

BitBlt(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left,

lpDrawItemStruct->rcItem.top, bm.bmWidth, bm.bmHeight,

hdcMem, 0, 0,SRCCOPY);

//

// Add some text..1st. get bounding rectangle, then position & display text

//

char szText[16];

RECT rText; // text rectangle

rText.left = lpDrawItemStruct->rcItem.left+24;

rText.top = lpDrawItemStruct->rcItem.top;

rText.right = lpDrawItemStruct->rcItem.right-20;

rText.bottom = lpDrawItemStruct->rcItem.bottom;

//

// add some text after the logo bitmap here

//

memset(szText,0,sizeof(szText));

strcpy(szText,"LOGO");// text to draw

SelectObject( lpDrawItemStruct->hDC, GetStockObject( ANSI_VAR_FONT ) );

::SetBkColor(lpDrawItemStruct->hDC, 0x00c0c0c0); // set background color

ExtTextOut(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+24, lpDrawItemStruct->rcItem.top+4, ETO_OPAQUE, &rText, szText,

strlen(szText),NULL );// draw the text in the rectangle rText

//

// End adding text. Reselect the original object into the DC.

//

SelectObject(hdcMem, hbmOld);

// Delete the compatible DC.

DeleteDC(hdcMem);

}

Make the following changes to the dialog's header file.

class CMyDlg : public CDialog

{

// Construction

public:

CMyDlg(CWnd* pParent = NULL);// standard constructor

CStatusBarCtrlm_StatBar;

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

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

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

}

CStatusBarCtrlm_StatBar;

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

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

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

}

Finally, make the following changes to the resource.h file.

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

//

#define IDM_ABOUTBOX0x0010

#define IDC_STATUSBAR 32500

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

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

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

That's it! You now have a status bar at the bottom of your dialog!

MFC allows you to easily add status bars to CFrameWnd-derived windows. However, if you want to add a status bar to a dialog, you're going to find the going just a bit more difficult.

Basically, it turned out that I had to dig very deep into the MFC documentation in order to find anything to help me out. One example I found is by ZEKSER Cyril. His techniques works fine, but (IMHO) is not very "clean" since you have to place an invisible static object on the dialog as a kind of placeholder for the status bar. However, I do want to thank him very much for showing me the light at the end of the tunnel.

The technique I came up with works like this: First, you need to develop your dialog (and define its CDialog-based class). Nothing new here so far.

Then, insert the following code into the CDialog::OnInitDialog function (the m_StatBar variable is of type CStatusBarCtrl).

BOOL CMyDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// 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

/************************************************************************/

/*Adding STATUSBAR*/

/************************************************************************/

int nTotWide;// total width of status bar

CRect rect;

this->GetWindowRect(&rect);

rect.top = rect.bottom- 25;

m_bRvStatOk = m_StatBar.Create(WS_CHILD | WS_BORDER | WS_VISIBLE ,rect,this,

IDC_STATUSBAR);

if (m_bRvStatOk == NULL)

{

AfxMessageBox ("Status Bar not created!", NULL, MB_OK );

}

//

//get size of window, use to configure the status

//bar with four separate parts

//

CRect rWin;

this->GetWindowRect(&rWin);

nTotWide = rWin.right-rWin.left;

//

// Make each part 1/4 of the total width of the window.

//

m_Widths[0] = nTotWide / 4;

m_Widths[1] = nTotWide / 2;

m_Widths[2] = nTotWide - m_Widths[0];

m_Widths[3] = -1;

m_StatBar.SetMinHeight(25);

m_StatBar.SetParts( 4, m_Widths);

//

// now lets put some text, different styles into parts of status bar

//

m_StatBar.SetText("WITH BORDER.", 0,0);

m_StatBar.SetText("WITHOUT BORDER.",1,SBT_NOBORDERS);

m_StatBar.SetText("POPUP.",2,SBT_POPOUT);

//

//make the last part owner-drawn, add a bitmap

//

m_StatBar.SetText(NULL,3, SBT_OWNERDRAW);

//hBmp is a Global Variable of type HBITMAP

hBmp = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));

/************************************************************************/

/*End STATUSBAR*/

/************************************************************************/

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control

}

The fourth pane of the status bar is owner drawn because it is used to display a bitmap. In order to do this, simply add a message handler for the dialog's WM_DRAWITEM message. Once you've added that function, update it so that when finished it looks like the following.

void CMyDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)

{

//

// Draw bitmap in status bar

//

HDC hdcMem;// device context for memory

HGDIOBJ hbmOld; // old bitmap area we're over-writing

BITMAP bm;// bitmap we're using

//

// Create a compatible DC in memory

//

hdcMem = CreateCompatibleDC(lpDrawItemStruct->hDC);

// Select the "logo.bmp" bitmap into the DC.

hbmOld = ::SelectObject(hdcMem, hBmp);

// Get the size of the bitmap

::GetObject(hBmp, sizeof(bm), &bm);

// Blt the bitmap to the part.

BitBlt(lpDrawItemStruct->hDC,lpDrawItemStruct->rcItem.left,

lpDrawItemStruct->rcItem.top, bm.bmWidth, bm.bmHeight,

hdcMem, 0, 0,SRCCOPY);

//

// Add some text..1st. get bounding rectangle, then position & display text

//

char szText[16];

RECT rText; // text rectangle

rText.left = lpDrawItemStruct->rcItem.left+24;

rText.top = lpDrawItemStruct->rcItem.top;

rText.right = lpDrawItemStruct->rcItem.right-20;

rText.bottom = lpDrawItemStruct->rcItem.bottom;

//

// add some text after the logo bitmap here

//

memset(szText,0,sizeof(szText));

strcpy(szText,"LOGO");// text to draw

SelectObject( lpDrawItemStruct->hDC, GetStockObject( ANSI_VAR_FONT ) );

::SetBkColor(lpDrawItemStruct->hDC, 0x00c0c0c0); // set background color

ExtTextOut(lpDrawItemStruct->hDC, lpDrawItemStruct->rcItem.left+24, lpDrawItemStruct->rcItem.top+4, ETO_OPAQUE, &rText, szText,

strlen(szText),NULL );// draw the text in the rectangle rText

//

// End adding text. Reselect the original object into the DC.

//

SelectObject(hdcMem, hbmOld);

// Delete the compatible DC.

DeleteDC(hdcMem);

}

Make the following changes to the dialog's header file.

class CMyDlg : public CDialog

{

// Construction

public:

CMyDlg(CWnd* pParent = NULL);// standard constructor

CStatusBarCtrlm_StatBar;

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

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

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

}

CStatusBarCtrlm_StatBar;

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

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

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

}

Finally, make the following changes to the resource.h file.

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

//

#define IDM_ABOUTBOX0x0010

#define IDC_STATUSBAR 32500

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

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

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

That's it! You now have a status bar at the bottom of your dialog!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有