windows 如何在空的 Visual C++ 项目(或控制台应用程序)中使用查找窗口和发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5248302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I use find window and sendmessage in empty visual c++ project (or console app)
提问by Juha
I want to make the most simplest application that can communicate via windows send messages (and parse json). I have found a sample code:
我想做最简单的应用程序,可以通过windows发送消息(并解析json)进行通信。我找到了一个示例代码:
CWnd* pWnd = FindWindow("old title");
pWnd->SendMessage(WM_SETTEXT,0,(LPARAM)"New title");
That works... but only if I use MS Visual Studios "create new MFC form application" wizard. How can I make a console application that sends messages to my program? Or can I? What do I need to include/link if I start an empty project or console application?
这有效......但前提是我使用 MS Visual Studios“创建新的 MFC 表单应用程序”向导。如何制作向我的程序发送消息的控制台应用程序?或者我可以吗?如果我启动一个空项目或控制台应用程序,我需要包含/链接什么?
The goal in pseudocode:
伪代码的目标:
a = ""
while !EOF
a += read(stdin)
commandArray = jsonToArray(a)
CWnd* pWnd = FindWindow("program");
pWnd->SendMessage(WM_COPYDATASTRUCT,0,commandArrayWrappedInCOPYDATASTRUCT);
exit
The annoyance is that the effective part of the code is roughly 20 lines (above), but the wizard generated part is hundreds of lines. And most of them is stuff that I don't understand. Plus, I get a window that I don't need.
令人烦恼的是,代码的有效部分大约为 20 行(上图),但向导生成的部分却有数百行。其中大部分是我不明白的东西。另外,我得到了一个不需要的窗口。
EDIT
编辑
Final main.cpp (without the json stuff):
最终的 main.cpp(没有 json 的东西):
/*
This closes calculator
*/
#include <Windows.h>
#include <atlstr.h>
int main (void)
{
HWND HWnd = FindWindow(NULL, CStringW("Calculator"));
SendMessage(HWnd, WM_CLOSE, 0, 0);
return 0;
}
br,
br,
Juha
朱哈
采纳答案by David Heffernan
If you want something so simple, then I'd just forget all about MFC and start with a basic console app from the New Project Wizard. MFC seems rather heavy duty for something so simple.
如果你想要这么简单的东西,那么我会忘记所有关于 MFC 的事情,并从 New Project Wizard 中的一个基本控制台应用程序开始。对于这么简单的事情,MFC 似乎很重要。