控制台应用程序 接收UI消息
//==========xisat @ 2009-2-10=========
一个古老的问题,箱底翻出来
标准的控制台应用程序是无法接收到系统的UI消息的,原因很简单,没有UI,windows消息没法投递过来。
需要解决这个问题也很简单
大致如下:
1. RegisterClassEx 注册窗口类
2. CreateWindow 创建隐藏窗口
3. 在WndProc 中处理消息
也可以这样:
1.CreateWindow创建一个已知类的窗口控件
2.SetWindowLong子类化该控件
3.在子类化的消息函数中处理消息
主要过程如下
view plaincopy to clipboardprint?
//一个消息循环是必需的
int messLoop()
{
MSG msg;
hWnd=cwindow();
g_oldLBProc=(WNDPROC)SetWindowLongA(hWnd, GWL_WNDPROC, (LONG) MyLBProc);//子类化窗口控件
// 消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//一个消息循环是必需的
int messLoop()
{
MSG msg;
hWnd=cwindow();
g_oldLBProc=(WNDPROC)SetWindowLongA(hWnd, GWL_WNDPROC, (LONG) MyLBProc);//子类化窗口控件
// 消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
cwindow()创建一个隐藏的button
view plaincopy to clipboardprint?
//建立隐藏窗口接受UI消息
HWND cwindow()
{
HWND hWnd;
hWnd = CreateWindow(L"Button",L"xcmd" ,WS_POPUP,
0, 0,
20, 30,
NULL,
0, NULL,GetModuleHandle(NULL),
NULL);
return hWnd;
//UpdateWindow(hWnd);
//ShowWindow(hWnd, 1);
}
//建立隐藏窗口接受UI消息
HWND cwindow()
{
HWND hWnd;
hWnd = CreateWindow(L"Button",L"xcmd" ,WS_POPUP,
0, 0,
20, 30,
NULL,
0, NULL,GetModuleHandle(NULL),
NULL);
return hWnd;
//UpdateWindow(hWnd);
//ShowWindow(hWnd, 1);
}
在子类化的消息处理函数中处理消息
view plaincopy to clipboardprint?
//子类化的窗口控件消息处理函数
LRESULT CALLBACK MyLBProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DEVICECHANGE:
printf(" 0x%X -- 0x%X ",uMsg,wParam);
//if(wParam==DBT_DEVICEARRIVAL)
//{
// DestroyWindow(hWnd);//销毁窗口,防止主线程中的waitforsingleobject阻塞消息
// ExitProcess(1);
//}
break;
}
return CallWindowProc(g_oldLBProc, hWnd, uMsg, wParam, lParam);
}
//子类化的窗口控件消息处理函数
LRESULT CALLBACK MyLBProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DEVICECHANGE:
printf(" 0x%X -- 0x%X ",uMsg,wParam);
//if(wParam==DBT_DEVICEARRIVAL)
//{
// DestroyWindow(hWnd);//销毁窗口,防止主线程中的waitforsingleobject阻塞消息
// ExitProcess(1);
//}
break;
}
return CallWindowProc(g_oldLBProc, hWnd, uMsg, wParam, lParam);
}
这里演示接受USB设备 WM_DEVICECHANGE 消息 如果还需要处理 DBT_DEVICEARRIVAL 消息
可以使用RegisterDeviceNotification 注册设备通知
cwindow()修改下
view plaincopy to clipboardprint?
HWND cwindow()
{
HWND hWnd;
hWnd = CreateWindow(L"Button",L"xcmd" ,WS_POPUP,
0, 0,
20, 30,
NULL,
0, NULL,GetModuleHandle(NULL),
NULL);
//GUID interfaceGUID={0x4D36E967,0xE325,0x11CE,0xBF,0xC1,0x08,0x00,0x2B,0xE1,0x03,0x18};//iscsi volume
GUID interfaceGUID={ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };//usb volume
HDEVNOTIFY hDeviceNotify ;
if(RegDeviceInterface(interfaceGUID,hWnd,&hDeviceNotify))
return hWnd;
//UpdateWindow(hWnd);
//ShowWindow(hWnd, 1);
}
HWND cwindow()
{
HWND hWnd;
hWnd = CreateWindow(L"Button",L"xcmd" ,WS_POPUP,
0, 0,
20, 30,
NULL,
0, NULL,GetModuleHandle(NULL),
NULL);
//GUID interfaceGUID={0x4D36E967,0xE325,0x11CE,0xBF,0xC1,0x08,0x00,0x2B,0xE1,0x03,0x18};//iscsi volume
GUID interfaceGUID={ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };//usb volume
HDEVNOTIFY hDeviceNotify ;
if(RegDeviceInterface(interfaceGUID,hWnd,&hDeviceNotify))
return hWnd;
//UpdateWindow(hWnd);
//ShowWindow(hWnd, 1);
}
view plaincopy to clipboardprint?
//注册设备通知b y MSDN example
BOOL RegDeviceInterface(
IN GUID InterfaceClassGuid,
IN HWND hWnd,
OUT HDEVNOTIFY *hDeviceNotify
)
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
*hDeviceNotify = RegisterDeviceNotification(
hWnd, // events recipient
&NotificationFilter, // type of device
DEVICE_NOTIFY_WINDOW_HANDLE // type of recipient handle
);
if ( NULL == *hDeviceNotify )
{
return FALSE;
}
return TRUE;
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/11/25/1610806.html,如需转载请自行联系原作者