vc关于窗口框架和状态栏的知识

  • 发表于
  • VC

1、在窗口框架创建之前改变窗口的大小、类型等。
在函数BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中添加以下代码:
cs.cx=200;
cs.cy=200;
cs.style=WS_OVERLAPPEDWINDOW;
//cs.style&=~FWS_ADDTOTITLE;
cs.lpszName="www.baidu.com";
2、在窗口框架创建之后改变窗口的大小、类型等。

在函数int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加以下代码:
//SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);
SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_STYLE)&~WS_MAXIMIZEBOX);
3、在窗口框架创建之前改变窗口的光标、背景、图标。
第一种方法:重新创建一个窗口类型。
在函数BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中添加以下代码:
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_HELP);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=AfxGetInstanceHandle();
wndcls.lpfnWndProc=::DefWindowProc;
wndcls.lpszClassName="predator.cn";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;

RegisterClass(&wndcls);
cs.lpszClass="predator.cn";
在函数BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)中添加以下代码:
BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
第二种方法:利用函数
在函数BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)中添加以下代码:
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,0,LoadIcon(NULL,IDI_WARNING));
在函数BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)中添加以下代码:
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0);
4、在窗口框架创建之后改变光标、背景、图标。
在CMainFrame类中添加成员变量 HICON m_hIcons[8],在源文件中填写:
extern CStyleApp theApp;
在函数int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加代码如下:
m_hIcons[0]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON1));
m_hIcons[1]=LoadIcon(theApp.m_hInstance,MAKEINTRESOURCE(IDI_ICON2));
m_hIcons[2]=LoadIcon(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDI_ICON3));
m_hIcons[3]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON4));
m_hIcons[4]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON5));
m_hIcons[5]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON6));
m_hIcons[6]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON7));
m_hIcons[7]=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON8));

SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[0]); //设置初始光标。
SetTimer(1,100,NULL);
添加消息处理函数:
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)m_hIcons[index]);
index=++index%8;
CFrameWnd::OnTimer(nIDEvent);
}
5、创建工具栏、在菜单栏中实现显示和隐藏工具栏:
插入工具栏资源,创建工具栏类对象。
在函数int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加代码;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[0]);
SetTimer(1,100,NULL);
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
}
m_newToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_newToolBar);
创建菜单项命令:在消息相应函数中实现:
第一种方法:
void CMainFrame::OnNewToolbar()
{
// TODO: Add your command handler code here
if(m_newToolBar.IsWindowVisible())
{
m_newToolBar.ShowWindow(SW_HIDE);
}
else
{
m_newToolBar.ShowWindow(SW_SHOW);
}
RecalcLayout();
m_newToolBar.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_newToolBar);
}
第二种方法:
void CMainFrame::OnNewToolbar()
{
// TODO: Add your command handler code here
ShowControlBar(&m_newToolBar,!m_newToolBar.IsWindowVisible(),FALSE);
}
为菜单项增加复选标记,为IDM_NEW_TOOLBAR命令添加UPDATE_COMMAND_UI命令响应,消息处理函数如下:
void CMainFrame::OnUpdateNewToolbar(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());
}
VC状态栏的修改:
在状态栏中添加时钟窗格和进度栏窗格:
在资源中插入两个字符串资源IDS_TIMER和IDS_PROGESS。
在状态栏类中的指示器数组中修改:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
IDS_TIMER,
IDS_PROGESS,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
在状态栏中添加时钟窗格:
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CTime t=CTime::GetCurrentTime();
CString str=t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str);
int index=0;
index=m_wndStatusBar.CommandToIndex(IDS_TIMER);
m_wndStatusBar.SetPaneInfo(index,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(index,str);
CFrameWnd::OnTimer(nIDEvent);
}
在状态栏中添加进度栏窗格:
在CMainFrame类中定义:CProgressCtrl m_progress;增加消息响应函数:
void CMainFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
CRect rect;
m_wndStatusBar.GetItemRect(1,rect);
if(!m_progress.m_hWnd)
{
m_progress.Create(WS_CHILD|WS_VISIBLE,rect,&m_wndStatusBar,123);
}
else
{
m_progress.MoveWindow(rect);
}
m_progress.SetPos(50);
SetTimer(1,1000,NULL);
// Do not call CFrameWnd::OnPaint() for painting messages
}
再增加响应函数:
void CMainFrame::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
m_progress.StepIt();
CFrameWnd::OnTimer(nIDEvent);
}
VC在状态栏中显示鼠标的坐标:
在源文件中显示鼠标信息:
void CHONGView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
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);
CView::OnMouseMove(nFlags, point);
}
创建应用程序的启动画面:
“工程”,“增加到工程”,“组件和控件”,选择vc++components,Splash.screen,在类中函数
SetTimer(1,750,NULL)中可以改变启动画面的停留时间