MFC 状态栏上创建按钮,创建 Edit 对话框等
"MainFrm.h"头文件增加:
CButton
m_wndAboutButton;
CEdit
m_wndEdit; //此处的 m_wndEdit 可以作为正常的 EditBox 的添加的变量调用。
如:m_wndEdit.SetWindowsTextA(“AAAA”);
1.
2.
3.
butfont;
////////////////////////////////////////////////////////////////
// MSDN Magazine -- December 2001
CFont
virtual void RecalcLayout(BOOL bNotify = TRUE);
afx_msg void CMainFrame::OnViewStatusBar();
4.
5.
6.
7.
8.
9.
10. // If this code works, it was written by Paul DiLascia.
11. // If not, I don't know who wrote it.
12. // Compiles with Visual C++ 6.0/Windows 98 and probably Windows 2000 too.
13. // Set tabsize = 3 in your editor.
14. //
15. #include "StdAfx.h"
16. #include "StatBarButn.h"
17. #include "MainFrm.h"
18.
19. #ifdef _DEBUG
20. #define new DEBUG_NEW
21. #undef THIS_FILE
22. static char THIS_FILE[] = __FILE__;
23. #endif
24.
25. const CXABOUTBUTTON = 50;
26.
27. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
28. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
29. //{{AFX_MSG_MAP(CMainFrame)
30. ON_COMMAND(ID_VIEW_STATUS_BAR, OnViewStatusBar)
31. ON_COMMAND(ID_WDKZ, &CMainFrame::OnWdkz)
32. ON_WM_CREATE()
33. //}}AFX_MSG_MAP
34. END_MESSAGE_MAP()
35.
36. static UINT indicators[] = {
37. ID_SEPARATOR, // status line indicator
38. ID_INDICATOR_CAPS,
39. ID_INDICATOR_NUM,
40. ID_INDICATOR_SCRL,
41. };
42.
43. CMainFrame::CMainFrame()
44. {
45. }
46.
47. CMainFrame::~CMainFrame()
48. {
49. }
50.
51. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
52. {
53. if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
54. return -1;
55.
56. if (!m_wndToolBar.Create(this) ||
57. !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) {
58. TRACE0("Failed to create toolbar\n");
59. return -1; // fail to create
60. }
61.
62. // Hack: turn off WS_THICKFRAME so the status bar will
63. // have no size grip handles.
64. //
65. ModifyStyle(WS_THICKFRAME,0);
66. if (!m_wndStatusBar.Create(this) ||
67. !m_wndStatusBar.SetIndicators(indicators,
68. sizeof(indicators)/sizeof(UINT))) {
69. TRACE0("Failed to create status bar\n");
70. return -1; // fail to create
71. }
72. // Turn WS_THICKFRAME back on.
73. ModifyStyle(0,WS_THICKFRAME);
//创建状态栏上的按钮
CRect rc;
VERIFY(m_wndAboutButton.Create("监测记录",
WS_VISIBLE|BS_FLAT,rc,this,ID_JCXX)); //创建状态栏上的按钮类型和关联显示项目
butfont.CreatePointFont(90,"宋体");//字体大小控制列表高度
m_wndAboutButton.SetFont(&butfont,true);
//创建状态栏上的EditBox等
VERIFY(m_wndEdit.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,rc,
this, 1)); //创建状态栏上的Editbox 类型和关联显示项目 代码中“1”可以是定义的
任意数字代表了 “nID”
//更改菜单栏背景色
1.
// TODO: Remove this if you don't want tool tips or a resizeable toolbar
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
return 0;
}
2.
3.
4.
1.
2.
3.
//////////////////
{
CFrameWnd::RecalcLayout(bNotify);
CRect rc;
if (m_wndStatusBar.m_hWnd)
{
1. // 重写功能,调整 MFC 框架窗口中的状态栏
2. void CMainFrame::RecalcLayout(BOOL bNotify)
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
1.
2.
3.
4.
5.
// 状态栏上按钮的位置大小
rc.left = rc.right;
rc.right += CXABOUTBUTTON;
SWP_NOZORDER);
SWP_NOZORDER);
m_wndStatusBar.GetWindowRect(&rc);
ScreenToClient(&rc);
rc.right -= CXABOUTBUTTON;
m_wndStatusBar.SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),
m_wndAboutButton.SetWindowPos(NULL,rc.left,rc.top,rc.Width(),rc.Height(),
// 状态栏上EditBox的位置大小
m_wndEdit.SetWindowPos(NULL,rc.left-1000,rc.top,rc.Width()+900,rc.Height(),
SWP_NOZORDER); //坐标 X,Y 位置从右向左
}
1.
2.
3.
4.
5. //////////////////
}
6. //查看状态栏句柄
7. //
8. void CMainFrame::OnViewStatusBar()
{
1.
2.
3.
4.
5.
6.
7.
// 调用基类的处理
OnBarCheck(AFX_IDW_STATUS_BAR);
//按钮状态栏风格
BOOL bShow = m_wndStatusBar.GetStyle() & WS_VISIBLE;
m_wndAboutButton.SetWindowPos(NULL, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
(bShow ? SWP_SHOWWINDOW : SWP_HIDEWINDOW));
8. //EditBox状态栏风格
9.
m_wndEdit.GetStyle() & WS_VISIBLE;
m_wndEdit.SetWindowPos(NULL, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_SHOWWINDOW);
10.
11.
12.
13. }
////////////////
14. void CMainFrame::OnWdkz()
15. {
CWDKZ pwd; //显示温度控制对话框
16. pwd.Create(IDD_WDKZ,this);//温控模块显示
17.
18.
pwd.ShowWindow(SW_SHOW);
19. }
接着上面定义的 EditBox 在状态栏上的文本框滚动文字显示
1、 定义
CString xinxi="";
2、 初始化中添加
SetTimer(1,1000,NULL); //设置定时器号,时钟周期为毫秒
3、添加消息响应 WM_TIMER
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
for(int i=0;i<32;i++)
{
}
xinxi=xinxi+tishi1[i].tishixinxi;
xinxi=xinxi+ssjk+r1sjk+r2sjk; //显示的文本
switch (nIDEvent)
{
case 1:
if(!xinxi.IsEmpty())
{
}
else
{
}
xinxi=xinxi.Right(xinxi.GetLength()-2);
m_wndEdit.SetWindowTextA(xinxi);
xinxi=xinxi;//xinxi中的内容是时刻变化的,前后赋值确保循环
xinxi=xinxi;
break;
}
CMDIFrameWnd::OnTimer(nIDEvent);
}
改变状态栏 EditBox 的底色和文本颜色
1、在头文件中定义:
CBrush m_brush; //界面背景画刷
2、新建消息响应函数 WM_CTLCOLOR
HBRUSH CMainFrame::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
}
HBRUSH hbr = CMDIFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改DC 的任何属性
if (pWnd->GetDlgCtrlID() == 1) //这里“==1”中“”是上面创建m_wndEdit是定义的nID为“”
{
pDC->SetBkColor(RGB(210,217,251));//背景色为绿色
pDC->SetTextColor(RGB(230, 0, 0));//文字为红色
return m_brush;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return hbr;