Visual C++编程技巧之六

来源:互联网 发布:刘青云郭蔼明 知乎 编辑:程序博客网 时间:2024/05/16 14:04
Visual C++编程技巧之六  

 

41、如何一个创建三态下压按钮

42、如何动态创建控件

43、如何限制编辑框中的准许字符

44、如何改变控件的颜色

45、当向列表框中添加多个项时如何防止闪烁

46、如何向编辑控件中添加文本

47、如何访问预定义的GDI对象

48、如何获取GDI对象的属性信息

  

41、如何一个创建三态下压按钮

可以使用新的BS_PUSHBUTTON 风格位和检测框以及按钮来创建一个三态下压按钮。这很容易,只需将检测框和按钮拖拉到对话中并指定属性Pushlike即可。不用任何附加程序就可以成为三态下压按钮。

42、如何动态创建控件

分配一个控件对象的实例并调用其Create成员函数。开发者最容易忽略两件事:忘记指定WS_VISBLE标签和在栈中分配控件对象。下例动态地创建一个下压按钮控件:

//In class declaration (.H file ).

private :

CButton* m _pButton ;

//In class implementation (.cpp file ) .

m_pButton =new CButton ;

ASSERT_VALID (m_pButton);

m_pButton >Create (_T ("Button Title ") , WS_CHILD WS_VISIBLE BS_PUSHBUTTON.

Crect ( 0, 0, 100 , 24) , this , IDC _MYBUTTON )

43、如何限制编辑框中的准许字符

如果用户在编辑控件中只允许接收数字,可以使用一个标准的编辑控件并指定新的创建标志ES_NUMBERS,它是Windows 95新增加的标志,该标志限制 编辑控件只按收数字字符。如果用户需要复杂的编辑控件,可以使用Microsoft 的屏蔽编辑控件,它是一个很有用的OLE定制控件。

如果希望不使用OLE 定制控件自己处理字符,可以派生一个CEdit 类并处理WM_CHAR消息,然后从编辑控件中过滤出特定的字符。首先,使用ClassWizard 建立一个 CEdit的派生类,其次,在对话类中指定一个成员变量将编辑控件分类在OnInitdialog 中调用CWnd: : SubclassDlgItem .

//In your dialog class declaration (.H file )

private :

CMyEdit m_wndEdit ; // Instance of your new edit control .

//In you dialog class implementation (.CPP file )

BOOL CSampleDialog : : OnInitDialog ( )

{

//Subclass the edit lontrod .

m_wndEdit .SubclassDlgItem (IDC_EDIT,this );

}

使用ClassWizard处理WM_CHAR消息,计算nChar参量并决定所执行的操作,用户可以确定是否修改、传送字符。下例说明了如何显示字母字符,如果字符是字母字符,则调用CWnd ; OnChar,否则不调用OnChar.

//Only display alphabetic dharacters .

void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UITN nFlags )

{

//Determine if nChar is an alphabetic character .

if (: : IsCharAlpha ( ( TCHAR) nChar ) )

CEdit : : OnChar (nChar, nRepCnt , nFlags );

}

如果要修改字符,则不能仅仅简单地用修改过的nChar调用CEdit : : OnChar,然后CEdit: : OnChar调用CWnd: : Default获取原来的wParam lParam 的值 ,这样是不行的。要修改一个字符,需要首先修改nChar,然后用修改过的nChar调用CWnd: : DefWindowProc。下例说明了如何将字符转变为大写:

//Make all characters uppercase

void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UINT nFlags )

{

//Make sure character is uppercase .

if (: : IsCharAlpha ( .( TCHAR) nChar)

nChar=: : CharUpper (nChar ) ;

//Bypass default OnChar processing and directly call

//default window proc.

DefWindProc (WM_CHAR, nChar , MAKELPARAM (nRepCnt , nFlags )) ;

}

44、如何改变控件的颜色

有两种方法。其一,可以在父类中指定控件的颜色,或者利用MFC4.0新的消息反射在控件类中指定颜色。当控件需要重新着色时,工作框调用父窗口(通常是对话框)的CWnd: : OnCrtlColor,可以在父窗口类中重置该函数并指定控件的新的绘画属性。例如,下述代码将对话中的所有编辑控件文本颜色改为红色:

HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)

{

HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor );

//Draw red text for all edit controls .

if (nCtlColor= = CTLCOLOR_EDIT )

pDC > SetTextColor (RGB (255 , 0 , 0 , ) ) ;

return hbr ;

}

然而,由于每个父窗口必须处理通知消息并指定每个控件的绘画属性,所以,这种方法不是完全的面向对象的方法。控件处理该消息并指定绘画属性更合情合理。消息反射允许用户这样做。通知消息首先发送给父窗口,如果父窗口没有处理则发送给控件。创建一个定制彩色列表框控件必须遵循下述步骤。

首先,使用ClassWizard 创建一个CListBox 的派生类并为该类添加下述数据成员。

class CMyListBox ; publilc CListBox

{

private;

COLORREF m_clrFor ; // foreground color

COLORREF m_clrBack ; //background color

Cbrush m_brush ; //background brush

} ;

其次,在类的构造函数中,初始化数据中。

CMyListBox : : CMyListBox ()

{

//Initialize data members .

m_clrFore =RGB (255 , 255 , 0) ; // yellow text

m_clrBack=RGB (0 , 0 , 255) ; // blue background

m_brush . CreateSolidBrush (m _clrBack );

}

最后,使用ClassWizard处理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的绘画属性。

HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )

{

pDC>SetTextColor (m_clrFore);

pDC>SetBkColor (m_clrBack);

return (HBRUSH) m_brush.GetSafeHandle ()

}

现在,控件可以自己决定如何绘画,与父窗口无关。

45、当向列表框中添加多个项时如何防止闪烁

调用CWnd::SetRedraw 清除重画标志可以禁止CListBox(或者窗口)重画。当向列表框添加几个项时,用户可以清除重画标志,然后添加项,最后恢复重画标志。为确保重画列表框的新项,调用SetRedraw (TRUE) 之后调用CWnd::Invalidate

//Disable redrawing.

pListBox->SetRedraw (FALSE);

//Fill in the list box gere

//Enable drwing and make sure list box is redrawn.

pListBox->SetRedraw (TRUE);

pListBox->Invalidate ();

46、如何向编辑控件中添加文本

由于没有CEdit:: AppendText函数,用户只好自己做此项工作。调用CEdit:: SetSel移动到编辑控件末尾,然后调用CEdit:: ReplaceSel添加文本。下例是AppendText 的一种实现方法:

void CMyEdit:: AppendText (LPCSTR pText)

{

int nLen=GetWindowTextLength ();

SetFocus ();

SetSel (nLen, nLen);

ReplaceSel (pText);

}

47、如何访问预定义的GDI对象

可以通过调用CDC:: SlectStockObject使用Windows的几个预定义的对象,诸如刷子、笔以及字体。下例使用了Windows预定义的笔和刷子GDI对象在视窗中画一个椭圆。

//Draw ellipse using stock black pen and gray brush.

void CSampleView:: OnDraw (CDC* pDC)

{

//Determine size of view.

CRect rcView;

GetClientRect (rcView);

//Use stock black pen and stock gray brush to draw ellipse.

pDC->SelectStockObject (BLACK_PEN);

pDC->SelectStockObject (GRAY_BRUSH)

//Draw the ellipse.

pDC->Ellipse (reView);

}

也可以调用新的SDK函数GetSysColorBrush获取一个系统颜色刷子,下例用背景色在视窗中画一个椭圆:

void CsampleView:: OnDraw (CDC* pDC)

{

//Determine size of view.

CRect rcView;

GetClientRect (rcView);

//Use background color for tooltips brush.

CBrush * pOrgBrush=pDC->SelectObject (

CBrush::FromHandle (::GetSysColorBrush (COLOR_INFOBK)));

//Draw the ellipse.

pDC->Ellipse (rcView);

//Restore original brush.

pDC->SelectObject (pOrgBrush);

}

48、如何获取GDI对象的属性信息

可以调用GDIObject:: GetObject。这个函数将指定图表设备的消息写入到缓冲区。下例创建了几个有用的辅助函数。

//Determine if font is bold.

BOOL IsFontBold (const CFont&font)

{

LOGFONT stFont;

font.GetObject (sizeof (LOGFONT), &stFont);

return (stFont.lfBold)? TRUE: FALSE;

}

//Return the size of a bitmap.

CSize GetBitmapSize (const CBitmap&bitmap)

{

BITMAP stBitmap;

bitmap.GetObject (sizeof (BITMAP), &stBitmap);

return CSize (stBitmap.bmWidth, stBitmap. bmHeight);

}

//Create a pen with the same color as a brush.

BOOL CreatePenFromBrush (Cpen&pen, cost Cbrush&brush)

{

LOGBRUSH stBrush;

brush.Getobject (sizeof (LOGBRUSH), &stBrush);

return pen. Createpen (PS_SOLID, 0, stBrush.ibColor);

}


原创粉丝点击