vc 6.0建立以一个Win32 hello world 程序,代码如下
//TestWin32.cpp:Definestheentrypointfortheapplication.
//
#include"stdafx.h"
#include"resource.h"
#include"dbt.h"
#include"windows.h"
#defineMAX_LOADSTRING100
//GlobalVariables:
HINSTANCEhInst;//currentinstance
TCHARszTitle[MAX_LOADSTRING];//Thetitlebartext
TCHARszWindowClass[MAX_LOADSTRING];//Thetitlebartext
//Fowarddeclarationsoffunctionsincludedinthiscodemodule:
ATOMMyRegisterClass(HINSTANCEhInstance);
BOOLInitInstance(HINSTANCE,int);
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULTCALLBACKAbout(HWND,UINT,WPARAM,LPARAM);
//检测光驱
charchFirstDriveFromMask(ULONGunitmask);
charchFirstDriveFromMask(ULONGunitmask)
...{
chari;
for(i=0;i<26;++i)//假设不会超过26个逻辑驱动器
...{
if(unitmask&0x1)//看该驱动器的状态是否发生了变化
break;
unitmask=unitmask>>1;
}
return(i+'A');
}
//
intAPIENTRYWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTRlpCmdLine,
intnCmdShow)
...{
//TODO:Placecodehere.
MSGmsg;
HACCELhAccelTable;
//Initializeglobalstrings
LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);
LoadString(hInstance,IDC_TESTWIN32,szWindowClass,MAX_LOADSTRING);
MyRegisterClass(hInstance);
//Performapplicationinitialization:
if(!InitInstance(hInstance,nCmdShow))
...{
returnFALSE;
}
hAccelTable=LoadAccelerators(hInstance,(LPCTSTR)IDC_TESTWIN32);
//Mainmessageloop:
while(GetMessage(&msg,NULL,0,0))
...{
if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg))
...{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
returnmsg.wParam;
}


//
//FUNCTION:MyRegisterClass()
//
//PURPOSE:Registersthewindowclass.
//
//COMMENTS:
//
//Thisfunctionanditsusageisonlynecessaryifyouwantthiscode
//tobecompatiblewithWin32systemspriortothe'RegisterClassEx'
//functionthatwasaddedtoWindows95.Itisimportanttocallthisfunction
//sothattheapplicationwillget'wellformed'smalliconsassociated
//withit.
//
ATOMMyRegisterClass(HINSTANCEhInstance)
...{
WNDCLASSEXwcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc=(WNDPROC)WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=LoadIcon(hInstance,(LPCTSTR)IDI_TESTWIN32);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName=(LPCSTR)IDC_TESTWIN32;
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,(LPCTSTR)IDI_SMALL);
returnRegisterClassEx(&wcex);
}
//
//FUNCTION:InitInstance(HANDLE,int)
//
//PURPOSE:Savesinstancehandleandcreatesmainwindow
//
//COMMENTS:
//
//Inthisfunction,wesavetheinstancehandleinaglobalvariableand
//createanddisplaythemainprogramwindow.
//
BOOLInitInstance(HINSTANCEhInstance,intnCmdShow)
...{
HWNDhWnd;
hInst=hInstance;//Storeinstancehandleinourglobalvariable
hWnd=CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hWnd)
...{
returnFALSE;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
returnTRUE;
}
//
//FUNCTION:WndProc(HWND,unsigned,WORD,LONG)
//
//PURPOSE:Processesmessagesforthemainwindow.
//
//WM_COMMAND-processtheapplicationmenu
//WM_PAINT-Paintthemainwindow
//WM_DESTROY-postaquitmessageandreturn
//
//
LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam)
...{
//
BOOLfRet=TRUE;//返回值
//通过响应WM_DEVICECHANGE消息得到的设备事件信息结构
PDEV_BROADCAST_HDRlpdb=(PDEV_BROADCAST_HDR)lParam;
//
intwmId,wmEvent;
PAINTSTRUCTps;
HDChdc;
TCHARszHello[MAX_LOADSTRING];
LoadString(hInst,IDS_HELLO,szHello,MAX_LOADSTRING);
switch(message)
...{

/**//*caseWM_INITDIALOG:
fRet=TRUE;
break;
*/
//对WM_DEVICECHANGE消息进行处理
caseWM_DEVICECHANGE:
charszMsg[80];//对话框中要表示的字符串
switch(wParam)
...{
//当一个设备变得被插入并变得可用时,
//系统会发送广播事件DBT_DEVICEARRIVAL
caseDBT_DEVICEARRIVAL:
//判断CDROM碟片是否已经插入到光驱中
if(lpdb->dbch_devicetype==DBT_DEVTYP_VOLUME)...{
PDEV_BROADCAST_VOLUMElpdbv=(PDEV_BROADCAST_VOLUME)lpdb;
//判断是否有CDROM碟片
if(lpdbv->dbcv_flags&DBTF_MEDIA)
...{
//显示消息,获取光驱的逻辑驱动器号
wsprintf(szMsg,"驱动器%c:已经可用 ",chFirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hWnd,szMsg,"光驱自动监测",MB_OK|MB_ICONINFORMATION);
}
}
break;
//当一个设备变得被移走并变得不可用时,
//系统会发送广播事件DBT_DEVICEREMOVECOMPLETE
caseDBT_DEVICEREMOVECOMPLETE:
//判断CDROM碟片是否从光驱中移走
if(lpdb->dbch_devicetype==DBT_DEVTYP_VOLUME)...{
PDEV_BROADCAST_VOLUMElpdbv=(PDEV_BROADCAST_VOLUME)lpdb;
if(lpdbv->dbcv_flags&DBTF_MEDIA)
...{
//显示消息,获取光驱的逻辑驱动器号
wsprintf(szMsg,"驱动器%c:已经弹出 ",chFirstDriveFromMask(lpdbv->dbcv_unitmask));
MessageBox(hWnd,szMsg,"光驱自动监测",MB_OK|MB_ICONINFORMATION);
}
}
break;
}
//
caseWM_COMMAND:
wmId=LOWORD(wParam);
wmEvent=HIWORD(wParam);
//Parsethemenuselections:
switch(wmId)
...{
caseIDM_ABOUT:
DialogBox(hInst,(LPCTSTR)IDD_ABOUTBOX,hWnd,(DLGPROC)About);
break;
caseIDM_EXIT:
DestroyWindow(hWnd);
break;
default:
returnDefWindowProc(hWnd,message,wParam,lParam);
}
break;
caseWM_PAINT:
hdc=BeginPaint(hWnd,&ps);
//TODO:Addanydrawingcodehere...
RECTrt;
GetClientRect(hWnd,&rt);
DrawText(hdc,szHello,strlen(szHello),&rt,DT_CENTER);
EndPaint(hWnd,&ps);
break;
caseWM_DESTROY:
PostQuitMessage(0);
break;
default:
fRet=FALSE;
returnDefWindowProc(hWnd,message,wParam,lParam);
}
//禁止光驱的AutoPlay功能以下代码在windowsserver2003上不启作用,要用注册表的方法

/**//*
staticUINTuMsgQueryCancelAutoPlay=RegisterWindowMessage("QueryCancelAutoPlay");
if(message==uMsgQueryCancelAutoPlay)
{
intn=MessageBox(hWnd,"你想禁止AutoPlay功能吗?",NULL,MB_YESNO|MB_ICONQUESTION);
//1代表取消AutoPlay
//0t代表允许AutoPlay
SetWindowLong(hWnd,message,(n==IDYES)?1:0);
fRet=(n==IDYES)?1:0;
}
*/
return0;
}
//Mesagehandlerforaboutbox.
LRESULTCALLBACKAbout(HWNDhDlg,UINTmessage,WPARAMwParam,LPARAMlParam)
...{
switch(message)
...{
caseWM_INITDIALOG:
returnTRUE;
caseWM_COMMAND:
if(LOWORD(wParam)==IDOK||LOWORD(wParam)==IDCANCEL)
...{
EndDialog(hDlg,LOWORD(wParam));
returnTRUE;
}
break;
}
returnFALSE;
}
本文介绍了一个使用VC6.0编写的Win32应用程序,该程序能够实时监测光驱状态的变化,并在CD-ROM插入或移除时显示相应的提示消息。
7636

被折叠的 条评论
为什么被折叠?



