VC程序設(shè)計小技巧20例
本文匯總了VC程序設(shè)計中常用的20則技巧實例,供大家參考。詳情如下:
1、打開CD-ROM
mciSendString("Set cdAudio door open wait",NULL,0,NULL);
2、關(guān)閉CD_ROM
mciSendString("Set cdAudio door closed wait",NULL,0,NULL);
3、關(guān)閉計算機
OSVERSIONINFO OsVersionInfo; //包含操作系統(tǒng)版本信息的數(shù)據(jù)結(jié)構(gòu) OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&OsVersionInfo); //獲取操作系統(tǒng)版本信息 if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { //Windows98,調(diào)用ExitWindowsEx()函數(shù)重新啟動計算機 DWORD dwReserved; ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改變第一個參數(shù),實現(xiàn)注銷用戶、 //關(guān)機、關(guān)閉電源等操作 // 退出前的一些處理程序 }
4、重啟計算機
typedef int (CALLBACK *SHUTDOWNDLG)(int); //顯示關(guān)機對話框函數(shù)的指針 HINSTANCE hInst = LoadLibrary("shell32.dll"); //裝入shell32.dll SHUTDOWNDLG ShutDownDialog; //指向shell32.dll庫中顯示關(guān)機對話框函數(shù)的指針 if(hInst != NULL) { //獲得函數(shù)的地址并調(diào)用之 ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60); (*ShutDownDialog)(0); }
5、枚舉所有字體
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); //枚舉函數(shù) int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam) { // Create a pointer to the dialog window CDay7Dlg* pWnd = (CDay7Dlg*) lparam; // add the font name to the list box pWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName); // Return 1 to continue font enumeration return 1; }
其中m_ctlFontList是一個列表控件變量
6、一次只運行一個程序?qū)嵗?,如果已運行則退出
if( FindWindow(NULL,"程序標題")) exit(0);
7、得到當前鼠標所在位置
CPoint pt; GetCursorPos(&pt); //得到位置
8、上下文菜單事件觸發(fā)事件 :OnContextMenu事件
9、顯示和隱藏程序菜單
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(); }
10、獲取可執(zhí)行文件的圖標
HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0); if (hIcon &&hIcon!=(HICON)-1) { pDC->DrawIcon(10,10,hIcon); } DestroyIcon(hIcon);
11、窗口自動靠邊程序演示
BOOL AdjustPos(CRect* lpRect) { //自動靠邊 int iSX=GetSystemMetrics(SM_CXFULLSCREEN); int iSY=GetSystemMetrics(SM_CYFULLSCREEN); RECT rWorkArea; BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0); CRect rcWA; if(!bResult) { //如果調(diào)用不成功就利用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) { //調(diào)整左 //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) { //調(diào)整上 //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()) { //調(diào)整右 //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()) { //調(diào)整下 //pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE); lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom); return TRUE; } return FALSE; } //然后在ONMOVEING事件中使用所下過程調(diào)用 CRect r=*pRect; AdjustPos(&r); *pRect=(RECT)r;
12、給系統(tǒng)菜單添加一個菜單項
給系統(tǒng)菜單添加一個菜單項需要進行下述三個步驟:
首先,使用Resource Symbols對話(在View菜單中選擇Resource Symbols...可以顯示該對話)定義菜單項ID,該ID應(yīng)大于0x0F而小于0xF000;
其次,調(diào)用CWnd::GetSystemMenu獲取系統(tǒng)菜單的指針并調(diào)用CWnd:: Appendmenu將菜單項添加到菜單中。下例給系統(tǒng)菜單添加兩個新的菜單項。
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或網(wǎng)址 char szMailAddress[80]; strcpy(szMailAddress,"mailto:netvc@163.com"); ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL); //2、運行可執(zhí)行程序 WinExec("notepad.exe",SW_SHOW); //運行計事本
14、動態(tài)增加或刪除菜單
(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--) //取得菜單的項數(shù)。 { (mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION); //將指定菜單項的標簽拷貝到指定的緩沖區(qū)。MF_BYPOSITION的解釋見上。 if(str=="Always on &Top") //如果是剛才我們增加的菜單項,則刪除。 { (mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION); break; } }
15、測試ALT鍵是否按下:
GetKeyState(VK_MENU); GetAlt();
16、檢查是否按下鼠標左鍵
if((nFlags&MK_LBUTTON)==MK_LBUTTON)
17、檢查鍵盤輸入
在OnKeyDown中的參數(shù)nChar是一個數(shù)值,當顯示的時候,需要轉(zhuǎn)換成字符,使用如下的命令:
char lsChar; lsChar=char(nChar); if(lsChar=='A'); { ....... }
18、調(diào)用另一個函數(shù)::GetKeyState(),用一個特定的鍵代碼來確定法鍵是否被按下。 如果::GetKeyState函數(shù)的返回值是負的,表 示該鍵被按下。如果返回值是非負的,表示該留未被按下。例如:如果要確定shift鍵是否被按下,可以使用下面的代碼:
if(::GetKeyState(VK_SHIFT) <O) { AfxMessageBox("shift is pressed"); }
19.如何在編程的過程中隨時結(jié)束應(yīng)用程序(常規(guī))
1)需要向窗口發(fā)送 WM_CLOSE/WM_QUIT消息,
調(diào)用 CWnd::OnClose成員函數(shù)并允許對用戶提示是否保存修改過的數(shù)據(jù).
AfxGetMainWnd()->SendMessage(WM_CLOSE); //別忘了先得到當前窗口的指針
2)使用函數(shù):
void PostQuitMessage( int nExitCode // exit code );
3)使用標準函數(shù):
void exit( int status ); //盡量不要在MFC中使用
20.得到屏幕的尺寸大小
HWND hWnd; CRect Rect; hWnd = ::GetDesktopWindow(); ::GetClientRect(hWnd, &Rect); //---------------------------------------------------------
如何查詢和設(shè)置系統(tǒng)參數(shù)
在Windows 3.1 SDK中介紹過SDK函數(shù)SystemParametersInfo,調(diào)用該函數(shù)可以查詢和設(shè)置系統(tǒng)參數(shù),諸如按鍵的重復(fù)速率設(shè)置、鼠標雙擊延遲時間、圖標字體以及桌面覆蓋位圖等等。
//Create a font that is used for icon titles. LOGFONT stFont; :: SystemParametersInfo (SPIF_GETICONTITLELOGFONT, sizeof (LOGFONT), &stFont, SPIF_SENDWININICHANGE); m_font.CreateFontIndirect (&stFont); //Change the wallpaper to leaves.bmp. :: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, _T("forest.bmp"),SPIF_UPDATEINIFILE);
如何使用一個預(yù)定義的Windows光標
調(diào)用CWinApp:: LoadStandardCursor并傳送光標標識符。
BOOL CSampleDialog:: OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message) { //Display wait cursor if busy. if (m_bBusy) { SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT)); return TRUE; } return CDialog:: OnSetCursor (pWnd. nHitTest,message); }
相關(guān)文章
C語言中四種取整方式,取余/取模運算以及負數(shù)取模問題詳解
這篇文章主要介紹了C語言中四種取整方式及負數(shù)取模問題,包括了算法的分析與改進,是很多程序設(shè)計競賽中常見的算法,需要的朋友可以參考下2021-09-09c++使用單例模式實現(xiàn)命名空間函數(shù)案例詳解
這篇文章主要介紹了c++使用單例模式實現(xiàn)命名空間函數(shù),本案例實現(xiàn)一個test命名空間,此命名空間內(nèi)有兩個函數(shù),分別為getName()和getNameSpace(),本文結(jié)合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2023-04-04C語言實現(xiàn)選擇排序、冒泡排序和快速排序的代碼示例
這篇文章主要介紹了C++中實現(xiàn)選擇排序、冒泡排序和快速排序的代碼示例,例子帶有執(zhí)行時間統(tǒng)計還可以簡單看一下效率對比,需要的朋友可以參考下2016-04-04