在Windows資源瀏覽器中,,可在DVD/CD光驅(qū)圖標(biāo)上單擊鼠標(biāo)右鍵,,選擇"彈出"來打開光驅(qū)倉門,,你可能也發(fā)現(xiàn)了,,菜單中并沒有"關(guān)閉"命令來關(guān)閉光驅(qū)。下面,,就讓我們用程序來控制打開,、關(guān)閉光驅(qū)。 - _: ]* F% _4 u: u3 _9 a5 f+ A
4 q/ ^6 a, ~1 X( { 程序的主要工作部分為CD_OpenClose(BOOL bOpen, TCHAR cDrive)函數(shù):* I7 O) Q8 Q- P , c' {. S- l# F6 f5 E2 I" @% E//cDrive是光驅(qū)盤符,,或者0x01為默認(rèn)驅(qū)動器,。- C, x, `% m" A0 N; a //例如:4 J$ T$ _$ @5 j; I# D //CD_OpenCloseDrive(TRUE, 'G'); //打開光驅(qū)G:' w, K; Z; k$ Y! W; M //CD_OpenCloseDrive(FALSE, 'G'); //關(guān)閉光驅(qū)G:& D* V7 v5 X- l1 l //CD_OpenCloseDrive(TRUE, 1); //打開第一個邏輯光驅(qū)$ S* J& _ m6 x i& c. u6 ~ 8 m6 Z* i3 k8 S( x! t- j- }void CD_OpenCloseDrive(BOOL bOpenDrive, TCHAR cDrive) : N# h4 F. b1 i% z" k: ]{ 4 V1 w4 G9 c- h' y. h+ |* { MCI_OPEN_PARMS op; 6 Y& L6 Y2 Z5 q MCI_STATUS_PARMS st;9 z, P& e( L: s. J DWORD flags;2 O( [% _) @2 ]% ? / d. M; W( V8 j4 L1 H5 l TCHAR szDriveName[4];: H& c9 p; O( h! b! J6 {5 J" {' f strcpy(szDriveName, "X:"); & b* Q! k8 P4 g x; m; E; ~! b% [ a( M$ n! J ::ZeroMemory(&op, sizeof(MCI_OPEN_PARMS)); " C% m) {, _* S0 S: o9 L( N op.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;& r' ^& X5 l8 a% f9 G _: J. P4 ^ c) D% _2 ~4 e" N# r if(cDrive > 1) ) H) i0 {5 \9 m( U { o1 X- L3 U! p% S' k4 b szDriveName[0] = cDrive;/ l- R$ R7 H/ g4 C# ?3 h- h, a7 O op.lpstrElementName = szDriveName; 3 N& a- d& A# }: e a; I, n2 k) c flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT| MCI_OPEN_SHAREABLE;: p# @4 {/ h: r* R } + b2 q# Y- p4 |" c8 e else flags = MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID | MCI_OPEN_SHAREABLE; ( M. _5 {' H* u) h, E: F& \7 X) H' }* s9 N2 ^, T+ J if (!mciSendCommand(0,MCI_OPEN,flags,(unsigned long)&op))% w' S$ s) t0 }& j6 T$ F {3 Q% ] M" c: \) ^8 J! e st.dwItem = MCI_STATUS_READY;0 A! g2 n J7 z5 b% } 0 ]+ y! n' ~+ @* k" y- t. ~* H0 V( p if(bOpenDrive) 1 f# l" P7 G2 }' Y& o, U4 n0 I mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);2 k7 E& `) e! W& t1 W3 ^ else H( q+ h" f' {& N" W mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);4 l& u' z- @2 }& n / q8 e( J1 E! x/ n9 r+ k mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,0);2 r% t& J/ b% M: Y$ C }% u4 Q8 i! h. m0 H( g }0 m) r8 U; B9 J7 Z- R5 O: E# D5 M 7 t2 f: P" c( A; Z8 [5 i* Z為了方便對多個光驅(qū)進(jìn)行操作,添加了下面這個函數(shù),,其會調(diào)用上面的CD_OpenCloseDrive()函數(shù): , p! u+ u# z% i1 Svoid CD_OpenCloseAllDrives(BOOL bOpenDrives) . J/ h: L1 Q0 K$ L( `8 s' Y{ , V( Z9 U0 R# D3 A //判定所有光驅(qū),,并逐個打開或關(guān)閉。 2 k" k/ i. [( |# o+ Y3 T$ r int nPos = 0; C7 }3 \3 B0 C( | UINT nCount = 0;/ Q7 q% n8 O- R! k1 X TCHAR szDrive[4]; * h- X' ~7 e1 F3 j a, ^ strcpy(szDrive, "?:");' s0 w1 X2 E, E2 [ , U! m# {' M9 `: ?' V; G+ W DWORD dwDriveList = ::GetLogicalDrives ();# F9 C R. I O# N6 P 9 u" b$ u# T& g7 B s4 q! Y while (dwDriveList) { ! K( ^8 O+ s+ ^+ L; Y2 ]; P4 k if (dwDriveList & 1) $ p& w- U8 P9 d3 n) z! ] { 7 t$ B. y; b" U! N& F2 P7 E szDrive[0] = 0x41 + nPos; : o' J$ s2 T7 @. n6 a, c- } if(::GetDriveType(szDrive) == DRIVE_CDROM) + @* Q& s$ l0 z' w CD_OpenCloseDrive(bOpenDrives, szDrive[0]); 0 H( `+ s+ b: S# W/ T } 8 O* \6 V5 R, m) C dwDriveList >>= 1; ) P2 j- G3 D) ] E) r0 [, T c+ X nPos++;$ l( ]+ Y* Y3 P& W& n; p. D, R } 6 M) i" {" @ B1 |} 5 x1 w9 J( W' Q& [7 _% N4 D 最后一點,,別忘了在程序開頭包括Mmsystem.h頭文件,,及在鏈接選項里寫上Winmm.lib。 #pragma comment("lib","Winmm.lib")
|
|