VC技巧四 - 工具条和状态条 增强型的状态条 首先在你的应用中添加一个新类,这个类的基类是CStatusBar类。本文中这个类 叫做CEnhanceStatusBar。 在头文件中声明以下的控件ID和标志: #define IDC_PROGRESS 3001 //进程条 #define IDC_INFOBUTTON 3002 //Info按钮 #define IDC_LEFTBUTTON 3003 //左移按钮 #define IDC_RIGHTBUTTON 3004 //右移按钮 #define ENHANCEBAR_PROGRESS 1 //进程条标志 #define ENHANCEBAR_INFO 2 //Info按钮标志 #define ENHANCEBAR_BUTTON 4 //左、右移按钮标志 将下列成员函数和成员变量添加到CEnhanceStatusBar类中,并且根据你的需要设 置其属性为public/protected/private: CBitmapButton LeftButton; //左移按钮 CBitmapButton RightButton; //右移按钮 CBitmapButton InfoButton; //Info按钮 CProgressCtrl ProgressControl; //进程条 //下面的四个成员函数返回各个控件: CBitmapButton& GetLeftButton() {return LeftButton;}; CBitmapButton& GetRightButton() {return RightButton;}; CBitmapButton& GetInfoButton() {return InfoButton;}; CProgressCtrl& GetProgressControl() {return ProgressControl;}; UINT ButtonWidth; //按钮宽度 UINT ButtonHeight; //按钮高度 UINT ButtonSpace; //按钮间距 int StatusFlags; //状态条标志 int ProgressPane; //进程条所在的pane标号 int ButtonPane; //按钮所在的pane标号 BOOL Create(CWnd* wnd, int flags, int progress, int button);//状态条生成函数 virtual BOOL AddButtonControl(); //添加按钮控件 virtual BOOL AddProgressControl(); //添加进程条控件 virtual void PositionControls(); //调整控件位置 virtual void DestroyControls(); //删除控件 //下面是为了测试按钮及进程条控件而添加的一些函数及变量 CStringList Messages; //一组CString,用于存贮讯息 POSITION CurrentMessagePosition; //当前讯息在List中的位置 virtual BOOL AddMessage(CString msg, int pos); //加入一条讯息 virtual BOOL ClearMessages(); //删除所有讯息 virtual void SetProgress(BOOL show, int range); //设置进程条 编写构造函数和析构函数,并给Create()函数加入代码: CEnhanceStatusBar::CEnhanceStatusBar() { //初始化参数 ButtonWidth = 14; ButtonHeight = 13; ButtonSpace = 3; CurrentMessagePosition = 0; StatusFlags = 0; ProgressPane = 0; ButtonPane = 0; } CEnhanceStatusBar::~CEnhanceStatusBar() { ClearMessages();//删除所有讯息 DestroyControls();//删除控件 } BOOL CEnhanceStatusBar::Create(CWnd* wnd, int flags, int progress, int button) { if (m_hWnd ==0) { if (!CStatusBar::Create(wnd)) return FALSE; } ClearMessages(); DestroyControls();//在通过菜单命令改变状态条格式时,这个调用很重要, //因为各控件只能生成一次,所以必须先删除 //得到标志和pane号 StatusFlags = flags;//flags标志状态条的格式,有无进程条和某些按钮等。 ProgressPane = progress;//progress表示状态条所在的pane号 ButtonPane = button;//button表示按钮所在的pane号 if (!AddProgressControl())//加入进程条 return FALSE; if (!AddButtonControl())//加入按钮 return FALSE; //加入你自己的控件 //if (!AddCustomControl()) // return FALSE; //加入你自己的控件 EnableToolTips(TRUE); PositionControls();//调整控件位置 //调整你自己的控件的位置 //PositionCustomControl(); //调整你自己的控件的位置 return TRUE; } 给AddButtonControl()、AddProgressControl()和DestroyControls()函数加入代码: BOOL CEnhanceStatusBar::AddButtonControl() { //设置ButtonRect CRect ButtonRect; ButtonRect.left = 100; ButtonRect.top = 2; ButtonRect.right = 114; ButtonRect.bottom = 15; //下面加入左移和右移按钮 if (StatusFlags & ENHANCEBAR_BUTTON) { if (!LeftButton.Create("",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, ButtonRect,this, IDC_LEFTBUTTON)) { TRACE0("Failed to create LeftButton button\n"); return FALSE; } if (!LeftButton.LoadBitmaps(IDB_LEFTBUTTON,IDB_LEFTBUTTON_SEL,NULL, IDB_LEFTBUTTON_DIS)) { TRACE0("Failed to load LeftButton bitmap\n"); return FALSE; } LeftButton.EnableWindow(FALSE); if(!RightButton.Create("",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, ButtonRect,this,IDC_RIGHTBUTTON)) { TRACE0("Failed to create RightButton button\n"); return FALSE; } if (!RightButton.LoadBitmaps(IDB_RIGHTBUTTON,IDB_RIGHTBUTTON_SEL, NULL,IDB_RIGHTBUTTON_DIS)) { TRACE0("Failed to load RightButton bitmap\n"); return FALSE; } RightButton.EnableWindow(FALSE); } //下面加入Info按钮 if (StatusFlags & ENHANCEBAR_INFO) { if (!InfoButton.Create("",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, ButtonRect,this,IDC_INFOBUTTON)) { TRACE0("Failed to create InfoButton\n"); return FALSE; } if (!InfoButton.LoadBitmaps(IDB_INFO,IDB_INFO_SEL,NULL,IDB_INFO_DIS)) { TRACE0("Failed to load InfoButton bitmap\n"); return FALSE; } InfoButton.EnableWindow(FALSE); } return TRUE; } BOOL CEnhanceStatusBar::AddProgressControl() { if (StatusFlags & ENHANCEBAR_PROGRESS) { //设置ProgressRect CRect ProgressRect; ProgressRect.left = 100; ProgressRec