MFC改变程序的窗口外观

1.窗口创建之前,修改窗口的外观

CWnd::PreCreateWindow
virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
/*框架在即将创建窗口前调用框架类的 PreCreateWindow。
通过修改传递给 PreCreateWindow 的结构 CREATESTRUCT,应用程序可以更改用于创建窗口的属性。
Called by the framework before the creation of the Windows window attached to this CWnd object.
*/


struct tagCREATESTRUCT 
/*CREATESTRUCT结构体中成员变量的类型和个数和创建窗口的函数CreateWindowEx()的参数和个数完全一致,只是顺序相反。
The CREATESTRUCT structure defines the initialization parameters passed to the window procedure of an application. 
These members are identical to the parameters of the CreateWindowEx function. 
*/


//可以重写PreCreateWindow()虚函数,在函数内部通过修改CREATESTRUCT结构的引用cs,达到修改窗口的目的,就如同调用了CreateWindowEx()创建一个新的窗口。
//例如通过修改cs.cx和cs.cy来指定窗口的大小,但要注意当修改cs.lpszClass时,要先注册同名的WNDCLASS窗口类。
//在单文档界面 (SDI) 应用程序中,框架中的默认窗口样式是 WS_OVERLAPPEDWINDOW 和 FWS_ADDTOTITLE 样式的组合。(MSDN详细参考Frame-Window Styles)

    
demo1 (创建前指定窗口的大小和标题)

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 = 200;
		
	//修改窗口标题
	cs.style &= ~FWS_ADDTOTITLE;
	//FWS_ADDTOTITLE是一种MFC类型,用来指示框架将文档的标题到窗口的标题,去掉此类型
	cs.lpszName = "My window";
	
	return TRUE;
}

======================================================================================================================

demo2 (创建前指定窗口的图标、光标和背景)
//首先编写自己的WNDCLASS窗口类。
//调用RegisterClass()注册窗口类。
//修改cs.lpszClass为窗口类的名字。
//在框架窗口中只能修改图标
//光标和背景在View类中改变

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
       if( !CFrameWnd::PreCreateWindow(cs) )
                return FALSE;

        //设计一个新的窗口类
	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 = "SYAU";//为cs.lpszClass赋值 
	wndcls.lpszMenuName = NULL;//菜单的名字,在构造单文档模板时指定
	wndcls.style = CS_HREDRAW | CS_VREDRAW;//窗口类的类型

        //注册新的窗口类
	RegisterClass(&wndcls);
	
	//在cs中修改窗口类的类名
	cs.lpszClass = "SYAU";

	return TRUE;
}

BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//修改窗口的光标和背景
	cs.lpszClass = "SYAU";//SYAU窗口类已经注册,可以在任何地方使用

	return CView::PreCreateWindow(cs);
}

======================================================================================================================

AfxRegisterWndClass  
LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 ); 
/*Return Value
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.
*/

demo3 (创建前指定窗口的图标、光标和背景)
//AfxRegisterWndClass简化构造新窗口类的步骤
//效果相同

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, 0, 0, LoadIcon(NULL,IDI_WARNING));
	
	return TRUE;
}



BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
	//修改窗口的光标和背景
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, 
		LoadCursor(NULL,IDC_CROSS), (HBRUSH)GetStockObject(BLACK_BRUSH),0);

	return CView::PreCreateWindow(cs);
}





2.窗口创建之后,修改窗口的外观

//The SetWindowLong function changes an attribute of the specified window. 
//设定指定窗口的属性
LONG SetWindowLong(
  HWND hWnd,       // handle to window
  int nIndex,      // offset of value to set
  LONG dwNewLong   // new value
);



//The GetWindowLong function retrieves information about the specified window. 
//获取指定窗口的属性
LONG GetWindowLong(
  HWND hWnd,  // handle to window
  int nIndex  // offset of value to retrieve
);


//在OnCreate()中去调用SetWindowLong来修改已经创建好的窗口。
//SetWindowLong(m_hWnd,GWL_STYLE,WS_OVERLAPPEDWINDOW);//取消FWS_ADDTOTITLE属性


demo4 (取消已经创建的窗口最大化框功能)
//在OnCreate()中添加如下代码

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
        //...
        //部分代码省略
        //...

	SetWindowLong(m_hWnd,GWL_STYLE,GetWindowLong(m_hWnd,GWL_STYLE) & ~WS_MAXIMIZEBOX);

	return 0;
}


======================================================================================================================


//Platform SDK: Windows User Interface
SetClassLong
DWORD SetClassLong(
  HWND hWnd,       // handle to window
  int nIndex,      // index of value to change
  LONG dwNewLong   // new value
);


demo5 (创建后指定窗口的图标、光标和背景)


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
        //...
        //部分代码省略
        //...

	SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI_ERROR));//改变图标

	return 0;
}


//在View类中响应WM_CREATE消息
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_IBEAM));//改变光标
	return 0;
}


你可能感兴趣的:(null,Class,mfc,Parameters,initialization,styles)