MFC(窗口,菜单栏,状态等的风格,孙鑫C++第九讲笔记整理)

1.修改外观和图标可以在MainFrm中进行,而修改背景和光标只能在View中进行。为什么?因为view的显示挡在了MainFrame的前面。

a.在MainFrame中

     PreCreateWindow()中,在窗口创建之前,用重新注册窗口类的方法,比较麻烦。在PreCreateWindow()中修改

     也可以用简单的方法,用全局函数

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

// LoadIcon(NULL,IDI_WARNING));

    在窗口创建之后,在OnCreate()中修改

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

b.在View中

   PreCreateWindow()中

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

// LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),NULL);

cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);

   OnCreate()中

SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));

SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));

2.创建一个不断变化的图标。用定时器和SetClassLong完成

a.准备三个图标文件,放在RES文件夹,Insert->Resource-三个图标,

b.在CMainFrame中增加图标句柄数组,m_hIcons[3]

m_hIcons[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));//MAKEINTRESOURCE是一个宏,它将整数转化为Win32的资源类型,简单的说它是一个类型转换#define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))

m_hIcons[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));//此处需要用到theAPP对象,故要在文件中声明extern CStyleApp theApp;

m_hIcons[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));

然后将其初始化

c.然后在定时器中实现

3.工具栏的编程

a.加入分隔符的方法,向右拖动即可;

b.删除按纽的方法,拖出即可。

4.创建一个新的工具栏的方法

a.插入一个工具栏,画出其图形。

b.在头文件中,定义CToolBar m_newToolBar

c.在MainFrm.cpp的OnCreate()中调用

if (!m_newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT

| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

!m_newToolBar.LoadToolBar(IDR_TOOLBAR1))

{

TRACE0("Failed to create toolbar\n");

return -1;     // fail to create

d.点击“新的工具栏”菜单时,隐藏工具栏。两种方法

第一种/*if(m_newToolBar.IsWindowVisible())

{

m_newToolBar.ShowWindow(SW_HIDE);

}

else

{

m_newToolBar.ShowWindow(SW_SHOW);

}

RecalcLayout();

DockControlBar(&m_newToolBar);*/

第二种ShowControlBar(&m_newToolBar,!m_newToolBar.IsWindowVisible(),FALSE);

e.将菜单增加复选标记。在OnUpdateUI中加入代码

   pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());

5.状态栏编程

a.Indicator[]数组中有状态栏的信息

如果要增加,可以在String Table中加入一个IDS_Timer,然后将其加入到[]中。

b.在时间栏显示时间,代码略,比较简单

6.进度栏

a.增加成员变量,CProgressCtrl m_progress

b.OnCreate中m_progress.Create(WS_CHILD | WS_VISIBLE,// | PBS_VERTICAL,

rect,&m_wndStatusBar,123);

m_progress.SetPos(50);*/ c.将其创建到状态栏的方法!如果在OnCreate()中创建,则不成立,因为获取矩形大小时失败。

   解决办法,用自定义消息:

   在MainFrm.h中#define UM_PROGRESS WM_USER+1

afx_msg void OnProgress();

   在MainFrm.cpp中

ON_MESSAGE(UM_PROGRESS,OnProgress)

然后实现这个函数

void CMainFrame::OnProgress()

{

CRect rect;

m_wndStatusBar.GetItemRect(2,&rect);

m_progress.Create(WS_CHILD | WS_VISIBLE | PBS_SMOOTH,

rect,&m_wndStatusBar,123);

m_progress.SetPos(50);

}

    最后在OnCreate中调用PostMessage(UM_PROGRESS);//不能用SendMessage()

  d.解决重绘时进度栏改变的问题。在OnPain()中重写代码

CRect rect;

m_wndStatusBar.GetItemRect(2,&rect);

m_progress.Create(WS_CHILD | WS_VISIBLE | PBS_SMOOTH,

rect,&m_wndStatusBar,123);

m_progress.SetPos(50);

然后在定时器消息处理函数中加入

m_progress.StepIt();

  e.显示鼠标位置。在View中增加OnMouseMove()处理函数

CString str;

str.Format("x=%d,y=%d",point.x,point.y);

//((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(str);

//((CMainFrame*)GetParent())->SetMessageText(str);

//((CMainFrame*)GetParent())->GetMessageBar()->SetWindowText(str);

GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);

7.加入启动画面

Project-Component and ->Visual C++ Components->SplashScreen->插入

代码如下:

  1. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
  2.     if( !CFrameWnd::PreCreateWindow(cs) ) 
  3.         return FALSE; 
  4.     // TODO: Modify the Window class or styles here by modifying 
  5.     //  the CREATESTRUCT cs 
  6.  
  7.  
  8.  
  9.     //cs.cx=300;//直接改变窗口的大小 
  10.     //cs.cy=200; 
  11.  
  12.     //cs.x=0;//改变窗口显示位置 
  13.     //cs.y=0; 
  14.  
  15.     //cs.style&=~FWS_ADDTOTITLE ;//MFC窗口默认的风格是WS_OVERLAPPEDWINDOW & FWS_ADDTOTITLE  
  16.     //cs.style=WS_OVERLAPPEDWINDOW; 
  17.     //cs.lpszName="634455283@qq.com";//有以上两条其中一条,改变窗口的标题 
  18.  
  19.     /*WNDCLASS wndclass;
  20.     wndclass.cbClsExtra=0;
  21.     wndclass.cbWndExtra=0;
  22.     wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//被客户区的VIEW窗体覆盖了
  23.     wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
  24.     wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
  25.     wndclass.hInstance=AfxGetInstanceHandle();//使用全局函数,获取运用程序实例
  26.     wndclass.lpfnWndProc=::DefWindowProc;//使用默认的,Cwnd也有DefWindowProc函数,所以要加一个作用域操作符
  27.     wndclass.lpszClassName="MRzhang";
  28.     wndclass.lpszMenuName=NULL;
  29.     wndclass.style=CS_HREDRAW|CS_VREDRAW;
  30.     RegisterClass(&wndclass);
  31.     
  32.     cs.lpszClass="MRzhang";//名字要和注册的窗口名字一样,这样才能生效*/ 
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.     return TRUE; 
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs){	if( !CFrameWnd::PreCreateWindow(cs) )		return FALSE;	// TODO: Modify the Window class or styles here by modifying	//  the CREATESTRUCT cs	//cs.cx=300;//直接改变窗口的大小	//cs.cy=200;	//cs.x=0;//改变窗口显示位置	//cs.y=0;	//cs.style&=~FWS_ADDTOTITLE ;//MFC窗口默认的风格是WS_OVERLAPPEDWINDOW & FWS_ADDTOTITLE 	//cs.style=WS_OVERLAPPEDWINDOW;	//cs.lpszName="634455283@qq.com";//有以上两条其中一条,改变窗口的标题	/*WNDCLASS wndclass;	wndclass.cbClsExtra=0;	wndclass.cbWndExtra=0;	wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//被客户区的VIEW窗体覆盖了	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);	wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);	wndclass.hInstance=AfxGetInstanceHandle();//使用全局函数,获取运用程序实例	wndclass.lpfnWndProc=::DefWindowProc;//使用默认的,Cwnd也有DefWindowProc函数,所以要加一个作用域操作符	wndclass.lpszClassName="MRzhang";	wndclass.lpszMenuName=NULL;	wndclass.style=CS_HREDRAW|CS_VREDRAW;	RegisterClass(&wndclass);		cs.lpszClass="MRzhang";//名字要和注册的窗口名字一样,这样才能生效*/	return TRUE;}


CMainFrom OnCreate

  1. //SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW|WS_VSCROLL);//改变窗口风格,有滚动条 
//SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW|WS_VSCROLL);//改变窗口风格,有滚动条


  1. int CWindowTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
  2.     if (CView::OnCreate(lpCreateStruct) == -1) 
  3.         return -1; 
  4.      
  5.     // TODO: Add your specialized creation code here 
  6.      
  7.     //SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_CROSS));//讲光标设成十字 
  8.     return 0; 
int CWindowTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) {	if (CView::OnCreate(lpCreateStruct) == -1)		return -1;		// TODO: Add your specialized creation code here		//SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_CROSS));//讲光标设成十字	return 0;}


一个函数,既可以在CMainFram中用,也可以在CXXView中用(PreCreateWindow)

  1. LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 );  
LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 ); 


CMainFram中

  1. //cs.lpszClass=AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW,0,0,LoadIcon(NULL,IDI_ERROR)); 
  2. //cs.lpszClass=AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW); 
	//cs.lpszClass=AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW,0,0,LoadIcon(NULL,IDI_ERROR));	//cs.lpszClass=AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW);


CXXView中

  1. //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0); 
  2.     //cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW); 
//cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0);	//cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW);


三种获取Hinstance的方法

  1. hIcon[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1)); 
  2. hIcon[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2)); 
  3. hIcon[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3)); 
  4.  
  5.  
  6. SetClassLong(m_hWnd,          // window handle  
  7.     GCL_HICON,              // changes icon  
  8.     (LONG) LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON1))); 
  9.  
  10. SetTimer(1,1000,NULL); 
	hIcon[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));	hIcon[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));	hIcon[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));	SetClassLong(m_hWnd,          // window handle 		GCL_HICON,              // changes icon 		(LONG) LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON1)));	SetTimer(1,1000,NULL);


每个XXAPP中都有一个全局变量theApp,在OnCreate之前,extern CXXApp theApp;声明

  1. void CMainFrame::OnTimer(UINT nIDEvent)  
  2.     // TODO: Add your message handler code here and/or call default 
  3.      
  4.     static int index=1; 
  5.     SetClassLong(m_hWnd,GCL_HICON,(LONG)hIcon[index]); 
  6.     index=++index%3; 
  7.     CFrameWnd::OnTimer(nIDEvent); 
void CMainFrame::OnTimer(UINT nIDEvent) {	// TODO: Add your message handler code here and/or call default		static int index=1;	SetClassLong(m_hWnd,GCL_HICON,(LONG)hIcon[index]);	index=++index%3;	CFrameWnd::OnTimer(nIDEvent);}


改变的是小图标

工具栏,小工具,自己创建一个:

ID和自己创建的菜单选项的ID是一样的

添加自己的工具栏

  1. protected:  // control bar embedded members 
  2.     CStatusBar  m_wndStatusBar; 
  3.     CToolBar    m_wndToolBar; 
  4.     CToolBar newToolBar; 
protected:  // control bar embedded members	CStatusBar  m_wndStatusBar;	CToolBar    m_wndToolBar;	CToolBar newToolBar;


CMAINFRAM中 OnCreate中添加

  1. if (!newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT 
  2.         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
  3.         !newToolBar.LoadToolBar(IDR_TOOLBAR1)) 
  4.     { 
  5.         TRACE0("Failed to create toolbar\n"); 
  6.         return -1;      // fail to create 
  7.     } 
  8.  
  9.     newToolBar.EnableDocking(CBRS_ALIGN_ANY); 
  10.     //EnableDocking(CBRS_ALIGN_ANY); 
  11.     DockControlBar(&newToolBar); 
if (!newToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_RIGHT		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||		!newToolBar.LoadToolBar(IDR_TOOLBAR1))	{		TRACE0("Failed to create toolbar\n");		return -1;      // fail to create	}	newToolBar.EnableDocking(CBRS_ALIGN_ANY);	//EnableDocking(CBRS_ALIGN_ANY);	DockControlBar(&newToolBar);


  1. void CMainFrame::OnNewViewToolbar()  
  2.     // TODO: Add your command handler code here 
  3.     if(newToolBar.IsWindowVisible()) 
  4.     { 
  5.         newToolBar.ShowWindow(SW_HIDE); 
  6.     } 
  7.     else 
  8.     { 
  9.         newToolBar.ShowWindow(SW_SHOW); 
  10.     } 
  11.     RecalcLayout();//重新调整工具栏的位置 
  12.     DockControlBar(&newToolBar);//使得把工具栏拖出来后,还能放回工具栏 
void CMainFrame::OnNewViewToolbar() {	// TODO: Add your command handler code here	if(newToolBar.IsWindowVisible())	{		newToolBar.ShowWindow(SW_HIDE);	}	else	{		newToolBar.ShowWindow(SW_SHOW);	}	RecalcLayout();//重新调整工具栏的位置	DockControlBar(&newToolBar);//使得把工具栏拖出来后,还能放回工具栏}


  1. void CMainFrame::OnNewViewToolbar()  
  2.     // TODO: Add your command handler code here 
  3.  
  4.  
  5.  
  6.     /*if(newToolBar.IsWindowVisible())
  7.     {
  8.         newToolBar.ShowWindow(SW_HIDE);
  9.     }
  10.     else
  11.     {
  12.         newToolBar.ShowWindow(SW_SHOW);
  13.     }
  14.     RecalcLayout();//重新调整工具栏的位置
  15.     
  16.     DockControlBar(&newToolBar);//使得把工具栏拖出来后,还能放回工具栏
  17.     */ 
  18.  
  19.     ShowControlBar(&newToolBar,!newToolBar.IsWindowVisible(),FALSE);//直接解决了 隐藏,显示浮动工具栏在原处的问题 
  20.      
void CMainFrame::OnNewViewToolbar() {	// TODO: Add your command handler code here	/*if(newToolBar.IsWindowVisible())	{		newToolBar.ShowWindow(SW_HIDE);	}	else	{		newToolBar.ShowWindow(SW_SHOW);	}	RecalcLayout();//重新调整工具栏的位置		DockControlBar(&newToolBar);//使得把工具栏拖出来后,还能放回工具栏	*/	ShowControlBar(&newToolBar,!newToolBar.IsWindowVisible(),FALSE);//直接解决了 隐藏,显示浮动工具栏在原处的问题	}


  1. void CMainFrame::OnUpdateNewViewToolbar(CCmdUI* pCmdUI)  
  2.     // TODO: Add your command update UI handler code here 
  3.     /*static int i=1;
  4.     ++i;
  5.     if(i%2)
  6.     {
  7.     GetMenu()->GetSubMenu(2)->CheckMenuItem(2,MF_CHECKED|MF_BYPOSITION);
  8.     }
  9.     else
  10.     {
  11.         GetMenu()->GetSubMenu(2)->CheckMenuItem(2,MF_UNCHECKED|MF_BYPOSITION);
  12.     }*/ 
  13.  
  14.     pCmdUI->SetCheck(newToolBar.IsWindowVisible()); 
  15.      
void CMainFrame::OnUpdateNewViewToolbar(CCmdUI* pCmdUI) {	// TODO: Add your command update UI handler code here	/*static int i=1;	++i;	if(i%2)	{	GetMenu()->GetSubMenu(2)->CheckMenuItem(2,MF_CHECKED|MF_BYPOSITION);	}	else	{		GetMenu()->GetSubMenu(2)->CheckMenuItem(2,MF_UNCHECKED|MF_BYPOSITION);	}*/	pCmdUI->SetCheck(newToolBar.IsWindowVisible());	}


添加工具栏事件

  1. void CStyleView::OnRectangle()  
  2.     // TODO: Add your command handler code here 
  3.     CClientDC ccdc(this); 
  4.     CBrush cb(RGB(255,0,0)); 
  5.     ccdc.SelectObject(&cb); 
  6.     ccdc.Rectangle(200,200,300,300); 
  7.     ccdc.TextOut(200,180,"Rectangle"); 
  8.  
  9. void CStyleView::OnEllipse()  
  10.     // TODO: Add your command handler code here 
  11.     CClientDC ccdc(this); 
  12.     CBrush cpen(RGB(255,0,0)); 
  13.     ccdc.SelectObject(&cpen); 
  14.     ccdc.Ellipse(400,200,500,350); 
  15.     ccdc.TextOut(400,180,"Ellipse"); 
  16.  
  17. void CStyleView::OnCircle()  
  18.     // TODO: Add your command handler code here 
  19.     CClientDC ccdc(this); 
  20.     CBrush cpen(RGB(255,0,0)); 
  21.     ccdc.SelectObject(&cpen); 
  22.     ccdc.Ellipse(600,200,700,300); 
  23.     ccdc.TextOut(600,180,"Circle"); 
void CStyleView::OnRectangle() {	// TODO: Add your command handler code here	CClientDC ccdc(this);	CBrush cb(RGB(255,0,0));	ccdc.SelectObject(&cb);	ccdc.Rectangle(200,200,300,300);	ccdc.TextOut(200,180,"Rectangle");}void CStyleView::OnEllipse() {	// TODO: Add your command handler code here	CClientDC ccdc(this);	CBrush cpen(RGB(255,0,0));	ccdc.SelectObject(&cpen);	ccdc.Ellipse(400,200,500,350);	ccdc.TextOut(400,180,"Ellipse");}void CStyleView::OnCircle() {	// TODO: Add your command handler code here	CClientDC ccdc(this);	CBrush cpen(RGB(255,0,0));	ccdc.SelectObject(&cpen);	ccdc.Ellipse(600,200,700,300);	ccdc.TextOut(600,180,"Circle");}


状态栏:

String Tablet添加String iD

添加到数组

  1. static UINT indicators[] = 
  2.     ID_SEPARATOR,           // status line indicator 
  3.     ID_SHOWTIME, 
  4.     ID_SHOWPROGRESS, 
  5.     ID_INDICATOR_CAPS, 
  6.     ID_INDICATOR_NUM, 
  7.     ID_INDICATOR_SCRL, 
  8. }; 
static UINT indicators[] ={	ID_SEPARATOR,           // status line indicator	ID_SHOWTIME,	ID_SHOWPROGRESS,	ID_INDICATOR_CAPS,	ID_INDICATOR_NUM,	ID_INDICATOR_SCRL,};


状态栏有显示了:

  1. CTime t=CTime::GetCurrentTime(); 
  2.     CString strtime=t.Format("%H:%M:%S"); 
  3.  
  4.     m_wndStatusBar.SetPaneText(1,strtime);//所以是在indicators数组中的索引 
CTime t=CTime::GetCurrentTime();	CString strtime=t.Format("%H:%M:%S");	m_wndStatusBar.SetPaneText(1,strtime);//所以是在indicators数组中的索引


状态中的格子容不下这个字符串,所以用GetTextExtent来设置格子的大小

  1. CTime t=CTime::GetCurrentTime(); 
  2.     CString strtime=t.Format("%H:%M:%S"); 
  3.  
  4.     m_wndStatusBar.SetPaneText(1,strtime);//所以是在indicators数组中的索引 
  5.  
  6.     CClientDC ccdc(this); 
  7.     CSize csize=ccdc.GetTextExtent(strtime); 
  8.     m_wndStatusBar.SetPaneInfo(1,ID_SHOWTIME,SBPS_NORMAL,csize.cx); 
CTime t=CTime::GetCurrentTime();	CString strtime=t.Format("%H:%M:%S");	m_wndStatusBar.SetPaneText(1,strtime);//所以是在indicators数组中的索引	CClientDC ccdc(this);	CSize csize=ccdc.GetTextExtent(strtime);	m_wndStatusBar.SetPaneInfo(1,ID_SHOWTIME,SBPS_NORMAL,csize.cx);


这样就能正常显示时间了,可是时间却是静态的,这个时候可以用一个定时器,定时的去显示时间。

把上面的代码也复制到OnTimer中,就可以是先动态显示时间了。

  1. cpg.Create(WS_CHILD|WS_VISIBLE,CRect(300,300,450,330),this,123); 
  2.     cpg.SetPos(50); 
cpg.Create(WS_CHILD|WS_VISIBLE,CRect(300,300,450,330),this,123);	cpg.SetPos(50);


OnCreate中没有反应,是因为窗口都还没有建立,怎么获取状态栏的大小?

  1. CRect crect; 
  2.     m_wndStatusBar.GetItemRect(2,&crect); 
  3.     cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123); 
CRect crect;	m_wndStatusBar.GetItemRect(2,&crect);	cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123);


所以尝试着用SendMessage或PostMessage去尝试一下,是否可以在窗口创建之后获取状态栏的大小:

自定义消息响应函数,(跟前面菜单按钮自定义消息步骤是一样的)

  1. #define WM_CPG WM_USER+1 
#define WM_CPG WM_USER+1
  1. afx_msg void Oncpg(); 
afx_msg void Oncpg();
  1. ON_MESSAGE(WM_CPG,Oncpg) 
ON_MESSAGE(WM_CPG,Oncpg)


  1. void CMainFrame::Oncpg() 
  2.     CRect crect; 
  3.     m_wndStatusBar.GetItemRect(2,&crect); 
  4.     cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123); 
  5.     cpg.SetPos(50); 
void CMainFrame::Oncpg(){	CRect crect;	m_wndStatusBar.GetItemRect(2,&crect);	cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123);	cpg.SetPos(50);}


在OnCreate中最后添加:

SendMessage(WM_CPG);

运行结果还是跟原来一样,没有显示,这是什么原因呢,原来SendMessage发送消息的时候,也是先调用了WM_CPG,还是在窗口调用前获取状态栏的大小,这当然也是不行的,把SendMessage(WM_CPG)改成PostMessage(WM_CPG);试试


这个时候就看到,原来PostMessage是将消息投递到消息队列里面了。

这样就确保了先创建完窗体之后才获取状态栏的大小

但是改变窗口大小,进度条不会跟着移动。

把OnCpg方法的代码剪切到OnPaint方法中去,把PostMessage方法注释掉

但是这样,当窗口改变是,会发生异常,同一对象建立多次(跟前面对话框的添加按钮是一样的)

所以将代码改成这样就完美了

  1. void CMainFrame::OnPaint()  
  2.     CPaintDC dc(this); // device context for painting 
  3.      
  4.  
  5.     // TODO: Add your message handler code here 
  6.  
  7.     CRect crect; 
  8.     m_wndStatusBar.GetItemRect(2,&crect); 
  9.     if(!cpg.m_hWnd) 
  10.     { 
  11.  
  12.      
  13.     cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123); 
  14.     } 
  15.     else{ 
  16.  
  17.         cpg.MoveWindow(crect); 
  18.     } 
  19.     cpg.SetPos(50); 
  20.      
  21.     // Do not call CFrameWnd::OnPaint() for painting messages 
void CMainFrame::OnPaint() {	CPaintDC dc(this); // device context for painting		// TODO: Add your message handler code here	CRect crect;	m_wndStatusBar.GetItemRect(2,&crect);	if(!cpg.m_hWnd)	{		cpg.Create(WS_CHILD|WS_VISIBLE,crect,&m_wndStatusBar,123);	}	else{		cpg.MoveWindow(crect);	}	cpg.SetPos(50);		// Do not call CFrameWnd::OnPaint() for painting messages}


进度条可以设置步长SetStep  SetpIt进度走动  在OnTimer中添加 cpg.StepIt();

进度条的风格可以设置成一格格的,也可以设置成平滑的

在状态栏中显示鼠标的位置(有几种方法可以使用)在CXXView中添加鼠标移动事件

  1. void CStyleView::OnMouseMove(UINT nFlags, CPoint point)  
  2.     // TODO: Add your message handler code here and/or call default 
  3.      
  4.     CString cstring; 
  5.     cstring.Format("x=%d,y=%d",point.x,point.y); 
  6.  
  7.     //((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(cstring); 
  8.     //((CMainFrame*)GetParent())->m_wndStatusBar.SetPaneText(0,cstring); 
  9.     //((CMainFrame*)GetParent())->SetMessageText(cstring); 
  10.     GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(cstring);//父窗口通过ID获取子孙窗口 
  11.  
  12.     CView::OnMouseMove(nFlags, point); 
void CStyleView::OnMouseMove(UINT nFlags, CPoint point) {	// TODO: Add your message handler code here and/or call default		CString cstring;	cstring.Format("x=%d,y=%d",point.x,point.y);	//((CMainFrame*)GetParent())->m_wndStatusBar.SetWindowText(cstring);	//((CMainFrame*)GetParent())->m_wndStatusBar.SetPaneText(0,cstring);	//((CMainFrame*)GetParent())->SetMessageText(cstring);	GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(cstring);//父窗口通过ID获取子孙窗口	CView::OnMouseMove(nFlags, point);}


设置启动画面:

启动画面是一个位图,可以修改它显示的时间:

 

 

转自:http://blog.csdn.net/zh634455283/article/details/7870819

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值