您的位置:首页 > 编程语言

窗口框架编程小结

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: