1.绘制点
COLORREF SetPixel( POINT point, COLORREF crColor );
2.绘制矩形:
bool rectangle(LPCRECT lpRect);
crect类重载了LPCRECT 操作符,其作用是将CRect转换为LPCRECT类型
operator LPCRECT( ) const;
//
Converts a CRect to an LPCRECT. When you use this function, you don't need the address-of (&) operator. This operator will be automatically used when you pass a CRect object to a function that expects an LPCRECT.
Example
void CYourView::OnInitialUpdate()
{
// CWnd::GetWindowRect() takes a LPRECT, but we can
// simply pass our CRect object because of the LPRECT
// cast operator in the CRect class.
CRect rect;
GetWindowRect(rect);
// Similarly, CWnd::MoveWindow() takes a LPCRECT but
// the LPCRECT cast operator is used implicitly:
MoveWindow(rect, FALSE);
// ... more code here ...
}
当我传递的参数数值类型和所需要的类型不匹配,但编译和运行都正确的时候:可能是这些类型之间本来就可以互相转换,例如short
和int。但是如果参数类型是对象类型的话,就要考虑:它选择的是对象的构造方法进行的隐式转换还是有其他重载的操作。]
3.GetStockObject
The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.
HGDIOBJ GetStockObject( int fnObject // stock object type );
Value | Meaning |
---|---|
BLACK_BRUSH | Black brush. |
DKGRAY_BRUSH | Dark gray brush. |
DC_BRUSH | Windows 2000/XP: Solid color brush. The default color is white. The color can be changed by using the SetDCBrushColor function. For more information, see the Remarks section. |
GRAY_BRUSH | Gray brush. |
HOLLOW_BRUSH | Hollow brush (equivalent to NULL_BRUSH). |
LTGRAY_BRUSH | Light gray brush. |
NULL_BRUSH | Null brush (equivalent to HOLLOW_BRUSH). |
WHITE_BRUSH | White brush. |
BLACK_PEN | Black pen. |
DC_PEN | Windows 2000/XP: Solid pen color. The default color is white. The color can be changed by using the SetDCPenColor function. For more information, see the Remarks section. |
WHITE_PEN | White pen. |
ANSI_FIXED_FONT | Windows fixed-pitch (monospace) system font. |
ANSI_VAR_FONT | Windows variable-pitch (proportional space) system font. |
DEVICE_DEFAULT_FONT | Windows NT/2000/XP: Device-dependent font. |
DEFAULT_GUI_FONT | Default font for user interface objects such as menus and dialog boxes. This is MS Sans Serif. Compare this with SYSTEM_FONT. |
OEM_FIXED_FONT | Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font. |
SYSTEM_FONT | System font. By default, the system uses the system font to draw menus, dialog box controls, and text. Windows 95/98 and Windows NT: The system font is MS Sans Serif. Windows 2000/XP: The system font is Tahoma |
SYSTEM_FIXED_FONT | Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier than 3.0. |
DEFAULT_PALETTE | Default palette. This palette consists of the static colors in the system palette. |
If the function succeeds, the return value is a handle to the requested logical object.
If the function fails, the return value is NULL.
4.CBrush::FromHandle
static CBrush* PASCAL FromHandle( HBRUSH hBrush );
Remarks
Returns a pointer to a CBrush object when given a handle to a Windows HBRUSH object. If a CBrush object is not already attached to the handle, a temporary CBrush object is created and attached. This temporary CBrush object is valid only until the next time the application has idle time in its event loop. At this time, all temporary graphic objects are deleted. In other words, the temporary object is valid only during the processing of one window message.
5.颜色对话框
CColorDialog( COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL );
保存用户所选择的颜色:CHOOSECOLOR结构体类型的成员变量:m_cc.
typedef struct { DWORD lStructSize; HWND hwndOwner; HWND hInstance; COLORREF rgbResult; COLORREF * lpCustColors; DWORD Flags; LPARAM lCustData; LPCCHOOKPROC lpfnHook; LPCTSTR lpTemplateName; } CHOOSECOLOR, *LPCHOOSECOLOR;
注意书中出现的调试错误。
6.字体对话框
CFontDialog( LPLOGFONT lplfInitial = NULL, DWORD
dwFlags = CF_EFFECTS | CF_SCREENFONTS, CDC* pdcPrinter = NULL,
CWnd* pParentWnd = NULL );
同样也有一个CHOOSEFONT的结构体类型的数据成员:m_cf来保存用户选择
typedef struct { DWORD lStructSize; HWND hwndOwner; HDC hDC; LPLOGFONT lpLogFont; INT iPointSize; DWORD Flags; COLORREF rgbColors; LPARAM lCustData; LPCFHOOKPROC lpfnHook; LPCTSTR lpTemplateName; HINSTANCE hInstance; LPTSTR lpszStyle; WORD nFontType; WORD ___MISSING_ALIGNMENT__; INT nSizeMin; INT nSizeMax; } CHOOSEFONT, *LPCHOOSEFONT;
cfont类的CreateFontIndirect成员函数来根据指定特征的逻辑字体来初始化这个字体对象。
BOOL CreateFontIndirect(const LOGFONT* lpLogFont );
利用参数lplogfont指向的LOGFont结构中的一些特征来初始化对象
7.窗口无效
void Invalidate( BOOL bErase = TRUE );
书中程序出现的错误。
释放资源:CGdiObject类的DeleteObject函数来实现]
判断对象是否与某个资源相关联:利用CGdiObjectde 的数据成员m_ hObject来判断
(A HANDLE containing the HBITMAP, HRGN, HBRUSH, HPEN, HPALETTE, or HFONT attached to this object.),该变量保存了与CGdiObject
对象相关连的 Windows GDI 资源的句柄。DeleteObject可以释放这个资源。
8.selectobject
CPen* SelectObject( CPen* pPen );
CBrush* SelectObject( CBrush* pBrush );
virtual CFont* SelectObject( CFont* pFont );
CBitmap* SelectObject( CBitmap* pBitmap );
int SelectObject( CRgn* pRgn );
Return Value
A pointer to the object being replaced. This is a pointer to an object of one of the classes derived from CGdiObject, such as CPen, depending on which version of the function is used. The return value is NULL if there is an error. This function may return a pointer to a temporary object. This temporary object is only valid during the processing of one Windows message. For more information, see CGdiObject::FromHandle.