定制应用程序外观
1.在应用程序创建之前修改它的外观和大小
例如修改窗口大小
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=300;
return TRUE;
}
修改单文档工程生成窗口的标题
在修改单文档工程窗口标题之前
要将 FWS_ADDTOTITLE去掉
例:
cs.style &=~FWS_ADDTOTITLE;
或
cs.style=WS_OVERLAPPEDWINDOW;
cs.lpszName="xxxxxxx";
2.在窗口后创建之后修改
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
SetWindowLong(this->m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);
//修改样式 去掉标题
}
还可以用GetWindowLong得到窗口样式
3.窗口创建前修改窗口的光标 、图标 背景
因为 光标、图标 背景 这些是在设计窗口类完成的。
我们可以自定义一个窗口类然后去注册
例: 在框架窗口的PreCreateWindow中
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the 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="sunxin.org";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
cs.lpszClass="sunxin.org";
return TRUE;
}
我们如果想改变 光标 图标这些 应该在视类中 因为那一大块的白色区域是视类的
例:视类PreCreateWindow中
BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass="sunxin.org";
return CView::PreCreateWindow(cs);
}
便捷的方法
AfxRegisterWndClass();
LPCTSTR AFXAPI AfxRegisterWndClass(
UINT nClassStyle,
HCURSOR hCursor = 0,
HBRUSH hbrBackground = 0,
HICON hIcon = 0 );
返回值:
一个以null结尾的字符串,其中包含了类名。你可以将这个类名传递给CWnd或其派生类的成员函数Create以创建一个窗口。这个名字是由微软基础类库生成的。
注意:
返回值是指向一个静态缓冲区的指针。如果要保存这个字符串,将它赋给一个CString变量。
BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass=AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,LoadCursor(NULL,IDC_CROSS),(HBRUSH)GetStockObject(BLACK_BRUSH),0);
return CView::PreCreateWindow(cs);
}
4.窗口创建后修改窗口的光标 、图标 背景
SetClassLong();
int CStyleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));
SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));
return 0;
}