1. 程式人生 > >vc++常用函式方法

vc++常用函式方法

一、開啟CD-ROM 
mciSendString("Set cdAudio door open wait",NULL,0,NULL); 
二、關閉CD_ROM 
mciSendString("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 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是一個列表控制元件變數 
六、一次只執行一個程式例項,如果已執行則退出 
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(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->W 

idth()) 
{//調整右 
//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); 
… 

十三、執行其它程式 
//1、執行EMAIL或網址 
char szMailAddress[80]; 
strcpy(szMailAddress,"mailto:
[email protected]
"); 
ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL); 

//2、執行可執行程式 
WinExec("notepad.exe",SW_SHOW); //執行計事本 
十四、動態增加或刪除選單 
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--) //取得菜 
單的項數。 

(mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION); 
//將指定選單項的標籤拷貝到指定的緩衝區。MF_BYPOSITION的解釋見上。 
if(str=="Always on &Top") //如果是剛才我們增加的選單項,則刪除。 

(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION); 
break; 

十五、改變應用程式的圖示 
靜態更改: 修改圖示資源IDR_MAINFRAME。它有兩個圖示,一個是16*16的,另一個是3 

2*32的,注意要一起修改。 
動態更改: 向主視窗傳送WM_SETICON訊息.程式碼如下: 
HICON hIcon=AfxGetApp()->LoadIcon(IDI_ICON); 
ASSERT(hIcon); 
AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);