发信人: mendy.bbs@bbs.nju.edu.cn (孟迪), 信区: cnprogram
标  题: VC FAQ(11)
发信站: nju_bbs (Sun Apr 19 13:51:39 1998)
转信站: Lilac!ustcnews!nju_bbs

2m发信人:m TMN (派生类)
2m信  区:m RAD
2m标  题:m VC FAQ(11)
2m发信站:m '3m紫金飞鸿m' (Wed Apr  8 08:17:19 1998) , 5m站内信件m

[返回首页] [分类讨论区] [全部讨论区]

发信人: nathan (珊儿), 信区: Programming 

教你各种改变背景的方法! 
*******************************************************************************

*******************************************************************************

To change the background color for a CView, CFrameWnd, or CWnd object, 
process the WM_ERASEBKGND message. The sample code below demonstrates 
how. 

Sample Code 
----------- 

BOOL CSampleView::OnEraseBkgnd(CDC* pDC) 

// Set brush to desired background color 
CBrush backBrush(RGB(255, 128, 128)); 

// Save old brush 
CBrush* pOldBrush = pDC->SelectObject(&backBrush); 

CRect rect; 
pDC->GetClipBox(&rect); // Erase the area needed 

pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), 
PATCOPY); 
pDC->SelectObject(pOldBrush); 
return TRUE; 


To change the background color for a CMDIFrameWnd you must subclass the 
multiple document interface (MDI) client window (window in the client area 
of CMDIFrameWnd) and process the WM_ERASEBKGND message. For more 
information about the MDI client window in an MDI application, see chapter 
18 in "Programming Windows 3.1 - Third Edition" by Charles Petzold. For an 
example that shows how to subclass the MDICLIENT window, please see the 
article in the Microsoft Knowledge Base: 

ARTICLE-ID: Q129471 
TITLE: How to subclass the MDICLIENT by Using MFC 

To change the background color of an MDI client window (client area of a 
CMDIFrameWnd), perform the following steps using an AppWizard-generated 
application: 

1. Create a generic CWnd class with ClassWizard. 

2. Add a member variable, using the type of the CWnd class from step 
1, to the CMainFrame class. 

3. In the CMainFrame's OnCreate member function, after the call to the 
base class CMDIFrameWnd::OnCreate(), add a call to SubclassWindow(). 
For example: 

if (!m_wndNewClient.SubclassWindow(m_hWndMDIClient)) 

TRACE("Failed to subclass MDI client window\n"); 
return -1; // fail to create 


m_hWndMDIClient is the member variable of CMDIFrameWnd that 
contains the handle to the MDI client window. Also, replace 
"m_wndNewClient" with the data member that you created in step 2. 

4. Whenever a window is subclassed, the GetSuperWndProcAddr() member 
function for the CWnd needs to be overridden to provide storage for 
the old window procedure's address. To do that, add the following 
function to the implementation of the CWnd class created in step 1: 

WNDPROC* CNewClientWnd::GetSuperWndProcAddr() 

static WNDPROC NEAR pfnSuper = NULL; 
return &pfnSuper; 


NOTE: Replace "CNewClientWnd" above with the name of your class. 

For more information on subclassing windows using the Microsoft 
Foundation Classes, please see the following materials: 

- "Class Library Reference" for functions CWnd::SubclassWindow(), 
CWnd::GetSuperWndProcAddr(), and CWnd::SubclassDlgItem() 

- Query on the following words in the Microsoft Knowledge Base: 

subclass and sample and mfc 

- See the CTRLTEST MFC sample application that is provided with 
Visual C++ for Windows and Visual C++ 32-bit Edition. 

- See the article titled "Subclassing Windows with the Microsoft 
Foundation Class Library" on the Microsoft Developer Network 
(MSDN) CD. 

5. Override the WM_ERASEBKGND message for the generic CWnd class, 
using the code listed above. 

To change the background color of a CFormView object, either process 
the WM_ERASEBKGND message and use the code above or process the 
WM_CTLCOLOR message to change the background color. 

For more information on changing the background color of a dialog box 
by processing the WM_CTLCOLOR message, please query on the following 
words in the Microsoft Knowledge Base: 

changing and background and color and MFC 

*******************************************************************************

*******************************************************************************


This article gives you two ways to make the CMiniFrameWnd miniframe window 
paint its background area. 

A miniframe window (CMiniFrameWnd) doesn't paint its background area even 
though Windows sends a WM_ERASEBKGND message to prepare the invalidated 
portion of the window for painting. The default window procedure for this 
message erases the background by using the class background brush specified

by the hbrBackground member of the WNDCLASS structure. As this background 
brush is NULL for a CMiniFrameWnd and MFC doesn't provide a handler for 
WM_ERASEBKGND to erase the background of a CMiniFrameWnd, the client area 
of a miniframe window is never erased. 

MORE INFORMATION 
================ 

A CMiniFrameWnd is a half-height/half-caption frame window typically 
enclosing another child window that takes up the entire client area of the 
window. The default window class for a CMiniFrameWnd is pre-registered by 
MFC without a background brush; that is, the hbrBackground member of the 
WNDCLASS structure is NULL. 

The application sends a WM_ERASEBKGND message to prepare the invalidated 
portion of a window for painting. The window procedure of MFC (AfxWndProc) 
receives this message first and in turn calls the default window procedure 
(DefWindowProc), which erases the background by using the class background 
brush. Because there is no background brush for a CMiniFrameWnd, the 
background area is never painted. 

This default behavior is by design because there is no need for the 
miniframe window to repaint its background, as the child window typically 
enclosing the miniframe window will paint over its background area. 

Following are two ways to make the CMiniFrameWnd paint its background area.


Method One 
---------- 

This method registers a window class for CMiniFrameWnd with a background 
brush. To implement this, derive a class from CMiniFrameWnd and override 
the Create member function. The Create function would be similar to the 
default implementation of CMiniFrameWnd::Create (in WINMINI.CPP of 
~/mfc/src directory), but with the addition of a background brush. The 
following code demonstrates this: 

// Header file of CMiniFrameWnd derived class (mymini.h) 
class CMyMiniFrameWnd : public CMiniFrameWnd 

DECLARE_DYNCREATE(CMyMiniFrameWnd) 
public: 
CMyMiniFrameWnd(); 

// Add the declaration for the Create function 
BOOL Create(LPCTSTR lpClassName, LPCTSTR lpszWindowName, 
DWORD dwStyle, const RECT& rect, 
CWnd *pParentWnd = NULL, UINT nID = 0); 

// Rest of the class definition 
... 
}; 

// Implementation file of CMiniFrameWnd derived class (mymini.cpp) 
BOOL CMyMiniFrameWnd::Create( 
LPCTSTR lpszClassName, LPCTSTR lpszWindowName, 
DWORD dwStyle, const RECT& rect, 
CWnd* pParentWnd, UINT nID) 

// Window title 

                         [返回首页] [分类讨论区] [全部讨论区]

--
m;31m※ 来源:·紫金飞鸿 bbs.njupt.edu.cn·[FROM: pc05.info.njupt]m

--
※ 来源:.南大小百合信息交换站 bbs.nju.edu.cn.[FROM: a507yjh.nju.edu]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.765毫秒