如何在控制台应用程序中使用仅消息窗口接收消息
How to receive messages using a message-only window in a console application?
我已经创建了一个简单的Win32控制台应用程序,它创建了一个隐藏的消息窗口并等待消息,完整的代码如下:
#include <iostream>
#include <Windows.h>
namespace {
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_COPYDATA)
std::cout << "Got a message!" << std::endl;
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
int main()
{
WNDCLASS windowClass = {};
windowClass.lpfnWndProc = WindowProcedure;
LPCWSTR windowClassName = L"FoobarMessageOnlyWindow";
windowClass.lpszClassName = windowClassName;
if (!RegisterClass(&windowClass)) {
std::cout << "Failed to register window class" << std::endl;
return 1;
}
HWND messageWindow = CreateWindow(windowClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
if (!messageWindow) {
std::cout << "Failed to create message-only window" << std::endl;
return 1;
}
MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
但是,我没有收到来自另一个应用程序的任何消息。GetMessage()
只是阻塞并且永远不会返回。我在发送消息的应用程序中使用具有相同类名的FindWindowEx()
,它找到了窗口。只是这条消息显然从未被接收过。
我在这里做错了什么吗?可以接收窗口消息的最小应用程序是什么?
您的消息可能被用户界面特权隔离阻止。在这种情况下,您可以使用ChangeWindowMessageFilterEx()
函数允许WM_COPYDATA消息通过。
我不确定你的程序有问题,但我总是使用相反的方法:我创建了一个win32应用程序,将其剥离并添加控制台窗口。
剥离所有包含winapp版本(也删除所有afx-垃圾文件):
// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER // Allow use of features specific to Windows XP or later.
#define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// MinWInApp.cpp : Defines the entry point for the application.
//
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <strsafe.h>
#define MAX_LOADSTRING 100
#define SZ_TITLE "MinWinApp"
#define SZ_WND_CLASS "MINWINAPP"
// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWnd;
HWND g_hwndNextViewer;
TCHAR* szWndClass = TEXT(SZ_WND_CLASS);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWndClass;
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
g_hInst = hInstance; // Store instance handle in our global variable
g_hWnd = CreateWindow(szWndClass, TEXT(SZ_TITLE), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!g_hWnd)
{
return FALSE;
}
ShowWindow(g_hWnd, SW_SHOW);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_CREATE:
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 0:
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
在这里可以找到添加控制台窗口的代码:http://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/
相关文章:
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- MFC 中的窗口消息管理:添加基类调用是否是强制性的?
- 输出窗口中消息的可视化工作室源
- 为什么初始化 Wintab 扩展后没有收到WT_PACKETEXT窗口消息?
- 与WM_DPICHANGED消息一起发送的建议窗口大小太大
- 如何处理窗口的消息?
- 发送到另一个窗口的鼠标移动消息不执行任何操作
- 打印窗口发送消息WM_PAINT或WM_PRINT
- 如何处理来自多个窗口的消息
- C++/CLI 项目如何在控制台窗口中显示消息
- 向Qt窗口发布消息不会生效
- 什么是窗口最大化/最小化/恢复的WM Windows消息
- 查找窗口( ... ) 未"查找"创建的消息窗口
- 如何让 wndproc 处理发送到多个窗口的消息
- 如何在Qt中弹出消息窗口
- 在弹出窗口中管理CEF客户端区域的窗口消息
- 仅显示消息窗口中的电源状态更改通知
- 如何在控制台应用程序中使用仅消息窗口接收消息
- Windows:当我从本机代码回调到托管代码时,将显示“仅消息”窗口