孙鑫VC++深入详解:Lesson9 Part2---修改窗口光标,图表,背景

一. 窗口创建之前修改,用自定义的窗口类WNDCLASS wc

由于窗口的图标,光标,背景是在窗口类设计是指定的,因此要改的话,
就自己定义一个WNDCLASS wc,按照自己的需要填充wc,并注册RegisterClass(&wc)

1. 在MFC程序中,要修改出现窗口图表只能在框架类进行(CMainFrame)

2. 要修改背景和光标,应该在视类中进行,在框架类修改则看不见效果,因为MFC程序有2个窗口,视类和框架类,视类覆盖了框架类

在框架类CMainFrame的虚函数PreCreateWindow中去修改,这样等于是在窗口创建之前就修改.

//在窗口创建之前修改,要修改什么,就对编写一个自己的窗口类wc,并注册
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

//------- 定义一个新的窗口类 wc
	
	WNDCLASS wc={0};
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.hCursor =LoadCursor(NULL,IDC_HELP); 
	wc.hIcon =LoadIcon(NULL,IDI_ERROR);
	wc.hInstance = AfxGetInstanceHandle();
	wc.lpfnWndProc = ::DefWindowProc;//全局的Win32API
	wc.lpszClassName ="sunxin.org"; //这个名称取定了,不能随便改了,这就好比一个资源的名称,
	wc.lpszMenuName =NULL;
	wc.style =CS_HREDRAW|CS_VREDRAW;

	RegisterClass(&wc); //注册
	cs.lpszClass = "sunxin.org"; //把注册的类传给cs
	return TRUE;
}

客户区背景色,光标等的修改还必须在CStyleView的虚函数PreCreateWindow中去进行:

BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

//客户区背景色,光标等的修改生效都必须在View类中加下面这条语句,使用前面已经注册的窗口类名

	cs.lpszClass = "sunxin.org";//这个"sunxin.org"就是前面定义的那个窗口类的类名

	return CView::PreCreateWindow(cs);
}


//---

二. 窗口创建之前修改,用全局函数AfxRegisterWndClass  修改

    LPCTSTR AFXAPI AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 ); 

     在框架类中:CMainFrame::PreCreateWindow(CREATESTRUCT& cs)     

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

//------使用全局函数AfxRegisterWndClass来修改
	  cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
								0, //不改光标,因为在框架类总改了也看不到
					        	0,//不改背景,因为在框架类中改了也看不见框架的背景
								LoadIcon(NULL,IDI_WARNING) // 能该的就是这个框架图标了
		  );


	return TRUE;
}

    在VIEW类中:BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)

  

BOOL CStyleView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	//------使用全局函数AfxRegisterWndClass来修改
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
								LoadCursor(NULL,IDC_CROSS), //改光标
								(HBRUSH)GetStockObject(BLACK_BRUSH),//改背景,如果用NULL就是透明的了
								0 // 图标不用改了,在框架类中改了.
								);
	


	return CView::PreCreateWindow(cs);
}

//---


三. 窗口创建之后修改,用SetClassLong(),在OnCreate()中修改/

      因为是在窗口创建之后,所以肯定是在OnCreate()中.

      在MainFrame中也只能修改ICON

      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);
	
//------用SetClassLong()修改标题栏图标
    SetClassLong(m_hWnd,GCL_HICON,(LONG)LoadIcon(NULL,IDI_ERROR));


	return 0;
}

在view中可以修改背景色,光标等等,但是view中没OnCreate函数,所以还必须先添加消WM_CREATE息响应函数  

// CStyleView message handlers

// 在CStyleView类中增加WM_CREATE消息响应函数
int CStyleView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

//------用SetClassLong()修改背景色和光标
    SetClassLong(m_hWnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(BLACK_BRUSH));//修改背景色
	SetClassLong(m_hWnd,GCL_HCURSOR,(LONG)LoadCursor(NULL,IDC_HELP));//修改背景色
	
	return 0;
}

//---


      

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值