一、打开CD-ROM
mciSendString("Set cdAudio door open wait",NULL,0,NULL);
二、关闭CD_ROM
mciSendString("Set cdAudio door closed wait",NULL,0,NULL);
三、关闭计算机

Code
1 OSVERSIONINFO OsVersionInfo; //包含操作系统版本信息的数据结构
2 OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
3 GetVersionEx(&OsVersionInfo); //获取操作系统版本信息
4 if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
5 {
6 //Windows98,调用ExitWindowsEx()函数重新启动计算机
7 DWORD dwReserved;
8 ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改变第一个参数,实现注销用户、
9 //关机、关闭电源等操作
10 // 退出前的一些处理程序
四、重启计算机

Code
1 typedef int (CALLBACK *SHUTDOWNDLG)(int); //显示关机对话框函数的指针
2 HINSTANCE hInst = LoadLibrary("shell32.dll"); //装入shell32.dll
3 SHUTDOWNDLG ShutDownDialog; //指向shell32.dll库中显示关机对话框函数的指针
4 if(hInst != NULL)
5 {
6 //获得函数的地址并调用之
7 ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);
8 (*ShutDownDialog)(0);
9 }
五、枚举所有字体

Code
1 LOGFONT lf;
2 lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structure
3 strcpy(lf.lfFaceName,"");
4 CClientDC dc (this);
5
6 //Enumerate the font families
7 ::EnumFontFamiliesEx((HDC) dc,&lf,
8 (FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0);
9
10 //枚举函数
11 int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam)
12 {
13 // Create a pointer to the dialog window
14 CDay7Dlg* pWnd = (CDay7Dlg*) lparam;
15 // add the font name to the list box
16
17 pWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName);
18
19 // Return 1 to continue font enumeration
20 return 1;
21 }
其中m_ctlFontList是一个列表控件变量
六、一次只运行一个程序实例,如果已运行则退出
if( FindWindow(NULL,"程序标题")) exit(0);
七、得到当前鼠标所在位置
CPoint pt;
GetCursorPos(&pt); //得到位置
八、上下文菜单事件触发事件:OnContextMenu事件
九、显示和隐藏程序菜单

Code
1 CWnd *pWnd=AfxGetMainWnd();
2 if(b_m) //隐藏菜单
3 {
4 pWnd->SetMenu(NULL);
5 pWnd->DrawMenuBar();
6 b_m=false;
7 }
8 else
9 {
10 CMenu menu;
11 menu.LoadMenu(IDR_MAINFRAME); ////显示菜单 也可改变菜单项
12 pWnd->SetMenu(&menu);
13 pWnd->DrawMenuBar();
14 b_m=true;
15 menu.Detach();
16 }
十、获取可执行文件的图标
1 HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0);
2 if (hIcon &&hIcon!=(HICON)-1)
3 {
4 pDC->DrawIcon(10,10,hIcon);
5 }
6 DestroyIcon(hIcon);
十一、窗口自动靠边程序演示

Code
1 BOOL AdjustPos(CRect* lpRect)
2 {
3 //自动靠边
4 int iSX=GetSystemMetrics(SM_CXFULLSCREEN);
5 int iSY=GetSystemMetrics(SM_CYFULLSCREEN);
6
7 RECT rWorkArea;
8 BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0);
9
10 CRect rcWA;
11 if(!bResult)
12 {
13 //如果调用不成功就利用GetSystemMetrics获取屏幕面积
14 rcWA=CRect(0,0,iSX,iSY);
15 }
16 else
17 rcWA=rWorkArea;
18
19 int iX=lpRect->left;
20 int iY=lpRect->top;
21 if(iX < rcWA.left + DETASTEP && iX!=rcWA.left)
22 {
23 //调整左
24 //pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
25 lpRect->OffsetRect(rcWA.left-iX,0);
26 AdjustPos(lpRect);
27 return TRUE;
28 }
29 if(iY < rcWA.top + DETASTEP && iY!=rcWA.top)
30 {
31 //调整上
32 //pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE);
33 lpRect->OffsetRect(0,rcWA.top-iY);
34 AdjustPos(lpRect);
35 return TRUE;
36 }
37 if(iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width())
38 {
39 //调整右
40 //pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
41 lpRect->OffsetRect(rcWA.right-lpRect->right,0);
42 AdjustPos(lpRect);
43 return TRUE;
44 }
45 if(iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect->Height())
46 {
47 //调整下
48 //pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
49 lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
50 return TRUE;
51 }
52 return FALSE;
53 }
54 //然后在ONMOVEING事件中使用所下过程调用
55 CRect r=*pRect;
56 AdjustPos(&r);
57 *pRect=(RECT)r;
十二、给系统菜单添加一个菜单项
给系统菜单添加一个菜单项需要进行下述三个步骤:
首先,使用Resource Symbols对话(在View菜单中选择Resource Symbols...可以显示该对话)定义菜单项ID,该ID应大于
0×0F而小于0xF000;
其次,调用CWnd::GetSystemMenu获取系统菜单的指针并调用CWnd:: Appendmenu将菜单项添加到菜单中。下例给系统菜单添加
两个新的菜单项:

Code
1 int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct)
2 {
3 …
4 //Make sure system menu item is in the right range.
5 ASSERT(IDM_MYSYSITEM<0xF000);
6 //Get pointer to system menu.
7 CMenu* pSysMenu=GetSystemMenu(FALSE);
8 ASSERT_VALID(pSysMenu);
9 //Add a separator and our menu item to system menu.
10 CString StrMenuItem(_T ("New menu item"));
11 pSysMenu->AppendMenu(MF_SEPARATOR);
12 pSysMenu->AppendMenu(MF_STRING, IDM_MYSYSITEM, StrMenuItem);
13 …
14 }
13、运行其它程序
1 //运行EMAIL或网址
2
3 char szMailAddress[80];
4
5 strcpy(szMailAddress,"mailto:netvc@21cn.com");
6
7 ShellExecute(NULL, "open", szMailAddress, NULL, NULL, SW_SHOWNORMAL);
//2、运行可执行程序
WinExec("notepad.exe",SW_SHOW); //运行计事本
14、动态增加或删除菜单
(1)、 增加菜单

Code
1 //添加
2 CMenu *mainmenu;
3 mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
4 (mainmenu->GetSubMenu (0))->AppendMenu (MF_SEPARATOR);//添加分隔符
5 (mainmenu->GetSubMenu (0))->AppendMenu(MF_STRING,ID_APP_ABOUT,_T("Always on &Top")); //添加新的菜单项
6 DrawMenuBar(); //重画菜单
(2)、 删除菜单

Code
1 //删除
2 CMenu *mainmenu;
3 mainmenu=AfxGetMainWnd()->GetMenu(); //得到主菜单
4 CString str ;
5 for(int i=(mainmenu->GetSubMenu (0))->GetMenuItemCount()-1;i>=0;i--) //取得菜单的项数。
6 {
7 (mainmenu->GetSubMenu (0))->GetMenuString(i,str,MF_BYPOSITION);
8 //将指定菜单项的标签拷贝到指定的缓冲区。MF_BYPOSITION的解释见上。
9 if(str=="Always on &Top") //如果是刚才我们增加的菜单项,则删除。
10 {
11 (mainmenu->GetSubMenu (0))->DeleteMenu(i,MF_BYPOSITION);
12 break;
13 }
14 }
15、测试ALT键是否按下:
GetKeyState(VK_MENU);
GetAlt();
16、检查是否按下鼠标左键
if((nFlags&MK_LBUTTON)==MK_LBUTTON)
17、检查键盘输入
在OnKeyDown中的参数nChar是一个数值,当显示的时候,需要转换成字符,使用如下的命令:
char lsChar;
lsChar=char(nChar);
if(lsChar=='A');
{
...
}
18、调用另一个函数::GetKeyState(),用一个特定的键代码来确定法键是否被按下。如果::GetKeyState函数的返回值是负的,表示该键被按下。如果返回值是非负的,表示该留未被按下。例如:如果要确定shift键是否被按下,可以使用下面的代码:
1 if(::GetKeyState(VK_SHIFT)<O)
2 {
3 AfxMessageBox("shift is pressed");
4 }
19.如何在编程的过程中随时结束应用程序(常规)
1)需要向窗口发送 WM_CLOSE/WM_QUIT消息,
调用 CWnd::OnClose成员函数并允许对用户提示是否保存修改过的数据.
AfxGetMainWnd()->SendMessage(WM_CLOSE); //别忘了先得到当前窗口的指针
2)使用函数: void PostQuitMessage( int nExitCode // exit code );
3)使用标准函数:void exit( int status ); //尽量不要在MFC中使用
20.得到屏幕的尺寸大小
1 HWND hWnd;
2 CRect Rect;
3 hWnd = ::GetDesktopWindow();
4 ::GetClientRect(hWnd, &Rect);
//———————————————————
如何查询和设置系统参数
在Windows 3.1 SDK中介绍过SDK函数SystemParametersInfo,调用该函数可以查询和设置系统参数,诸如按键的重复速率设置、鼠标双击延迟时间、图标字体以及桌面覆盖位图等等。

Code
1 //Create a font that is used for icon titles.
2 LOGFONT stFont;
3 ::SystemParametersInfo(SPIF_GETICONTITLELOGFONT,
4 sizeof (LOGFONT),&stFont,SPIF_SENDWININICHANGE);
5 m_font.CreateFontIndirect (&stFont); //Change the wallpaper to leaves.bmp.
6 :: SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, _T("forest.bmp"),SPIF_UPDATEINIFILE);
//———————————————————
如何使用一个预定义的Windows光标
调用CWinApp:: LoadStandardCursor并传送光标标识符。

Code
1 BOOL CSampleDialog::OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message){
2 //Display wait cursor if busy.
3 if (m_bBusy){
4 SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT));
5 return TRUE;
6 }
7 return CDialog:: OnSetCursor (pWnd. nHitTest,message);
8 }