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; }
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; }
// 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; }