欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VC程序設(shè)計(jì)小技巧20例

 更新時(shí)間:2014年07月18日 11:13:46   投稿:shichen2014  
這篇文章主要介紹了VC程序設(shè)計(jì)小技巧20例,需要的朋友可以參考下

本文匯總了VC程序設(shè)計(jì)中常用的20則技巧實(shí)例,供大家參考。詳情如下:

1、打開(kāi)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)閉計(jì)算機(jī)

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ù)重新啟動(dòng)計(jì)算機(jī) 
  DWORD dwReserved; 
  ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改變第一個(gè)參數(shù),實(shí)現(xiàn)注銷(xiāo)用戶、 
  //關(guān)機(jī)、關(guān)閉電源等操作 
  // 退出前的一些處理程序 
} 

4、重啟計(jì)算機(jī)

typedef int (CALLBACK *SHUTDOWNDLG)(int); //顯示關(guān)機(jī)對(duì)話框函數(shù)的指針 
HINSTANCE hInst = LoadLibrary("shell32.dll"); //裝入shell32.dll 
SHUTDOWNDLG ShutDownDialog; //指向shell32.dll庫(kù)中顯示關(guān)機(jī)對(duì)話框函數(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是一個(gè)列表控件變量

6、一次只運(yùn)行一個(gè)程序?qū)嵗?,如果已運(yùn)行則退出

if( FindWindow(NULL,"程序標(biāo)題")) 
exit(0); 

7、得到當(dāng)前鼠標(biāo)所在位置

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); ////顯示菜單 也可改變菜單項(xiàng) 
  pWnd->SetMenu(&menu); 
  pWnd->DrawMenuBar(); 
  b_m=true; 
  menu.Detach(); 
} 

10、獲取可執(zhí)行文件的圖標(biāo)

HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0); 
if (hIcon &&hIcon!=(HICON)-1) 
{ 
  pDC->DrawIcon(10,10,hIcon); 
} 
DestroyIcon(hIcon); 

11、窗口自動(dòng)靠邊程序演示

BOOL AdjustPos(CRect* lpRect) 
{ 
  //自動(dòng)靠邊 
  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事件中使用所下過(guò)程調(diào)用 
CRect r=*pRect; 
AdjustPos(&r); 
*pRect=(RECT)r; 

12、給系統(tǒng)菜單添加一個(gè)菜單項(xiàng)
給系統(tǒng)菜單添加一個(gè)菜單項(xiàng)需要進(jìn)行下述三個(gè)步驟:
首先,使用Resource Symbols對(duì)話(在View菜單中選擇Resource Symbols...可以顯示該對(duì)話)定義菜單項(xiàng)ID,該ID應(yīng)大于0x0F而小于0xF000;
其次,調(diào)用CWnd::GetSystemMenu獲取系統(tǒng)菜單的指針并調(diào)用CWnd:: Appendmenu將菜單項(xiàng)添加到菜單中。下例給系統(tǒng)菜單添加兩個(gè)新的菜單項(xià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、運(yùn)行其它程序

//運(yùn)行EMAIL或網(wǎng)址 
char szMailAddress[80]; 
strcpy(szMailAddress,"mailto:netvc@163.com"); 
ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL); 
//2、運(yùn)行可執(zhí)行程序 
WinExec("notepad.exe",SW_SHOW); //運(yùn)行計(jì)事本 

14、動(dòng)態(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")); //添加新的菜單項(xiàng) 
DrawMenuBar(); //重畫(huà)菜單 

(2)、 刪除菜單

//刪除 
CMenu *mainmenu; 
mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜單 
CString str ; 
for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i--) //取得菜單的項(xiàng)數(shù)。 
{ 
  (mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION); 
  //將指定菜單項(xiàng)的標(biāo)簽拷貝到指定的緩沖區(qū)。MF_BYPOSITION的解釋見(jiàn)上。 
  if(str=="Always on &Top") //如果是剛才我們?cè)黾拥牟藛雾?xiàng),則刪除。 
  { 
(mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION); 
 break; 
  } 
} 

15、測(cè)試ALT鍵是否按下:

GetKeyState(VK_MENU); 
GetAlt(); 

16、檢查是否按下鼠標(biāo)左鍵 

if((nFlags&MK_LBUTTON)==MK_LBUTTON) 


17、檢查鍵盤(pán)輸入
在OnKeyDown中的參數(shù)nChar是一個(gè)數(shù)值,當(dāng)顯示的時(shí)候,需要轉(zhuǎn)換成字符,使用如下的命令:

char lsChar; 
lsChar=char(nChar); 
if(lsChar=='A'); 
{ 
....... 
} 

18、調(diào)用另一個(gè)函數(shù)::GetKeyState(),用一個(gè)特定的鍵代碼來(lái)確定法鍵是否被按下。 如果::GetKeyState函數(shù)的返回值是負(fù)的,表 示該鍵被按下。如果返回值是非負(fù)的,表示該留未被按下。例如:如果要確定shift鍵是否被按下,可以使用下面的代碼:

if(::GetKeyState(VK_SHIFT) <O) 
{ 
  AfxMessageBox("shift is pressed"); 
} 

19.如何在編程的過(guò)程中隨時(shí)結(jié)束應(yīng)用程序(常規(guī))
1)需要向窗口發(fā)送 WM_CLOSE/WM_QUIT消息,
調(diào)用 CWnd::OnClose成員函數(shù)并允許對(duì)用戶提示是否保存修改過(guò)的數(shù)據(jù).

AfxGetMainWnd()->SendMessage(WM_CLOSE); //別忘了先得到當(dāng)前窗口的指針 

2)使用函數(shù):

void PostQuitMessage( int nExitCode // exit code ); 

3)使用標(biāo)準(zhǔn)函數(shù):

void exit( int status ); //盡量不要在MFC中使用 

20.得到屏幕的尺寸大小

HWND hWnd; 
CRect Rect; 
hWnd = ::GetDesktopWindow(); 
::GetClientRect(hWnd, &Rect); 

//--------------------------------------------------------- 

如何查詢(xún)和設(shè)置系統(tǒng)參數(shù)
    在Windows 3.1 SDK中介紹過(guò)SDK函數(shù)SystemParametersInfo,調(diào)用該函數(shù)可以查詢(xún)和設(shè)置系統(tǒng)參數(shù),諸如按鍵的重復(fù)速率設(shè)置、鼠標(biāo)雙擊延遲時(shí)間、圖標(biāo)字體以及桌面覆蓋位圖等等。 

//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); 

如何使用一個(gè)預(yù)定義的Windows光標(biāo)
 調(diào)用CWinApp:: LoadStandardCursor并傳送光標(biāo)標(biāo)識(shí)符。

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)文章

最新評(píng)論