窗口框架编程小结
2007-07-23 08:10
295 查看
注册窗口类,主要修改图标,背景和光标
在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="Persuper";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
cs.lpszClass="Persuper";
还有一种方法修改图标,光标,背景颜色。
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,0,0,
LoadIcon(NULL,IDI_WARNING));
或者 cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);
在创建后修改背景和图标
SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));
SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));
动态修改图标,背景的方法:在定义图标数组,在Oncreate函数中加载图标
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));
其中包含了3中取得当前程序的实例,使用theApp需要添加说明
extern CStyleApp theApp;
表示在外部源文件中定义的变量。
最后设置定时器 SetTimer(1,1000,NULL);
另外:
void CMainFrame::OnTimer(UINT nIDEvent)
{
static int index=1;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[index]);
index=++index%3;
CFrameWnd::OnTimer(nIDEvent);
}
删除工具栏上的按钮,左键按住,拖出工具栏区域即可!
工具栏停靠设置:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); 工具栏自己可以停靠
EnableDocking(CBRS_ALIGN_ANY); 本对象可以被停靠
DockControlBar(&m_wndToolBar);设置该工具栏被停靠
判断工具栏状态,在菜单事件响应中添加:
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);
这种方式更好。
为菜单项添加复选标记
void CMainFrame::OnUpdateViewNewtool(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());
}
状态栏
状态指示器:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
IDS_TIMER,
IDS_PROGRESS,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
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);
或者
CTime t=CTime::GetCurrentTime();
CString str=t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str); // 得到字符串在面板上显示的长度
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(1,str);
判断句柄是否有值
void CMainFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
m_wndStatusBar.GetItemRect(2,&rect);
if(!m_progress.m_hWnd)
m_progress.Create(WS_CHILD | WS_VISIBLE ,//| PBS_SMOOTH,
rect,&m_wndStatusBar,123);
else
m_progress.MoveWindow(rect);
m_progress.SetPos(50);
// Do not call CFrameWnd::OnPaint() for painting messages
}
状态栏文本设置
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);
在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="Persuper";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
cs.lpszClass="Persuper";
还有一种方法修改图标,光标,背景颜色。
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,0,0,
LoadIcon(NULL,IDI_WARNING));
或者 cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);
在创建后修改背景和图标
SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));
SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));
动态修改图标,背景的方法:在定义图标数组,在Oncreate函数中加载图标
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));
其中包含了3中取得当前程序的实例,使用theApp需要添加说明
extern CStyleApp theApp;
表示在外部源文件中定义的变量。
最后设置定时器 SetTimer(1,1000,NULL);
另外:
void CMainFrame::OnTimer(UINT nIDEvent)
{
static int index=1;
SetClassLong(m_hWnd,GCL_HICON,(LONG)m_hIcons[index]);
index=++index%3;
CFrameWnd::OnTimer(nIDEvent);
}
删除工具栏上的按钮,左键按住,拖出工具栏区域即可!
工具栏停靠设置:
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); 工具栏自己可以停靠
EnableDocking(CBRS_ALIGN_ANY); 本对象可以被停靠
DockControlBar(&m_wndToolBar);设置该工具栏被停靠
判断工具栏状态,在菜单事件响应中添加:
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);
这种方式更好。
为菜单项添加复选标记
void CMainFrame::OnUpdateViewNewtool(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_newToolBar.IsWindowVisible());
}
状态栏
状态指示器:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
IDS_TIMER,
IDS_PROGRESS,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
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);
或者
CTime t=CTime::GetCurrentTime();
CString str=t.Format("%H:%M:%S");
CClientDC dc(this);
CSize sz=dc.GetTextExtent(str); // 得到字符串在面板上显示的长度
m_wndStatusBar.SetPaneInfo(1,IDS_TIMER,SBPS_NORMAL,sz.cx);
m_wndStatusBar.SetPaneText(1,str);
判断句柄是否有值
void CMainFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rect;
m_wndStatusBar.GetItemRect(2,&rect);
if(!m_progress.m_hWnd)
m_progress.Create(WS_CHILD | WS_VISIBLE ,//| PBS_SMOOTH,
rect,&m_wndStatusBar,123);
else
m_progress.MoveWindow(rect);
m_progress.SetPos(50);
// Do not call CFrameWnd::OnPaint() for painting messages
}
状态栏文本设置
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);
相关文章推荐
- GTK编程基础-----框架、比例框架、分栏窗口构件
- (转载)VS2010/MFC编程入门之四十一(文档、视图和框架:分割窗口)
- VC编程中关于新建的框架窗口的销毁的一个心得
- Windows编程之基本窗口控件小结
- VC编程中关于新建的框架窗口的销毁的一个心得
- MFC单文档框架编程(一): 分隔窗口的实现
- 在MDI主框架窗口中添加位图(转载自CSDN VC编程知识总结)
- VC编程中关于新建的框架窗口的销毁的一个心得
- MFC单文档框架编程(一): 分隔窗口的实现
- VS2010/MFC编程入门之四十一(文档、视图和框架:分割窗口)
- 积累的VC编程小技巧之框架窗口及其他
- Windows编程 从消息窗口到基本窗口 游戏循环窗口框架的简单实现
- VC编程小技巧之框架窗口及其他
- VC编程小技巧之框架窗口及其他
- VS2013/MFC编程入门之三十八(文档、视图和框架:分割窗口)
- VS2010/MFC编程入门之四十一(文档、视图和框架:分割窗口)
- VS2013/MFC编程入门之三十八(文档、视图和框架:分割窗口)
- VC++编程获取窗口句柄的方法小结
- [Visual C++系列]3.1 文档 视图 框架窗口间的关系和消息传送规律
- .NET:不显示窗口的应用程序框架