孙鑫《vc ++深入详解》第十章绘图控制

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
);
Parameters
fnObject
[in] Specifies the type of stock object. This parameter can be one of the following values.
ValueMeaning
BLACK_BRUSHBlack brush.
DKGRAY_BRUSHDark gray brush.
DC_BRUSHWindows 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_BRUSHGray brush.
HOLLOW_BRUSHHollow brush (equivalent to NULL_BRUSH).
LTGRAY_BRUSHLight gray brush.
NULL_BRUSHNull brush (equivalent to HOLLOW_BRUSH).
WHITE_BRUSHWhite brush.
BLACK_PENBlack pen.
DC_PENWindows 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_PENWhite pen.
ANSI_FIXED_FONTWindows fixed-pitch (monospace) system font.
ANSI_VAR_FONTWindows variable-pitch (proportional space) system font.
DEVICE_DEFAULT_FONTWindows NT/2000/XP: Device-dependent font.
DEFAULT_GUI_FONTDefault font for user interface objects such as menus and dialog boxes. This is MS Sans Serif. Compare this with SYSTEM_FONT.
OEM_FIXED_FONTOriginal equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.
SYSTEM_FONTSystem 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_FONTFixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier than 3.0.
DEFAULT_PALETTEDefault palette. This palette consists of the static colors in the system palette.

Return Values

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.

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、在视频Lesson2中,在介绍构造函数时,我说:“构造函数最重要的作用是创建对象本身,对象内存的分配由构造函数来完成的”,这句话是错的,对象内存的分配和构造函数没有关系,对象内存的分配是由编译器来完成的,构造函数的作用是对对象本身做初始化工作,也就是给用户提供初始化类中成员变量的一种方式,在类对象有虚表的情况下,构造函数还对虚表进行初始化。 另外,我说:“C++又规定,如果一个类没有提供任何的构造函数,则C++提供一个默认的构造函数(由C++编译器提供)”,这句话也是错误的,正确的是: 如果一个类中没有定义任何的构造函数,那么编译器只有在以下三种情况,才会提供默认的构造函数: 1、如果类有虚拟成员函数或者虚拟继承父类(即有虚拟基类)时; 2、如果类的基类有构造函数(可以是用户定义的构造函数,或编译器提供的默认构造函数); 3、在类中的所有非静态的对象数据成员,它们对应的类中有构造函数(可以是用户定义的构造函数,或编译器提供的默认构造函数)。 二、在视频 Lesson4 的Code 中,画扇形用如下代码即可: if(m_bDraw == TRUE) { dc.MoveTo(m_ptOrigin); dc.LineTo(point); } 带边线的扇形用如下代码即可: if(m_bDraw == TRUE) { dc.MoveTo(m_ptOrigin); dc.LineTo(point); dc.LineTo(m_ptOld); m_ptOld = point; } 三、在视频Lesson8中,关于在对话框上放置组合框的问题,我说“如果拖动的矩形较小,组合框的列表框部分将无法显示,此时也无法调整组合框的上下位置的大小了”。实际上,组合框的上下位置还是可以调整的,调整的办法如下: 在对话框资源处于编辑状态时,将鼠标移动到组合框控件右边向下的箭头上,当鼠标变成上下箭头形状时,单击鼠标左键,此时可以看到举行框围绕着组合框。将鼠标移动到该矩形框下端的蓝色小方块上,当鼠标变成上下箭头形状时,按住鼠标左键向下拖动,直到把组合框的下拉列表框范围拖动到合适的大小时松开鼠标左键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值