第九课

如何修改MFC AppWizard向导生成的框架程序的外观和大小,修改图标、光标、背景的三种方法。如何增加和删除工具栏按钮,如何给应用程序增加工具栏,如何显示和隐藏工具栏。定制状态栏,在状态栏中添加时钟显示,CTime类及其用法。在状态栏中添加进度条(主窗口产生后立即产生进度条的巧妙思想,不能在 OnCreate函数中直接处理,要用到自定义消息的方法)。鼠标坐标显示,在CView中获取状态栏对象的几种方式。如何为应用程序添加启动画面。

修改窗口样式外观,注册自己的窗口类

只能改变图标

 

 
  
  1. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
  2.     if( !CFrameWnd::PreCreateWindow(cs) ) 
  3.         return FALSE; 
  4.      
  5. //  cs.cx=300; 
  6. //  cs.cy=200; 
  7.     //cs.style&=~FWS_ADDTOTITLE; 
  8.     //cs.style=WS_OVERLAPPEDWINDOW; 
  9.     //cs.lpszName="http://www.sunxin.org"; 
  10.     /*WNDCLASS wndcls; 
  11.     wndcls.cbClsExtra=0; 
  12.     wndcls.cbWndExtra=0; 
  13.     wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); 
  14.     wndcls.hCursor=LoadCursor(NULL,IDC_HELP); 
  15.     wndcls.hIcon=LoadIcon(NULL,IDI_ERROR); 
  16.     wndcls.hInstance=AfxGetInstanceHandle(); 
  17.     wndcls.lpfnWndProc=::DefWindowProc; 
  18.     wndcls.lpszClassName="sunxin.org"; 
  19.     wndcls.lpszMenuName=NULL; 
  20.     wndcls.style=CS_HREDRAW | CS_VREDRAW; 
  21.  
  22.     RegisterClass(&wndcls); 
  23.  
  24.     cs.lpszClass="sunxin.org";*/ 
  25.     //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,0,0, 
  26.     //  LoadIcon(NULL,IDI_WARNING)); 
  27.     //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW); 
  28.     return TRUE; 

 

在程序运行时修改程序窗口风格

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);

SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_STYLE)& ~WS_MAXIMIZEBOX);

/SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI_ERROR));


 
  
  1. 窗口的背景和光标在这里修改 
  2. BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs) 
  3.     // TODO: Modify the Window class or styles here by modifying 
  4.     //  the CREATESTRUCT cs 
  5.     //cs.lpszClass="sunxin.org"; 
  6.     //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, 
  7.     //  LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0); 
  8.     //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW); 
  9.     return CView::PreCreateWindow(cs); 

在框架窗口中只能修改图标,为了修改图标需要重写窗口类,太麻烦,MFC中提供了一个全局函数,AfxRegisterWndClass()修改、设定一个窗口类的类型,光标、背景、图标。返回一个注册成功的窗口类类名。如果只修改类的类型(wndcls.style),只要第一个参数,其它取默认值。

LPCTSTR AFXAPI AfxRegisterWndClass(

   UINT nClassStyle,

   HCURSOR hCursor = 0,

   HBRUSH hbrBackground = 0,

   HICON hIcon = 0

);

cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,0,0,

LoadIcon(NULL,IDI_WARNING));

获得当前实例号hInstance的三种方法

1.利用全局afx函数即AfxGetInstanceHandle(),在任何地方都可用,相对来说比较方便
2.在类的全局定义引入extern App的那个类(如CmyApptheApp,然后调用theApp.m_hInstance即可
3.利用全局AfxGetApp()函数,利用AfxGetApp()->m_hInstance即可!

hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
或者响应WM_CREATE 消息,hInstance=(LPCREATESTRUCT)lParam->hInstance;

创建一个CToolBar步骤

Create a toolbar resource.   创建一个资源

Construct the CToolBar object.创建一个对象在CMainFrame

Call the Create (or CreateEx) function to create the Windows toolbar and attach it to the CToolBar object.调用函数创建窗口然后吸附在主窗口上

Call LoadToolBar to load the toolbar resource. 加载资源

Otherwise, follow these steps:

Construct the CToolBar object.    创建对象

Call the Create (or CreateEx) function to create the Windows toolbar and attach it to the CToolBar object.调用函数创建窗口然后吸附在主窗口上

Call LoadBitmap to load the bitmap that contains the toolbar button p_w_picpaths.加载位图

Call SetButtons to set the button style and associate each button with an p_w_picpath in the bitmap.设置位图

 
  
  1. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中 
  2. if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 
  3.         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
  4.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 
  5.     { 
  6.         TRACE0("Failed to create toolbar\n"); 
  7.         return -1;      // fail to create 
  8.     } 
  9. m_newToolBar.EnableDocking(CBRS_ALIGN_ANY); 
  10.     DockControlBar(&m_newToolBar); 
  11.  
  12.  
  13. 自定义工具栏 
  14. void CMainFrame::OnViewNewtool()  
  15.     // TODO: Add your command handler code here 
  16.     /*if(m_newToolBar.IsWindowVisible())判断是否可视 
  17.     { 
  18.         m_newToolBar.ShowWindow(SW_HIDE); 
  19.     } 
  20.     else 
  21.     { 
  22.         m_newToolBar.ShowWindow(SW_SHOW); 
  23.     } 
  24.     RecalcLayout();调整工具栏的横条 
  25.  
  26.     DockControlBar(&m_newToolBar);*/停靠工具栏 
  27.     ShowControlBar(&m_newToolBar,!m_newToolBar.IsWindowVisible(),FALSE);这个函数可以实现上面的功能,比较好 
  28. 状态栏 
  29. 状态栏指示器数组 
  30. static UINT indicators[] = 
  31.     ID_SEPARATOR,           // status line indicator 
  32.     IDS_TIMER, 
  33.     IDS_PROGRESS, 
  34.     ID_INDICATOR_CAPS, 
  35.     ID_INDICATOR_NUM, 
  36.     ID_INDICATOR_SCRL, 
  37. }; 
  38.  
  39. CStatusBar    
  40. 加载面板字符串  
  41. int index=0; 
  42.     index=m_wndStatusBar.CommandToIndex(IDS_TIMER);获取面板索引值 
  43.     m_wndStatusBar.SetPaneInfo(index,IDS_TIMER,SBPS_NORMAL,sz.cx);设置指示器面板是信息 
  44.     m_wndStatusBar.SetPaneText(index,str); 
  45.     CSize sz=dc.GetTextExtent(str);得到字符串显示的宽度和高度 
  46.  
  47. Cprocgressctrl进度栏控件 
  48. CProgressCtrl   m_progress; 
  49. 设置进度控件到状态栏 
  50. CRect rect; 
  51.     m_wndStatusBar.GetItemRect(2,&rect); 
  52.     m_progress.Create(WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 
  53.         rect,&m_wndStatusBar,123); 
  54.     m_progress.SetPos(50); 
  55.     m_progress.StepIt();设置进度栏进度前进 
  56.  
  57.  
  58. SetMessageText(str);设置状态栏的的文本 
  59. //((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str); 
  60.     //((CMainFrame*)GetParent())->SetMessageText(str); 
  61.     //((CMainFrame*)GetParent())->GetMessageBar()->SetWindowText(str); 
  62. GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);得到子孙窗口 

启动画面和右键那样的弹出菜单都需要组件控件的支持。

CSplashWnd::ShowSplashScreen(this);启动画面