修改窗口的图标---------默认的MFC程序是一个MFC的图标,现在修改为带有感叹号的Warning!
修改窗口的背景---------默认的是一个白色背景,修改为黑色。
修改光标------------------默认的是箭头的光标,这里修改为十字光标,即叉叉。。
设计过程:
新建一个单文档的程序,然后在CMainFrame和CView文档中的PreCreateWindow函数中添加响应的修改代码。
代码如下:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,0,0,LoadIcon(NULL,IDI_WARNING));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return TRUE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
BOOL CTest12View::PreCreateWindow(CREATESTRUCT& cs)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
// 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);
}
只要使用的函数是AfxRegiseterWndClass修改窗口参数。具体的资料见API函数。
引申:CMainFrame类包含文件的框架,因此修改图标的代码放在该类中。
CXXView类表征了框架之上的视图,因此,应用程序画刷和光标的风格设置必须在该类的函数中。
AfxRegisterWndClass
LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 );
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Return Value
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
A null-terminated string containing the class name. You can pass this class name to the Create member function in CWnd or other CWnd-derived classes to create a window. The name is generated by the Microsoft Foundation Class Library.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Note The return value is a pointer to a static buffer. To save this string, assign it to a CString variable.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Parameters
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
nClassStyle
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Specifies the Windows class style or combination of styles, created by using the bitwise-OR (|) operator, for the window class. For a list of class styles, see theWNDCLASS structure in the Win32 SDK documentation. If NULL, the defaults will be set as follows:
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Sets the mouse style to CS_DBLCLKS, which sends double-click messages to the window procedure when the user double-clicks the mouse.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Sets the arrow cursor style to the Windows standard IDC_ARROW.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Sets the background brush to NULL, so the window will not erase its background.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Sets the icon to the standard, waving-flag Windows logo icon.
hCursor
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Specifies a handle to the cursor resource to be installed in each window created from the window class. If you use the default of 0, you will get the standard IDC_ARROW cursor.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
hbrBackground
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Specifies a handle to the brush resource to be installed in each window created from the window class. If you use the default of 0, you will have a NULL background brush, and your window will, by default, not erase its background while processingWM_ERASEBKGND.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
hIcon
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Specifies a handle to the icon resource to be installed in each window created from the window class. If you use the default of 0, you will get the standard, waving-flag Windows logo icon.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Remarks
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
The Microsoft Foundation Class Library automatically registers several standard window classes for you. Call this function if you want to register your own window classes.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
The name registered for a class by AfxRegisterWndClass depends solely on the parameters. If you call AfxRegisterWndClass multiple times with identical parameters, it only registers a class on the first call. Subsequent calls to AfxRegisterWndClass with identical parameters simply return the already-registered classname.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
If you call AfxRegisterWndClass for multiple CWnd-derived classes with identical parameters, instead of getting a separate window class for each class, each class shares the same window class. This can cause problems if the CS_CLASSDC class style is used. Instead of multiple CS_CLASSDC window classes, you end up with one CS_CLASSDC window class, and all C++ windows that use that class share the same DC. To avoid this problem, call AfxRegisterClass to register the class.
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Example
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
CString strMyClass;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
// load stock cursor, brush, and icon for
// my own window class
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
try
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
strMyClass = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
}
catch (CResourceException* pEx)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
AfxMessageBox(
_T("Couldn't register class! (Already registered?)"));
pEx->Delete();
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2010-02-16 16:38
deercoder 阅读(823)
评论(0) 编辑 收藏 引用