- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在使用 SendMessageA(handle,WM_SETTEXT,0,(LPARAM)text) 时遇到问题;如果在除构造函数之外的任何其他地方使用,它会导致崩溃。
MWC.h
#include <Windows.h>
#include <iostream>
#include <string>
class MWC{
private:
MSG msg;
public:
MWC();
~MWC();
int mLoop();
int mClose(UINT);
class System{
public:
System(){ }
~System(){ }
class Form{
public:
HWND handle; // Need to access it in order to create other controls
Form(char*,int,int,int,int);
Form(char*,int,int);
~Form(){ }
bool Show();
HWND ReturnHandle();
};
class TextBox{
protected:
HWND handle;
private:
int ID;
public:
TextBox(char* text,int width,int height,int x,int y,int id,Form* p);
TextBox(int width,int height,int x,int y,int id,Form* p);
~TextBox(){ }
bool IsChanged(UINT,WPARAM);
bool Text(char *);
int GetId(){ return ID; }
};
class messageBox{
public:
messageBox(){ };
~messageBox(){ };
bool Show(LPCWSTR,LPCWSTR,UINT);
bool ShowA(char*,char*,UINT);
bool ShowA(std::string,std::string,UINT);
};
};
};
MWC.cpp
#include "MWC.h"
MWC::MWC(){ }
MWC::~MWC(){ }
int MWC::mLoop(){
while(GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 1;
}
int MWC::mClose(UINT _msg){
if(_msg == WM_CLOSE){PostQuitMessage(0);return 1;}
return 0;
}
MWC::System::Form::Form(char* Title,int width,int height,int x,int y){
handle = CreateWindowEx(0,WC_DIALOG,Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE,x,y,width,height,NULL,NULL,NULL,NULL);
}
MWC::System::Form::Form(char* Title,int width,int height){
handle = CreateWindowEx(0,WC_DIALOG,Title,WS_OVERLAPPEDWINDOW | WS_VISIBLE,400,100,width,height,NULL,NULL,NULL,NULL);
}
bool MWC::System::Form::Show(){
return false;
}
HWND MWC::System::Form::ReturnHandle(){
return handle;
}
MWC::System::TextBox::TextBox(char* text,int width,int height,int x,int y,int id,Form* p){
ID = id;
handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",text,WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL);
}
MWC::System::TextBox::TextBox(int width,int height,int x,int y,int id,Form* p){
ID = id;
handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL);
SendMessageA(handle,WM_SETTEXT,0,(LPARAM)"It works here!");
}
bool MWC::System::TextBox::IsChanged(UINT message, WPARAM wParam){
if(message==WM_COMMAND && HIWORD(wParam)==EN_CHANGE && LOWORD(wParam)==ID){
return true;
}
return false;
}
bool MWC::System::TextBox::Text(char* text){
if(handle == NULL){
return 0;
}
else{
//SendMessageA(handle,WM_SETTEXT,0,(LPARAM)text); // It doesn't work here!
return 1;
}
return 1;
}
bool MWC::System::messageBox::Show(LPCWSTR text,LPCWSTR caption,UINT id){
::MessageBoxW(NULL,text,caption,id);
return true;
}
bool MWC::System::messageBox::ShowA(char* text,char* caption,UINT id){
::MessageBoxA(NULL,(LPCSTR)text,(LPCSTR)caption,id);
return true;
}
bool MWC::System::messageBox::ShowA(std::string text,std::string caption,UINT id){
::MessageBoxA(NULL,(LPCSTR)text.c_str(),(LPCSTR)caption.c_str(),id);
return true;
}
main.cpp
#include "MWC.h"
MWC mwc = MWC();
MWC::System::messageBox mbox = MWC::System::messageBox();
MWC::System::Form form1 = MWC::System::Form("Window",600,600);
MWC::System::TextBox textbox1 = MWC::System::TextBox(600,500,0,0,156,&form1);
BOOL WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
mwc.mClose(message);
//check if text in textbox has been changed by user
textbox1.Text("Test");
if(textbox1.IsChanged(message,wParam) == true){
}
return false;
}
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
SetWindowLong(form1.ReturnHandle(), DWL_DLGPROC, (long)myProc); // to be added in the class
mwc.mLoop();
return 0;
}
问题是 .. sendmessage 在 (MWC.cpp) 上运行良好:
MWC::System::TextBox::TextBox(int width,int height,int x,int y,int id,Form* p){
ID = id;
handle = CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT","",WS_VISIBLE | WS_CHILD,x,y,width,height,p->handle,(HMENU)id,NULL,NULL);
SendMessageA(handle,WM_SETTEXT,0,(LPARAM)"It works here!"); // TESTING
}
但如果在这里使用它会导致崩溃:
bool MWC::System::TextBox::Text(char* text){
if(handle == NULL){
return 0;
}
else{
//SendMessageA(handle,WM_SETTEXT,0,(LPARAM)text); // It doesn't work here!
return 1;
}
return 0;
}
顺便说一句,我只是一个玩弄 winapi 的业余爱好者 :)。请随时评论您碰巧发现的任何错误!
最佳答案
您的对话过程看起来完全错误。每次对话过程运行时,您都试图发送 WM_SETTEXT!这可能会导致无限递归和各种其他问题。
您的 SendMessage
调用绝对没问题,只是您无法在每次运行对话过程时都调用它。
我不明白 IsChanged()
函数是什么意思。你的框架设计很奇怪。我建议您先看看其他一些框架(例如 ATL),以获得有关如何围绕 Win32 编写框架的一些想法。或者,更好的是,只使用现有的框架。
关于c++ - Windows API SendMessage + WM_SETTEXT 结果崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7388625/
我正在编写 google chrome 扩展程序,并尝试将信息从注入(inject)网页的一段代码发送到我的内容脚本。 根据 http://developer.chrome.com/extension
我正在使用 websockets 和 Jetty 7.1.6.v20100715。 我有几个问题,主要是因为缺乏有关这些方法的信息/解释。 1st)sendMessage(字节帧,字符串数据)中的字节
我正在尝试调试我的 Windows 安装问题。细节并不特别重要,但我正在寻找以下问题的答案: explorer.exe receives a WM_SETTINGCHANGE message. In
我试图了解转换十六进制整数(十进制)和 IntrPtr 的规则是什么。 我在某处读到它应该代表“更高的内存”或类似的东西。 如果有人能向我解释一下,那就太好了。 但实际上它只是关于以下内容: 我想用S
我遇到了一个问题,即 SendMessage() 函数导致脚本挂起并因此永远不会退出,尽管它正在像它应该的那样运行 SendMessage(它的任务完成)。有没有办法解决这个问题,因为我有一段时间从主
我使用 InnoSetup 在注册表中设置环境变量: [Registry] Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session
我试图理解 SendMessage 函数,这是我的实际代码: [DllImport("user32.dll")] public static extern IntPtr SendMessage(Int
我正在将 Eclipse 与 ADT 结合使用,并且正在学习本教程:http://developer.android.com/training/basics/firstapp/starting-act
在使用 C++ 时,我已经花了很多时间试图解决这个问题。这段代码来自一个工作程序,我正在用 C# 重写它,但发生了一些我不明白的事情。 下面的代码正是我按下“Step Into”时运行的代码。现在使用
不确定为什么 SendMessage 没有从我需要的类中获取文本。我以前做过,但它是 VisualBasic,我想将它移植到 C++。我没有在任何其他程序上尝试过此代码。我正在阅读一些关于它可能是 u
在开始阅读问题之前请注意我阅读了 Petzold 的书(windows 编程第 5 版)! 在 form.h 中,我声明了名为 CMessage 的自定义消息(它代表“自定义消息”,稍后调用创建按钮的
我试图在两个应用程序之间传递消息 - 其中一个是插件,另一个是独立的配置实用程序。当我的插件检测到一个事件时,我想向我的实用程序发送一条消息并提示用户重新配置。 我使用的代码如下: [DllImpor
我正在尝试使用 sendmessage 将消息从我的 C++ 应用程序传递到 C# 我的c++代码是这样的 int _tmain(int argc, _TCHAR* argv[]) { COPYDA
为什么 Windows SendMessage() 总是返回零,即使消息传递成功?有没有办法用 SendMessage() 检查消息传递失败? 编辑 忘记提及我在 C++ DLL 中使用 SendMe
当我们发送消息时,“如果指定的窗口是由调用线程创建的,则立即将窗口过程作为子例程调用”。但是“如果指定的窗口是由不同的线程创建的,系统会切换到该线程并调用适当的窗口过程。只有当接收线程执行消息检索代码
我正在尝试通过 SendMessage(..) 方法将鼠标事件发送到 windows 中的窗口。 我面临的问题是消息似乎没有传送到我发送它们的窗口,即使 SendMessage 返回 0,这(根据文档
我已经成功地使用 Windows SendMessage 方法帮助我在我的文本编辑器中做各种事情,但每次我只是复制和粘贴别人建议的代码,我真的不知道它是什么意思。始终有一个神秘的消息编号作为参数。我如
我想从 MyServerProtocol 类外部调用 sendMessage 方法并向连接的客户端发送消息。我使用 threading 来做到这一点。 当我使用这段代码时: from autobahn
我正在编写 C# 自动化工具。 由于 Microsoft UI Automation 不提供任何模拟右键单击或弹出上下文菜单的方法,因此我使用 SendMessage 来代替。我宁愿不使用 SendI
我的问题是:如何使用 SendMessage() 实现两个线程之间的线程通信,一个有窗口(GUI),另一个没有窗口? 问题是 SendMessage() 需要一个句柄 (HWND)? 关于我的项目的另
我是一名优秀的程序员,十分优秀!