vc 6.0建立以一个Win32 hello world 程序,代码如下
//
TestWin32.cpp : Defines the entry point for the application.
//
#include
"
stdafx.h
"
#include
"
resource.h
"
#include
"
dbt.h
"
#include
"
windows.h
"
#define
MAX_LOADSTRING 100
//
Global Variables:
HINSTANCE hInst;
//
current instance
TCHAR szTitle[MAX_LOADSTRING];
//
The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];
//
The title bar text
//
Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE,
int
); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//
检测光驱
char
chFirstDriveFromMask(ULONG unitmask);
char
chFirstDriveFromMask (ULONG unitmask)
...
{ char i; for (i = 0 ; i < 26 ; ++ i) // 假设不会超过26个逻辑驱动器 ... { if (unitmask & 0x1 ) // 看该驱动器的状态是否发生了变化 break ; unitmask = unitmask >> 1 ; } return (i + ' A ' ); }
//
int
APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int
nCmdShow)
...
{ // TODO: Place code here. MSG msg; HACCEL hAccelTable; // Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_TESTWIN32, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if ( ! InitInstance (hInstance, nCmdShow)) ... { return FALSE; } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TESTWIN32); // Main message loop: while (GetMessage( & msg, NULL, 0 , 0 )) ... { if ( ! TranslateAccelerator(msg.hwnd, hAccelTable, & msg)) ... { TranslateMessage( & msg); DispatchMessage( & msg); } } return msg.wParam; }
//
//
FUNCTION: MyRegisterClass()
//
//
PURPOSE: Registers the window class.
//
//
COMMENTS:
//
//
This function and its usage is only necessary if you want this code
//
to be compatible with Win32 systems prior to the 'RegisterClassEx'
//
function that was added to Windows 95. It is important to call this function
//
so that the application will get 'well formed' small icons associated
//
with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
...
{ WNDCLASSEX wcex; 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); return RegisterClassEx( & wcex); }
//
//
FUNCTION: InitInstance(HANDLE, int)
//
//
PURPOSE: Saves instance handle and creates main window
//
//
COMMENTS:
//
//
In this function, we save the instance handle in a global variable and
//
create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance,
int
nCmdShow)
...
{ HWND hWnd; hInst = hInstance; // Store instance handle in our global variable hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0 , CW_USEDEFAULT, 0 , NULL, NULL, hInstance, NULL); if ( ! hWnd) ... { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; }
//
//
FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//
PURPOSE: Processes messages for the main window.
//
//
WM_COMMAND - process the application menu
//
WM_PAINT - Paint the main window
//
WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
...
{ // BOOL fRet = TRUE; // 返回值 // 通过响应WM_DEVICECHANGE消息得到的设备事件信息结构 PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; // int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; TCHAR szHello[MAX_LOADSTRING]; LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) ... { /**/ /* case WM_INITDIALOG: fRet = TRUE; break; */ // 对 WM_DEVICECHANGE 消息进行处理 case WM_DEVICECHANGE: char szMsg[ 80 ]; // 对话框中要表示的字符串 switch (wParam) ... { // 当一个设备变得被插入并变得可用时, // 系统会发送广播事件DBT_DEVICEARRIVAL case DBT_DEVICEARRIVAL: // 判断CDROM碟片是否已经插入到光驱中 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) ... { PDEV_BROADCAST_VOLUME lpdbv = (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 case DBT_DEVICEREMOVECOMPLETE: // 判断CDROM碟片是否从光驱中移走 if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME) ... { PDEV_BROADCAST_VOLUME lpdbv = (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 ; } // case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the menu selections: switch (wmId) ... { case IDM_ABOUT: DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About); break ; case IDM_EXIT: DestroyWindow(hWnd); break ; default : return DefWindowProc(hWnd, message, wParam, lParam); } break ; case WM_PAINT: hdc = BeginPaint(hWnd, & ps); // TODO: Add any drawing code here... RECT rt; GetClientRect(hWnd, & rt); DrawText(hdc, szHello, strlen(szHello), & rt, DT_CENTER); EndPaint(hWnd, & ps); break ; case WM_DESTROY: PostQuitMessage( 0 ); break ; default : fRet = FALSE; return DefWindowProc(hWnd, message, wParam, lParam); } // 禁止光驱的AutoPlay功能 以下代码在windows server2003 上不启作用,要用注册表的方法 /**/ /* static UINT uMsgQueryCancelAutoPlay=RegisterWindowMessage("QueryCancelAutoPlay"); if (message==uMsgQueryCancelAutoPlay) { int n = MessageBox(hWnd, "你想禁止AutoPlay功能吗?", NULL,MB_YESNO | MB_ICONQUESTION); // 1代表取消 AutoPlay // 0 t代表允许AutoPlay SetWindowLong(hWnd, message, (n == IDYES) ? 1 : 0); fRet = (n == IDYES)?1:0; } */ return 0 ; }
//
Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
...
{ switch (message) ... { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) ... { EndDialog(hDlg, LOWORD(wParam)); return TRUE; } break ; } return FALSE; }