|   
 | 
| 17、如何创建一个字回绕的ceditview 
 重载cwnd : : precreatewindow和修改createstruct结构,关闭ceditview对象的es_autohscroll和ws_hscroll风格位, 由于ceditview : : precreatewindow显示设置cs. style,调用基类函数后要修改cs . style。
 
 bool csampleeditview : : precreatewindow (createstruct&cs)
 
 {
 
 //first call basse class function .
 
 bool bresutl =ceditview : : precreatewindow (cs) ;
 
 // now specify the new window style .
 
 cs.style &= ~ (es_autohscroll |ws_hscroll);
 
 return bresult ;
 
 }
 
 18、通用控件的显示窗口
 
 mfc提供了几个cview派生的视窗类, 封装了通用控件的功能,但仍然使用工
 
 作框文档显示窗口体系结构:ceditview封装了编辑控件,ctreeview保持了树列表
 
 控件,clistview封装了列表显示窗口控件,cricheditview可以处理多种编辑控件。
 
 19、移动窗口
 
 调用cwnd : : setwindowpos并指定swp_nosize标志。目的位置与父窗口
 
 有关(顶层窗口与屏幕有关)。调用cwnd : : movewindow时必须要指定窗口
 
 的大小。
 
 //move window to positoin 100 , 100 of its parent window .
 
 setwindowpos (null, 100 , 100 , 0 , 0 , swp_nosize |swp_noaorder);
 
 20、重置窗口的大小
 
 调用cwnd: : setwindowpos并指定swp_nomove标志, 也可调用
 
 cwnd : : movewindow 但必须指定窗口的位置。
 
 // get the size of the window .
 
 crect rewindow ;
 
 getwindowrect (rewindow );
 
 //make the window twice as wide and twice as tall .
 
 setwindowpos (null , 0 , 0 , rewindow . width ( ) *2,
 
 rewindow . height () * 2,
 
 swp_nomove |swp_nozorder );
 
 21、如何单击除了窗口标题栏以外的区域使窗口移动
 
 当窗口需要确定鼠标位置时windows向窗口发送wm_nchittest信息,可以处理
 
 该信息使windows认为鼠标在窗口标题上。对于对话框和基于对话的应用程序,可
 
 以使用classwizard处理该信息并调用基类函数, 如果函数返回htclient 则表明
 
 鼠标在客房区域,返回htcaption表明鼠标在windows的标题栏中。
 
 uint csampledialog : : onnchittest (cpoint point )
 
 {
 
 uint nhittest =cdialog: : onnchittest (point );
 
 return (nhittest = =htclient)? htcaption : nhittest ;
 
 }
 
 上述技术有两点不利之处, 其一是在窗口的客户区域双击时, 窗口将极大;
 
 其二, 它不适合包含几个视窗的主框窗口。还有一种方法,当用户按下鼠标左键
 
 使主框窗口认为鼠标在其窗口标题上,使用classwizard在视窗中处理wm_lbuttodown
 
 信息并向主框窗口发送一个wm_nclbuttondown信息和一个单击测试htcaption。
 
 void csampleview : : onlbuttondown (uint nflags , cpoint point )
 
 {
 
 cview : : onlbuttondow (nflags , pont );
 
 //fool frame window into thinking somene clicked on
 
 its caption bar .
 
 getparentframe ( ) —> postmessage (
 
 wm_nclbuttondown , htcaption , makelparam (poitn .x , point .y) );
 
 }
 
 该技术也适用于对话框和基于对的应用程序,只是不必调用cwnd : : getparentframe 。
 
 void csampledialog : : onlbuttondown (uint nflags, cpoint point )
 
 {
 
 cdialog : : onlbuttondow (nflags, goint );
 
 //fool dialog into thinking simeone clicked on its caption bar .
 
 postmessage (wm_nclbuttondown , htcaption , makelparm (point.x , point. y ) )
 
 }
 
 22、如何改变视窗的背景颜色
 
 windows向窗口发送一个wm_erasebkgnd消息通知该窗口擦除背景,可以使用
 
 classwizard重载该消息的缺省处理程序来擦除背景(实际是画),并返回true以
 
 防止windows擦除窗口。
 
 //paint area that needs to be erased.
 
 bool csampleview : : onerasebkgnd (cdc* pdc)
 
 {
 
 // create a pruple brush.
 
 cbrush brush (rgb (128 , 0 , 128) );
 
 // select the brush into the device context .
 
 cbrush* poldbrush = pdc—>selcetobject (&brush);
 
 // get the area that needs to be erased .
 
 crect reclip ;
 
 pdc—>getcilpbox (&rcclip);
 
 //paint the area.
 
 pdc—> patblt (rcclip.left , rcclip.top ,
 
 rcclip.width ( ) , rcclip.height ( ) , patcopy );
 
 //unselect brush out of device context .
 
 pdc—>selectobject (poldbrush );
 
 // return nonzero to half fruther processing .
 
 return true;
 
 }
 
 23、如何改变窗口标题
 
 调用cwnd : : setwindowtext可以改变任何窗口(包括控件)的标题。
 
 //set title for application's main frame window .
 
 afxgetmainwnd ( ) —> setwindowtext (_t("application title") );
 
 //set title for view's mdi child frame window .
 
 getparentframe ( ) —> setwindowtext ("_t ("mdi child frame new title") );
 
 //set title for dialog's push button control.
 
 getdigitem (idc_button) —> setwindowtext (_t ("button new title ") );
 
 如果需要经常修改窗口的标题(注:控件也是窗口),应该考虑使用半文档化
 
 的函数afxsetwindowtext。该函数在afxpriv.h中说明,在winutil.cpp中实现,在
 
 联机帮助中找不到它,它在afxpriv.h中半文档化, 在以后发行的mfc中将文档化。
 
 afxsetwindowtext的实现如下:
 
 voik afxapi afxsetwindowtext (hwnd hwndctrl , lpctstr ipsznew )
 
 {
 
 itn nnewlen= istrlen (ipaznew);
 
 tchar szold [256];
 
 //fast check to see if text really changes (reduces flash in the controls )
 
 if (nnewlen >_contof (szold) ||
 
 : : getwindowtext (hwndcrtl , szold , _countof (szold) !=nnewlen ||
 
 istrcmp (szold , ipsznew )! = 0
 
 {
 
 //change it
 
 : : setwindowtext (hwndctrl , ipsznew );
 
 }
 
 }
 
 24、如何防止主框窗口在其说明中显示活动的文档名
 
 创建主框窗口和mdi子窗口进通常具有fws_addtotitle风格位, 如果不希望在
 
 说明中自动添加文档名, 必须禁止该风格位, 可以使用classwizard重置
 
 cwnd: : precreatewindow并关闭fws_addtotitle风格。
 
 bool cmainframe : : precreatewindow (createstruct&cs)
 
 {
 
 //turn off fws_addtotitle in main frame .
 
 cs.styel & = ~fws_addtotitle ;
 
 return cmdiframewnd : : precreatewindow (cs );
 
 }
 
 关闭mdi子窗口的fws _addtotitle风格将创建一个具有空标题的窗口,可以调
 
 用cwnd: : setwindowtext来设置标题。记住自己设置标题时要遵循接口风格指南。
 | 
 |