|   
 | 
| 33、如何获取一个对话控件的指针 
 有两种方法。其一,调用cwnd: : getdlgitem,获取一个cwnd*指针调用成员函数。下例调用getdlgitem,将返回值传给一个cspinbuttonctrl*以便调用cspinbuttonctrl : : setpos 函数:
 
 bool csampledialog : : oninitdialog ( )
 
 {
 
 cdialog : : oninitdialog ( ) ;
 
 //get pointer to spin button .
 
 cspinbuttonctrl * pspin - ( cspinbuttonctrl *) getdlgitem (idc_spin) ;
 
 assert _ valid (pspin) ;
 
 //set spin button's default position .
 
 pspin —> setpos (10) ;
 
 return true ;
 
 }
 
 其二, 可以使用classwizard将控件和成员变量联系起来。在classwizard中简单地选择member variables标签,然后选择add variable …按钮。如果在对话资源编辑器中,按下ctrl键并双击控件即可转到add member variable对话。
 
 34、如何禁止和使能控件
 
 控件也是窗口,所以可以调用cwnd : : enablewindow使能和禁止控件。
 
 //disable button controls .
 
 m_wndok.enablewindow (false ) ;
 
 m_wndapply.enablewindow (false ) ;
 
 35、如何改变控件的字体
 
 由于控件是也是窗口,用户可以调用cwnd: : setfont指定新字体。该函数用一个cfont指针,要保证在控件撤消之前不能撤消字体对象。下例将下压按钮的字体改为8点arial字体:
 
 //declare font object in class declaration (.h file ).
 
 private :
 
 cfont m_font ;
 
 // set font in class implementation (.cpp file ). note m_wndbutton is a
 
 //member variable added by classwizard.ddx routines hook the member
 
 //variable to a dialog button contrlo.
 
 bool csampledialog : : oninitdialog ( )
 
 {
 
 …
 
 //create an 8-point arial font
 
 m_font . createfont (muldiv (8 , -pdc—> getdevicecaps (logpixelsy) , 72).
 
 0 , 0 , 0 , fw_normal , 0 , 0, 0, ansi_charser, out_stroke_precis ,
 
 clip_stroke _precis , draft _quality
 
 variable_pitch |ff_swiss, _t ("arial") );
 
 //set font for push button .
 
 m_wndbutton . setfont (&m _font );
 
 …
 
 }
 
 36、如何在ole控件中使用ole_color数据类型
 
 诸如colecontrol : : getfortcolor和colecontrol : : getbackcolor等函数返回ole _color数据类型的颜色,而gdi对象诸如笔和刷子使用的是colorref数据类型,调用colecontrol : : translatecolor可以很容易地将ole_color类型改为colorref类型。下例创建了一个当前背景颜色的刷子:
 
 void csamplecontrol : : ondraw (cdc* pdc
 
 const crect& rcbounds , const crect& rcinvalid )
 
 {
 
 //create a brush of the cuttent background color .
 
 cbrush brushback (translatecolor (getbackcolor ( ) ) );
 
 //paint the background using the current background color .
 
 pdc—> filllrect (rcbounds , &brushback) ;
 
 //other drawign commands
 
 …
 
 }
 
 37、在不使用通用文件打开对话的情况下如何显示一个文件列表
 
 调用cwnd: : dlgdirlist或者cwnd: : dlgdirlistcombobox, windows 将自动地向列表框或组合框填充可用的驱动器名或者指定目录中的文件,下例将windows目录中的文件填充在组合框中:
 
 bool csampledig : : oninitdialog ( )
 
 {
 
 cdialog : : oninitdialog ( )
 
 tchar szpath [max_path] = {"c:\\windows"} ;
 
 int nreslt = dlgdirlistcombobox (szpath , idc_combo , idc_curidir,
 
 ddl_readwrite |ddl_readonly|ddl_hidden|
 
 ddl_system|ddl_archive ) ;
 
 return true ;
 
 }
 
 38、为什么旋转按钮控件看起来倒转
 
 需要调用cspinctrl : : setrange 设置旋转按钮控件的范围,旋转按钮控件的缺省上限为0,缺省下限为100,这意味着增加时旋转按控件的值由100变为0。下例将旋转按钮控件的范围设置为0到100:
 
 bool caboutdlg : : oninitdialog ( )
 
 {
 
 cdialog : : oninitdialog ( )
 
 //set the lower and upper limit of the spin button
 
 m_wndspin . setrange ( 0 ,100 ) ;
 
 return true ;
 
 }
 
 visual c++ 4.0 print对话中的copise旋转按钮控件也有同样的问题:按下up按钮时拷贝的数目减少,而按下down 按钮时拷贝的数目增加。
 
 39为什么旋转按钮控件不能自动地更新它下面的编辑控件
 
 如果使用旋转按钮的autu buddy特性, 则必须保证在对话的标记顺序中buddy窗口优先于旋转按钮控件。从layout菜单中选择tab order菜单项(或者按下crtl+d)可以设置对话的标签顺序。
 
 40、如何用位图显示下压按钮
 
 windows 95按钮有几处新的创建风格,尤其是bs_bitmap和bs_icon,要想具有位图按钮,创建按钮和调用cbutton : : setbitmap或cbutton : : seticon时要指定bs_bitmap或bs_icon风格。
 
 首先,设置按钮的图标属性。
 
 然后,当对话初始化时调用cbutton: : seticon。注意:下例用图标代替位图,使用位图时要小心,因为不知道背景所有的颜色——并非每个人都使用浅灰色。
 
 bool csampledlg : : oninitdialog ( )
 
 {
 
 cdialog : : oninitdialog ( ) ;
 
 //set the images for the push buttons .
 
 m_wndbutton1.seticon (afxgetapp ( ) —> loadicon (idi _ iption1) )
 
 m_wndbutton2.seticon (afxgetapp ( ) —> loadicon (idi _ iption2) )
 
 m_wndbutton3.seticon (afxgetapp ( ) —> loadicon (idi _ iption3) )
 
 return true ;
 
 }
 | 
 |