九、显示和隐藏程序菜单CWnd *pWnd=AfxGetMainWnd(); if(b_m) //隐藏菜单 { pWnd->SetMenu(NULL); pWnd->DrawMenuBar(); b_m=false; } else { CMenu menu; menu.LoadMenu(IDR_MAINFRAME); ////显示菜单 也可改变菜单项 pWnd->SetMenu(&menu); pWnd->DrawMenuBar(); b_m=true; menu.Detach(); } 十、获取可执行文件的图标HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0); if (hIcon &&hIcon!=(HICON)-1) { pDC->DrawIcon(10,10,hIcon); } DestroyIcon(hIcon); 十一、窗口自动靠边程序演示BOOL AdjustPos(CRect* lpRect) {//自动靠边 int iSX=GetSystemMetrics(SM_CXFULLSCREEN); int iSY=GetSystemMetrics(SM_CYFULLSCREEN); RECT rWorkArea; BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkAre a, 0); CRect rcWA; if(!bResult) {//如果调用不成功就利用GetSystemMetrics获取屏幕面积 rcWA=CRect(0,0,iSX,iSY); } else rcWA=rWorkArea; int iX=lpRect->left; int iY=lpRect->top; if(iX < rcWA.left + DETASTEP && iX!=rcWA.left) {//调整左 //pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE); lpRect->OffsetRect(rcWA.left-iX,0); AdjustPos(lpRect); return TRUE; } if(iY < rcWA.top + DETASTEP && iY!=rcWA.top) {//调整上 //pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE); lpRect->OffsetRect(0,rcWA.top-iY); AdjustPos(lpRect); return TRUE; } if(iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width()) {//调整右 //pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE); lpRect->OffsetRect(rcWA.right-lpRect->right,0); AdjustPos(lpRect); return TRUE; } if(iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect ->Height()) {//调整下 //pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE); lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom); return TRUE; } return FALSE; } //然后在ONMOVEING事件中使用所下过程调用 CRect r=*pRect; AdjustPos(&r); *pRect=(RECT)r; 十二、给系统菜单添加一个菜单项 给系统菜单添加一个菜单项需要进行下述三个步骤: 首先,使用Resource Symbols对话(在View菜单中选择Resource Symbols...可以显示该对话)定义菜单项ID,该ID应大于0x0F而小于0xF000; 其次,调用CWnd::GetSystemMenu获取系统菜单的指针并调用CWnd:: Appendmenu将菜单项添加到菜单中。下例给系统菜单添加两个新的 int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct) { //… //Make sure system menu item is in the right range. ASSERT(IDM_MYSYSITEM<0xF000); //Get pointer to system menu. CMenu* pSysMenu=GetSystemMenu(FALSE); ASSERT_VALID(pSysMenu); //Add a separator and our menu item to system menu. CString StrMenuItem(_T ("New menu item")); pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_MYSYSITEM, StrMenuItem); //… }
|