一、打開CD-ROM?
mciSendString("Set?cdAudio?door?open?wait",NULL,0,NULL);?
二、關(guān)閉CD_ROM?
mciSendString("Set?cdAudio?door?closed?wait",NULL,0,NULL);?
三、關(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)注銷用戶、?
//關(guān)機(jī)、關(guān)閉電源等操作?
//?退出前的一些處理程序?
}?
四、重啟計(jì)算機(jī)?
typedef?int?(CALLBACK?*SHUTDOWNDLG)(int);?//顯示關(guān)機(jī)對(duì)話框函數(shù)的指針?
HINSTANCE?hInst?=?LoadLibrary("shell32.dll");?//裝入shell32.dll?
SHUTDOWNDLG?ShutDownDialog;?//指向shell32.dll庫中顯示關(guān)機(jī)對(duì)話框函數(shù)的指針?
if(hInst?!=?NULL)?
{?
//獲得函數(shù)的地址并調(diào)用之?
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);?
//枚舉函數(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è)列表控件變量?
六、一次只運(yùn)行一個(gè)程序?qū)嵗绻堰\(yùn)行則退出?
if(?FindWindow(NULL,"程序標(biāo)題"))?exit(0);?
七、得到當(dāng)前鼠標(biāo)所在位置?
CPoint?pt;?
GetCursorPos(&pt);?//得到位置?
八、上下文菜單事件觸發(fā)事件:OnContextMenu事件?
九、顯示和隱藏程序菜單?
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();?
}?
十、獲取可執(zhí)行文件的圖標(biāo)?
HICON?hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0);?
if?(hIcon?&&hIcon!=(HICON)-1)?
{?
pDC->DrawIcon(10,10,hIcon);?
}?
DestroyIcon(hIcon);?
十一、窗口自動(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),?&rWorkAre?
a,?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->W?
idth())?
{//調(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;?
十二、給系統(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è)新的?
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);?
…?
}?
十三、運(yùn)行其它程序?
//1、運(yùn)行EMAIL或網(wǎng)址?
char?szMailAddress[80];?
strcpy(szMailAddress,"mailto:netvc@21cn.com");?
ShellExecute(NULL,?"open",?szMailAddress,?NULL,?NULL,?SW_SHOWNORMAL);?
//2、運(yùn)行可執(zhí)行程序?
WinExec("notepad.exe",SW_SHOW);?//運(yùn)行計(jì)事本?
十四、動(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();?//重畫菜單?
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的解釋見上。?
if(str=="Always?on?&Top")?//如果是剛才我們?cè)黾拥牟藛雾?xiàng),則刪除。?
{?
(mainmenu->GetSubMenu?(0))->DeleteMenu(i,MF_BYPOSITION);?
break;?
}?
十五、改變應(yīng)用程序的圖標(biāo)?
靜態(tài)更改:?修改圖標(biāo)資源IDR_MAINFRAME。它有兩個(gè)圖標(biāo),一個(gè)是16*16的,另一個(gè)是3?
2*32的,注意要一起修改。?
動(dòng)態(tài)更改:?向主窗口發(fā)送WM_SETICON消息.代碼如下:?
HICON?hIcon=AfxGetApp()->LoadIcon(IDI_ICON);?
ASSERT(hIcon);?
AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);?
十六、另一種改變窗口標(biāo)題的方法?
使用語句?CWnd*?m_pCWnd?=?AfxGetMainWnd(?),然后,再以如下形式調(diào)用SetWindowTe?
xt()函數(shù):?
SetWindowText(?*m_pCWnd,(LPCTSTR)m_WindowText);//?m_WindowText可以是一個(gè)CSt?
ring類的變量。?
十七、剪切板上通過增強(qiáng)元文件拷貝圖像數(shù)據(jù)?
下面代碼拷貝通過元文件拷貝圖像數(shù)據(jù)到任何應(yīng)用程序,其可以放置在CView派生類的函?
數(shù)中。?
CMetaFileDC?*?m_pMetaDC?=?new?CMetaFileDC();?
m_pMetaDC->CreateEnhanced(GetDC(),NULL,NULL,"whatever");?
//draw?meta?file?
//do?what?ever?you?want?to?do:?bitmaps,?lines,?text...?
//close?meta?file?dc?and?prepare?for?clipboard;?
HENHMETAFILE?hMF?=?m_pMetaDC->CloseEnhanced();?
//copy?to?clipboard?
OpenClipboard();?
EmptyClipboard();?
::SetClipboardData(CF_ENHMETAFILE,hMF);?????????????????????????????????????????????????????????????????????????????????
CloseClipboard();?
//DeleteMetaFile(hMF);?
delete?m_pMetaDC;?
十八、剪切板上文本數(shù)據(jù)的傳送?
把文本放置到剪接板上:?
CString?source;?
//put?your?text?in?source?
if(OpenClipboard())?
{?
HGLOBAL?clipbuffer;?
char?*?buffer;?
EmptyClipboard();?
clipbuffer?=?GlobalAlloc(GMEM_DDESHARE,?source.GetLength()+1);?
buffer?=?(char*)GlobalLock(clipbuffer);?
strcpy(buffer,?LPCSTR(source));?
GlobalUnlock(clipbuffer);?
SetClipboardData(CF_TEXT,clipbuffer);?
CloseClipboard();?
}?
從剪接板上獲取文本:?
char?*?buffer;?
if(OpenClipboard())?
{?
buffer?=?(char*)GetClipboardData(CF_TEXT);?
//do?something?with?buffer?here?
//before?it?goes?out?of?scope?
}?
CloseClipboard();?
十九、將捕捉屏幕圖像到剪切版中?
void?CShowBmpInDlgDlg::OnCutScreen()?
{?
ShowWindow(SW_HIDE);?
RECT?r_bmp={0,0,::GetSystemMetrics(SM_CXSCREEN),?
::GetSystemMetrics(SM_CYSCREEN)};???????????????????????????????????????????????????????????????????????????????????????
HBITMAP?hBitmap;?
hBitmap=CopyScreenToBitmap(&r_bmp);?
//hWnd為程序窗口句柄?
if?(OpenClipboard())?
{?
EmptyClipboard();?
SetClipboardData(CF_BITMAP,?hBitmap);?
CloseClipboard();?
}?
ShowWindow(SW_SHOW);?
}?
HBITMAP?CShowBmpInDlgDlg::CopyScreenToBitmap(LPRECT?lpRect)?
{?
//lpRect?代表選定區(qū)域?
{?
HDC?hScrDC,?hMemDC;?
//?屏幕和內(nèi)存設(shè)備描述表?
HBITMAP?hBitmap,?hOldBitmap;?
//?位圖句柄?
int?nX,?nY,?nX2,?nY2;?
//?選定區(qū)域坐標(biāo)?
int?nWidth,?nHeight;?
//?位圖寬度和高度?
int?xScrn,?yScrn;?
//?屏幕分辨率?
//?確保選定區(qū)域不為空矩形?
if?(IsRectEmpty(lpRect))?
return?NULL;?
//為屏幕創(chuàng)建設(shè)備描述表?
hScrDC?=?CreateDC("DISPLAY",?NULL,?NULL,?NULL);?
//為屏幕設(shè)備描述表創(chuàng)建兼容的內(nèi)存設(shè)備描述表?
hMemDC?=?CreateCompatibleDC(hScrDC);?
//?獲得選定區(qū)域坐標(biāo)?
nX?=?lpRect->left;?
nY?=?lpRect->top;?
nX2?=?lpRect->right;?
nY2?=?lpRect->bottom;?
//?獲得屏幕分辨率?
xScrn?=?GetDeviceCaps(hScrDC,?HORZRES);?
yScrn?=?GetDeviceCaps(hScrDC,?VERTRES);?
//確保選定區(qū)域是可見的?
if?(nX<0)?
nX?=?0;?
if?(nY<0)?
nY?=?0;?
if?(nX2>xScrn)?
nX2?=?xScrn;?
if?(nY2>yScrn)?
nY2?=?yScrn;?
nWidth?=?nX2?-?nX;?
nHeight?=?nY2?-?nY;?
//?創(chuàng)建一個(gè)與屏幕設(shè)備描述表兼容的位圖?
hBitmap?=?CreateCompatibleBitmap?
(hScrDC,?nWidth,?nHeight);?
//?把新位圖選到內(nèi)存設(shè)備描述表中?
hOldBitmap?=(HBITMAP)SelectObject(hMemDC,?hBitmap);?
//?把屏幕設(shè)備描述表拷貝到內(nèi)存設(shè)備描述表中?
BitBlt(hMemDC,?0,?0,?nWidth,?nHeight,?
hScrDC,?nX,?nY,?SRCCOPY);?
//得到屏幕位圖的句柄?
hBitmap?=?(HBITMAP)SelectObject(hMemDC,?hOldBitmap);?
//清除?
DeleteDC(hScrDC);?
DeleteDC(hMemDC);?
//?返回位圖句柄?
return?hBitmap;?
}?
}?
二十、如何將位圖縮放顯示在Static控件中?
//在Staic控件內(nèi)顯示位圖?
void?CShowBmpInDlgDlg::ShowBmpInStaic()?
{?
CBitmap?hbmp;?
HBITMAP?hbitmap;?
//將pStatic指向要顯示的地方?
CStatic?*pStaic;?
pStaic=(CStatic*)GetDlgItem(IDC_IMAGE);?
//裝載資源?MM.bmp是我的一個(gè)文件名,用你的替換?
hbitmap=(HBITMAP)::LoadImage?(::AfxGetInstanceHandle(),"MM.bmp",?
IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);?
hbmp.Attach(hbitmap);?
//獲取圖片格式?
BITMAP?bm;?
hbmp.GetBitmap(&bm);?
CDC?dcMem;?
dcMem.CreateCompatibleDC(GetDC());?
CBitmap?*poldBitmap=(CBitmap*)dcMem.SelectObject(hbmp);?
CRect?lRect;?
pStaic->GetClientRect(&lRect);?
//顯示位圖?
pStaic->GetDC()->StretchBlt(lRect.left?,lRect.top?,lRect.Width(),lRect.Heigh?
t(),?
&dcMem,0?,0,bm.bmWidth,bm.bmHeight,SRCCOPY);?
dcMem.SelectObject(&poldBitmap);?
}