首页 > 其他分享 >StatusBar on Dialogs-在VC中为作为主窗口的对话框添加状态栏的几种方法

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

时间:2022-12-12 15:00:37浏览次数:72  
标签:status VC Dialogs StatBar 对话框 lpDrawItemStruct bitmap dialog rcItem


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

感谢CSDN论坛上的 goodboyws(深夜不眠者) 提供了一个为对话框加状态栏和工具栏的演示程序的下载链接:​​http://www.codeproject.com/dialog/dlgtoolstatusbar.asp​

感谢CSDN论坛上的  DentistryDoctor(My heart will fly,in the sky.)   提供了本文的链接

​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 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
CStatusBarCtrl m_StatBar;

.....................................
...................................
......................................
}

CStatusBarCtrl m_StatBar;

..................................... ................................... ...................................... }

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

//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
//

#define IDM_ABOUTBOX 0x0010
#define IDC_STATUSBAR 32500
.....................................
...................................
......................................

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

标签:status,VC,Dialogs,StatBar,对话框,lpDrawItemStruct,bitmap,dialog,rcItem
From: https://blog.51cto.com/endurer/5929625

相关文章

  • 弹出时间对话框
    //弹出时间对话框button=findViewById(R.id.select);button.setOnClickListener(newView.OnClickListener(){@Override......
  • 点击弹出日期对话框
    //按钮事件findViewById(R.id.btn_ok).setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(View......
  • 显示日期对话框
    <DatePickerandroid:id="@+id/dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:calen......
  • spring mvc DispatcherServlet详解之interceptor和filter的区别
    首先我们看一下springmvcInterceptor的功能及实现:​​http://wenku.baidu.com/link?url=Mw3GaUhCRMhUFjU8iIDhObQpDcbmmRy_IPeumazg0ppnbmwqFUtLp9kSpuPPpeysf6EnHBLYFeWr......
  • MVCC
    什么是mvccmulti-versionconcurrencycontrl多版本并发控制InnoDB中的实现主要是为了提高数据库并发性能,用更好的方式去处理读-写冲突,做到即使有读写冲突时,也能做到不......
  • 我的Swagger实例ASP.NET MVC 集成SwaggerUI
      注意:1.在此之前你要确定你已经通过NuGet安装好 Swashbucklev5.6.02.Web项目右键属性-》生成-》勾选XML文档文件,建议用默认路径 3.并且,修改了,项目》App_Start》Swagg......
  • 使用Eclipse构建Maven的SpringMVC项目
    使用Eclipse构建Maven的SpringMVC项目首先Eclipse需要安装Maven的插件,地址:http://m2eclipse.sonatype.org/sites/m2e。用MyEclipse安装Maven插件,......
  • spring 3 mvc中对title2的处理
    在springmvc3中可以用title2的框架简化,下面是步骤:1加上包,包如下图:2在springmvc中设置title框架/WebContent/WEB-INF/spring-servlet.xml<beanid="viewResolver......
  • spring mvc——@RequestMapping注解的详解(003)
                                                     ......
  • spring mvc——@RequestMapping注解的讲解(002)
            //一个方法处理一个请求,不能一个请求对应两个方法@RequestMapping("/handle01")publicStringhandle01(){System.out......