C# sendmessage 的问题?
你好,关于C#对其他windows进程进行通信我思考了
第一步 是增加
("using System.Runtime.InteropServices;")
第二步是调用win32api,加入sendmessage和findwindow
{ [DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName,string
lpWindowName);
//定义消息常数
public const int USER = 0x0400; // 是windows系统定义的用户消息
public const int TEST = USER + 1;
const int WM_Lbutton = 0x201; //定义了鼠标的左键点击消息
}
then add查找窗口,这里以记事本为例
{
int WINDOW_HANDLER = FindWindow(null, @"无标题 - 记事本");
if (WINDOW_HANDLER == 0)
}
third send message
SendMessage(WINDOW_HANDLER, WM_SETTEXT,0,caption);
这里他报“Error 1 The name 'WM_SETTEXT' does not exist in the current context”
问题1:如何解决这个报错?如何设置标题?
问题2:如何从一个listbox里拷贝字符串出来?
以上问题各为50分 --------------------编程问答-------------------- 'WM_SETTEXT'
你首先要了解windows消息机制,然后从网上搜一下windows消息大全,从中取出WM_SETTEXT的定义值,消息就是一个整型数字,然后在你的C#程序中定议常量WM_SETTEXT或直接写WM_SETTEXT的消息值(但这样不直观) --------------------编程问答-------------------- 了解windows消息机制
了解windows消息机制 --------------------编程问答--------------------
--------------------编程问答-------------------- 第一个问题解决了
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wparam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, bool wparam, int lParam);//首先在program中声明sendmessage方法
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack lpfn, int lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
const int WM_SHOWDROPDOWN = 0x014D;//在窗体中声明消息常量
const int WM_SETCURSOR = 0x14F;
const int CB_FINDSTRING = 0x14C;
谢谢3楼 --------------------编程问答-------------------- 推荐3楼 推荐3楼 推荐3楼 --------------------编程问答--------------------
const int LB_GETCOUNT = 0x18B;
const int LB_GETTEXT = 0x189;
const int LB_GETTEXTLEN = 0x18A;
第二个问题
也用POSTMESSAGE这个函数 --------------------编程问答-------------------- spy++ 查找listBox 类名 及层次位置,取得hwnd
--------------------编程问答-------------------- using System;
[DllImport("user32.dll ", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, StringBuilder lParam);
StringBuilder sb = new StringBuilder();
SendMessage(hwnd, WM_GETTEXT, sb.Capacity, sb); //取得文本
MessageBox.Show(sb.ToString());
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Rohan
{
class Program
{
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
string lParam // second message parameter
);
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName,string
lpWindowName);
//定义消息常数
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
const int WM_SHOWDROPDOWN = 0x014D;//在窗体中声明消息常量
const int WM_SETCURSOR = 0x14F;
const int CB_FINDSTRING = 0x14C;
static void Main(string[] args)
{
string str = "NGSNLL";
int WINDOW_HANDLER = FindWindow(null, @"无标题 - 记事本");
if (WINDOW_HANDLER == 0)
{
throw new Exception("Could not find Main window!");
}
SendMessage(WINDOW_HANDLER, WM_SETTEXT, 0,str);
}
}
}
--------------------编程问答-------------------- http://topic.csdn.net/u/20091112/21/318e7060-ee1e-42bc-8fde-e2b19e45bb48.html --------------------编程问答-------------------- WINDOW_HANDLER, WM_SETTEXT这些都是在C++或C的头文件中定义的整型常量,你必须搞清楚它们的含义,在C#中以C#语法再定义一次,才能正常使用.
问题2:如何从一个listbox里拷贝字符串出来?
这个问题太笼统. 这取决于listbox里绑定的是什么对象. --------------------编程问答-------------------- Runtime --------------------编程问答-------------------- 发现自己的windows应用程序设计方面的基础知识严重欠缺。
面壁ing…… --------------------编程问答--------------------
listbox里每一条都是字符串类型 --------------------编程问答--------------------
那是不是这样
foreach(object item in listbox.Items)
{
MessageBox.Show(item.ToString());
} --------------------编程问答--------------------
是windows中别的程序 = =汗。。。
那样我需要这么麻烦么? --------------------编程问答--------------------
试过7楼的方法没有,会出不来?
winapi 只区分hwnd ,是不是两个进程,代码通用。 --------------------编程问答-------------------- 我的方法操纵VB写的程序 没有任何问题,已经过测试 --------------------编程问答-------------------- good good study,day day up --------------------编程问答-------------------- 消息好难记,直接生写WndProc爱杂整杂整
补充:.NET技术 , C#