(1) 如何通过代码获得应用程序主窗口的 指针?主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现。AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)//使程序最大化. (2) 确定应用程序的路径Use GetModuleFileName 获得应用程序的路径,然后去掉可执行文件名。Example:TCHARexeFullPath[MAX_PATH] // MAX_PATH在API中定义了吧,好象是128GetModuleFileName(NULL,exeFullPath,MAX_PATH) (3) 如何在程序中获得其他程序的 图标?两种方法:(1) SDK函数 SHGetFileInfo 或使用 ExtractIcon获得图标资源的 handle,(2) SDK函数 SHGetFileInfo 获得有关文件的很多信息,如大小图标,属性, 类型等.Example(1):在程序窗口左上角显示 NotePad图标.void CSampleView:OnDraw(CDC * pDC){if( :: SHGetFileInfo(_T("c:\\pwin95\\notepad.exe"),0,&stFileInfo,sizeof(stFileInfo),SHGFI_ICON)){pDC ->DrawIcon(10,10,stFileInfo.hIcon)}}Example(2):同样功能,Use ExtractIcon Functionvoid CSampleView:: OnDraw(CDC *pDC){HICON hIcon=:: ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0)if (hIcon &&hIcon!=(HICON)-1)pDC->DrawIcon(10,10,hIcon)} 说明: 获得notepad.exe的路径正规上来说用GetWindowsDirectory函数得到, 如果是调用 win95下的画笔,应该用访问注册表的方法获得其路径,要作成一个比较考究的程序,考虑应该全面点. (4) 获得各种目录信息Windows目录: Use "GetWindowsDirectory"Windows下的system目录: Use "GetSystemDirectory"temp目录: Use "GetTempPath"当前目录: Use "GetCurrentDirectory" 请注意前两个函数的第一个参数为目录变量名,后一个为缓冲区后两个相反. (5) 如何自定义消息1) 手工定义消息,可以这么写#define WM_MY_MESSAGE(WM_USER+100),MS 推荐的至少是 WM_USER+100 (2)写消息处理函数,用WPARAM,LPARAM返回LRESULT.LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam) {temp目录: Use "GetTempPath"//加入你的处理函数 irectory"} (6) 如何改变窗口的图标?向窗口发送 WM_SECTION消息。Example:HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)ASSERT(hIcon)AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM)hIcon) (7) 如何改变窗口的缺省风格?重载 CWnd:: PreCreateWindow 并修改CREATESTRUCT结构来指定窗口风格和其他创建信息.Example: Delete "Max" Button and Set OriginalWindow's Position and Size BOOL CMainFrame:: PreCreateWindow(CREATESTRUCT &cs){cs.style &=~WS_MAXINIZEMOX cs.x=cs.y=0cs.cx=GetSystemMetrics(SM_CXSCREEN/2)cs.cy=GetSystemMetrics(SM_CYSCREEN/2) return CMDIFramewnd ::PreCreateWindow(cs)} (8) 如何将窗口居中显示?Call Function CWnd::Center Windows Example(1):Center Window( ) //Relative to it's parent// Relativeto ScreenExample(2):Center Window(CWnd:: GetDesktopWindow( ))//Relative toApplication's MainWindowAfxGetMainWnd( ) ->Center Window( ) (9) 如何让窗口和 MDI窗口一启动就最大化和最小化?先说窗口。在 InitStance 函数中设定 m_nCmdShow的取值.m_nCmdShow=SW_SHOWMAXMIZED //最大化m_nCmdShow=SW_SHOWMINMIZED //最小化m_nCmdShow=SW_SHOWNORMAL //正常方式 MDI窗口:如果是创建新的应用程序,可以用MFC AppWizard 的Advanced 按钮并在MDI子窗口风格组中检测最大化或最小化还可以重载 MDI Window 的PreCreateWindow函数,设置WS_MAXMIZE or WS_MINMIZE 如果从 CMDIChildWnd派生,调用 OnInitialUpdate函数中的 CWnd::Show Window来指定 MDI Child Window的风格。 (10) 如何限制窗口的大小?也就是 FixedDialog形式。 Windows发送 WM_GETMAXMININFO消息来跟踪, 响应它,在 OnGetMAXMININFO 中写代码: (11) 如何使窗口不可见?很简单,用SW_HIDE 隐藏窗口,可以结合 FindWindow,ShowWindow控制. (12) 如何创建一个字回绕的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} (13) 如何使程序保持极小状态?这么办: 在恢复程序窗体大小时,Windows会发送WM_QUERY-OPEN消息,用 ClassWizard设置成员函数OnQueryOpen() ,add following code: Bool CMainFrame:: OnQueryOpen( ){Return false} (14) 移动窗口调用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) (15) 通用控件的显示窗口MFC提供了几个CView派生的视窗类, 封装了通用控件的功能,但仍然使用工作框文档显示窗口体系结构:CEditView封装了编辑控件,CTreeView保持了树列表控件,CListView封装了列表显示窗口控件,CRichEditView可以处理多种编辑控件。 (16) 重置窗口的大小调用CWnd: : SetWindowPos并指定SWP_NOMOVE标志, 也可调用CWnd : : MoveWindow 但必须指定窗口的位置。// Get the size of the window .Crect reWindowGetWindowRect (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 ) (17) 如何单击除了窗口标题栏以外的区域使窗口移动当窗口需要确定鼠标位置时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 clickedonits 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 itscaption bar .PostMessage (WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARM (point.x, point. y) )} (18) 如何改变视窗的背景颜色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 reClippDC—>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} (19) 如何改变窗口标题调用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 (reducesflash in thecontrols )if (nNewLen >_contof (szOld)|| : : GetWindowText (hWndCrtl, szOld , _countof (szOld) !=nNewLen|| Istrcmp (szOld , IpszNew)! = 0{//change it: : SetWindowText(hWndCtrl , IpszNew )}} (20) 如何防止主框窗口在其说明中显示活动的文档名创建主框窗口和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来设置标题。记住自己设置标题时要遵循接口风格指南。 (21) 如何获取有关窗口正在处理的当前消息的信息调用CWnd: : GetCurrentMessage可以获取一个MSG指针。例如,可以使用ClassWizard将几个菜单项处理程序映射到一个函数中,然后调用GetCurrentMessage来确定所选中的菜单项。viod CMainFrame : : OnCommmonMenuHandler ( ){//Display selected menu item in debug window .TRACE ("Menu item %u was selected . \n" , (22) 如何在代码中获取工具条和状态条的指针缺省时, 工作框创建状态条和工具条时将它们作为主框窗口的子窗口,状态条有一个AFX_IDW_STATUS_BAR标识符,工具条有一个AFX_IDW_TOOLBAR标识符,下例说明了如何通过一起调用CWnd: : GetDescendantWindow和AfxGetMainWnd来获取这些子窗口的指针://Get pointer to status bar .CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )—> GetDescendantWindow(AFX_IDW_STUTUS_BAR) //Get pointer to toolbar .CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( )—> GetDescendantWindow(AFX_IDW_TOOLBAR) (23) 如何使能和禁止工具条的工具提示如果设置了CBRS_TOOLTIPS风格位,工具条将显示工具提示,要使能或者禁止工具提示,需要设置或者清除该风格位。下例通过调用CControlBar : : GetBarStyle和CControlBar : : SetBarStyle建立一个完成此功能的成员函数:void CMainFrame : : EnableToolTips ( BOOL bDisplayTips ){ASSERT_VALID (m_wndToolBar) DWORD dwStyle = m _wndToolBar.GetBarStyle ( ) if (bDisplayTips) dwStyle |=CBRS_TOOLTIPS elsedwStyle & = ~CBRS_TOOLTIPS m_wndToolBar.SetBarStyle (dwStyle )} (24) 如何创建一个不规则形状的窗口可以使用新的SDK函数SetWindowRgn。该函数将绘画和鼠标消息限定在窗口的一个指定的区域,实际上使窗口成为指定的不规则形状。 使用AppWizard创建一个基于对的应用程序并使用资源编辑器从主对话资源中删除所在的缺省控件、标题以及边界。给对话类增加一个CRgn数据成员,以后要使用该数据成员建立窗口区域。Class CRoundDlg : public CDialog{…private :Crgn m_rgn : // window region…}修改OnInitDialog函数建立一个椭圆区域并调用SetWindowRgn将该区域分配给窗口:BOOL CRoundDlg : : OnInitDialog ( ){CDialog : : OnInitDialog ( ) //Get size of dialog .CRect rcDialogGetClientRect (rcDialog ) // Create region and assign to window .m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width( ) , rcDialog.Height ( ) )SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn ,TRUE ) return TRUE} 通过建立区域和调用SetWindowRgn,已经建立一个不规则形状的窗口,下面的例子程序是修改OnPaint函数使窗口形状看起来象一个球形体。voik CRoundDlg : : OnPaint ( ){CPaintDC de (this) // device context for painting.//draw ellipse with out any borderdc. SelecStockObject (NULL_PEN)//get the RGB colour components of the sphere colorCOLORREF color= RGB( 0 , 0 , 255)BYTE byRed =GetRvalue (color)BYTE byGreen = GetGvalue (color)BYTE byBlue = GetBvalue (color) // get the size of the view windowCrect rectGetClientRect (rect) // get minimun number of unitsint nUnits =min (rect.right , rect.bottom ) //calculate he horiaontal and vertical step sizefloat fltStepHorz = (float) rect.right /nUnitsfloat fltStepVert = (float) rect.bottom /nUnits int nEllipse = nUnits/3 // calculate how many todrawint nIndex// current ellipse that is being draw CBrush brush// bursh used for ellipse fill colorCBrush *pBrushOld // previousbrush that was selected into dc//draw ellipse , gradually moving towards upper-rightcornerfor (nIndex = 0 nIndes < + nEllipse nIndes++){//creat solid brushbrush . CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ).( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue)/nEllipse ) ) ) //select brush into dcpBrushOld= dc .SelectObject (&brhsh) //draw ellipsedc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex ,rect. right -( (int) fltStepHorz * nIndex )+ 1,rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1) //delete the brushbrush.DelecteObject ( )}} 最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。UINT CRoundDlg : : OnNchitTest (Cpoint point ){//Let user move window by clickign anywhere on thewindow .UINT nHitTest = CDialog : : OnNcHitTest (point)rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest } (25) 如何获取应用程序的 实例句柄?应用程序的实例句柄保存在CWinApp m_hInstance 中,可以这么调用AfxGetInstancdHandle获得句柄.Example: HANDLE hInstance=AfxGetInstanceHandle()

评论