メモ帳のウィンドウにメッセージを送信するために取得し、C#はSendMessage関数は、私はこの質問を見てきました
How to send text to Notepad in C#/Win32?
私は問題ではないはずだと思うわずかな変動に答えるために...ということです私はメモ帳ウィンドウの束を持っています。これをテストするためにnotepad.exeをnotepad.exeにコピーしてnotepadd.exeを開いたので、私のメモ帳ウィンドウの1つのみがnotepadd.exeプロセスです。
は、私はそれがメモ帳のウィンドウに触れていない
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace testsendmessage
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("notepadd");
if (notepads.Length == 0) return;
if (notepads[0] != null)
{
IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, "abcd");
}
}
}
}
このコードを持っています。
私はデバッグを試みましたが、notepads配列に1つの項目が含まれていることがわかりましたが、これは間違いありません。
そして、それは「もし」の中に取得し、それがSendMessage(child, 0x000C, 0, "abcd");
を実行します。しかし、私はそれだけで何もメモ帳に表示されていないのコードからエラーが届かないメモ帳のウィンドウ
に表示されて何も見えませんそして、私はwinapiのことを本当に理解していないので、それを解決しようとして進める方法がわからないのですか?
あなたはそれがその行に到達するのを見ることができますが、ウォッチウィンドウを使ってnotepads Process配列と 'child'を見ることができますが、なぜそうでないのか判断するためにはウィンドウに送信
を追加しました
新コードレミーの提案に基づいて
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace testsendmessage
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", CharSet = CharSet.Unicode, EntryPoint = "SendMessageW")]
public static extern IntPtr SendMessageWStr(IntPtr hWnd, uint uMsg, IntPtr wParam, string lParam);
const uint WM_SETTEXT = 0x000C;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("notepadd");
if (notepads.Length == 0) return;
if (notepads[0] != null)
{
IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessageWStr(child, WM_SETTEXT, IntPtr.Zero, "abcd");
}
}
}
}
しかし、私はまだノートパッドのウィンドウがボタンをクリックする前に空白であったのと同じ問題を抱えています。 notepaddウィンドウにテキストを送信していません。テキストをその行に送ることを意図したコード行に到達しているにもかかわらず、
さらに追加します。
現在のコード、
私はFindWindowExWにFindWindowExを変更したと私はIntPtr.Zeroに新規のIntPtr(0)を変更した、それはまだ応答しません。
私はcmdからnotepadd.exeを開いていますが、そこにウィンドウが表示されています。もちろんタスクマネージャーのnotepadd.exeですが、アプリケーションのボタンをクリックしてもそのウィンドウにテキストは書き込まれません。
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace testsendmessage
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindowExW")]
public static extern IntPtr FindWindowExW(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", CharSet = CharSet.Unicode, EntryPoint = "SendMessageW")]
public static extern IntPtr SendMessageWStr(IntPtr hWnd, uint uMsg, IntPtr wParam, string lParam);
const uint WM_SETTEXT = 0x000C;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("notepadd");
if (notepads.Length == 0) return;
if (notepads[0] != null)
{
IntPtr child = FindWindowExW(notepads[0].MainWindowHandle, IntPtr.Zero, "Edit", null);
SendMessageWStr(child, WM_SETTEXT, IntPtr.Zero, "abcd");
}
}
}
}
'SendMessage()'の戻り値は何ですか? [文書化されている](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644.aspx): "*テキストが設定されている場合、戻り値は' TRUE'で、 'FALSE'ですエディットコントロールのテキストを設定するのに十分なスペースがない場合は、 'CB_ERRSPACE'(リストボックスの場合)、' CB_ERRSPACE'(コンボボックスの場合)、 'CB_ERRSPACE' * " –
@RemyLebeau私が現時点で望んでいることは、私が作業デモに持っているコードを作る調整です。私はwinapiについてほとんど知りません、PInvoke 、マーシャルなど。 – barlop
これ以上のものを試してください: 'const uint WM_SETTEXT = 0x000C; [SendMessageWStr(IntPtr hWnd、uint uMsg、IntPtr wParam、string lParam);を使用して、[DllImport( "User32.dll"、CharSet = CharSet.Unicode、EntryPoint = "SendMessageW"子、WM_SETTEXT、IntPtr.Zero、 "abcd"); ' –