Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#和部分工作的SendMessage?_C#_.net_Windows_Winapi - Fatal编程技术网

C#和部分工作的SendMessage?

C#和部分工作的SendMessage?,c#,.net,windows,winapi,C#,.net,Windows,Winapi,我曾尝试在Windows7中向各种应用程序发送文本消息(如记事本++、记事本等)。SendMessage适用于记事本,但不适用于记事本++ 若我将文本发送到记事本,所有内容看起来都很好,整个文本,我从SendMessage方法返回1。但是对于记事本+,只会出现第一个字母,方法SendMessage返回0,但是Marshal.GetLastWin32Error()没有返回任何错误(结果0) 代码如下: GUITHREADINFO gti = new GUITHREADINFO(); IntPtr

我曾尝试在Windows7中向各种应用程序发送文本消息(如记事本++、记事本等)。SendMessage适用于记事本,但不适用于记事本++

若我将文本发送到记事本,所有内容看起来都很好,整个文本,我从SendMessage方法返回1。但是对于记事本+,只会出现第一个字母,方法SendMessage返回0,但是Marshal.GetLastWin32Error()没有返回任何错误(结果0)

代码如下:

GUITHREADINFO gti = new GUITHREADINFO();
IntPtr hWnd = GetForegroundWindow();
uint processId;
uint activeThreadId = GetWindowThreadProcessId(hWnd, out processId);
if (GetInfo(activeThreadId, out gti))
{
    int EM_REPLACESEL = 0x00C2;

    int error = Marshal.GetLastWin32Error();
    int result = SendMessageW(gti.hwndCaret, EM_REPLACESEL, -1, passed);//3rd param doesn't change anything
    error = Marshal.GetLastWin32Error();
}
WinAPI函数和GetInfo方法以及所需的结构定义如下:

[DllImport("User32.dll", EntryPoint = "SendMessageW", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int SendMessageW(IntPtr hWnd, int uMsg, int wParam, string lParam);

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
//"Borrowed" code
public static bool GetInfo(uint hwnd, out GUITHREADINFO lpgui)
{
     uint lpdwProcessId;
     uint threadId = GetWindowThreadProcessId(hwnd, out lpdwProcessId);

     lpgui = new GUITHREADINFO();
     lpgui.cbSize = Marshal.SizeOf(lpgui);

    return GetGUIThreadInfo(threadId, ref lpgui);
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int iLeft;
    public int iTop;
    public int iRight;
    public int iBottom;
}

[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
    public int cbSize;
    public int flags;
    public IntPtr hwndActive;
    public IntPtr hwndFocus;
    public IntPtr hwndCapture;
    public IntPtr hwndMenuOwner;
    public IntPtr hwndMoveSize;
    public IntPtr hwndCaret;
    public RECT rectCaret;
}

因此,我想知道我应该怎么做才能修复它,使它(完全)适用于所有窗口,而不仅仅适用于记事本中的编辑控件?

EM_REPLACESEL应该只发送到编辑或RichEdit控件。这不是Notepad++所使用的。
GetLastWin32Error
在调用
SendMessage
后不会返回任何有用的内容。只有在上一次调用返回一个表示应该调用的值时,才应该调用该函数。在所有其他情况下,该值未定义。