VC小技巧20个
- 格式:docx
- 大小:19.34 KB
- 文档页数:10
一、打开CD-ROMmciSendString("Set cdAudio door open wait",NULL,0,NULL);二、关闭CD_ROMmciSendString("Set cdAudio door closed wait",NULL,0,NULL);三、关闭计算机OSVERSIONINFO OsVersionInfo; //包含操作系统版本信息的数据结构OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&OsVersionInfo); //获取操作系统版本信息if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS){//Windows98,调用ExitWindowsEx()函数重新启动计算机DWORD dwReserved;ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改变第一个参数,实现注销用户、//关机、关闭电源等操作// 退出前的一些处理程序}四、重启计算机typedef int (CALLBACK *SHUTDOWNDLG)(int); //显示关机对话框函数的指针HINSTANCE hInst = LoadLibrary("shell32.dll"); //装入shell32.dll SHUTDOWNDLG ShutDownDialog; //指向shell32.dll库中显示关机对话框函数的指针if(hInst != NULL){//获得函数的地址并调用之ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);(*ShutDownDialog)(0);}五、枚举所有字体LOGFONT lf;lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structure strcpy(lf.lfFaceName,"");CClientDC dc (this);//Enumerate the font families::EnumFontFamiliesEx((HDC) dc,&lf,(FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0);//枚举函数int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam){// Create a pointer to the dialog windowCDay7Dlg* pWnd = (CDay7Dlg*) lparam;// add the font name to the list boxpWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName);// Return 1 to continue font enumerationreturn 1;}其中m_ctlFontList是一个列表控件变量六、一次只运行一个程序实例,如果已运行则退出if( FindWindow(NULL,"程序标题")) exit(0);七、得到当前鼠标所在位置CPoint pt;GetCursorPos(&pt); //得到位置八、上下文菜单事件触发事件:OnContextMenu事件九、显示和隐藏程序菜单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(REC T), &rWorkArea, 0);CRect rcWA;if(!bResult){//如果调用不成功就利用GetSystemMetrics获取屏幕面积rcWA=CRect(0,0,iSX,iSY);}elsercWA=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-lp Rect->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.bott om-lpRect->Height()){//调整下//pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_N OSIZE);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:: Appendme nu将菜单项添加到菜单中。
下例给系统菜单添加两个新的菜单项。
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);…}13、运行其它程序//运行EMAIL或网址char szMailAddress[80];strcpy(szMailAddress,"mailto:netvc@");ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMA L);//2、运行可执行程序WinExec("notepad.exe",SW_SHOW); //运行计事本14、动态增加或删除菜单(1)、增加菜单//添加CMenu *mainmenu;mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单(mainmenu->GetSubMenu (0))->AppendMenu (MF_SEPARATOR);//添加分隔符(mainmenu->GetSubMenu (0))->AppendMenu(MF_STRING,ID_APP_ABOUT,_ T("Always on &Top")); //添加新的菜单项DrawMenuBar(); //重画菜单(2)、删除菜单//删除CMenu *mainmenu;mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单CString str ;for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i--) //取得菜单的项数。