2016-09-28 6 views
-2

メモ帳のウィンドウにメッセージを送信するために取得し、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'を見ることができますが、なぜそうでないのか判断するためにはウィンドウに送信

enter image description here

を追加しました

新コードレミーの提案に基づいて

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"); 
      } 

     } 
    } 
} 
+0

'SendMessage()'の戻り値は何ですか? [文書化されている](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632644.aspx): "*テキストが設定されている場合、戻り値は' TRUE'で、 'FALSE'ですエディットコントロールのテキストを設定するのに十分なスペースがない場合は、 'CB_ERRSPACE'(リストボックスの場合)、' CB_ERRSPACE'(コンボボックスの場合)、 'CB_ERRSPACE' * " –

+0

@RemyLebeau私が現時点で望んでいることは、私が作業デモに持っているコードを作る調整です。私はwinapiについてほとんど知りません、PInvoke 、マーシャルなど。 – barlop

+0

これ以上のものを試してください: '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"); ' –

答えて

0

レミーには大きなクレジットがあります。いくつかのトラブルシューティングの後、私はコードが正常に動作していることを知りました。

良いトラブルシューティングのステップは、nircmdを使ってウィンドウを移動させて、ハンドルを持っていることを確認することです。あなたは0x00230632あなたがすべきハンドルを見つけるものは何でもすることnircmd win close handle 0x00230632変更などのnircmdコマンドを使用することができます

ウィンドウのハンドルを取得するには、あなたがnirsoft winlisterを使用することができ、またはwinspector

。または、それを閉じないようにしてください。そうしなければ、新しいウィンドウに新しいハンドルが追加されます。nircmd win move handle 0x00B917AE 80 10 100 100のようなコマンドです。コードの問題にかかわらず、ハンドルが正しいことがわかります。

Winspectorも、C#コードに戻って

enter image description here

、あなたが

が、それはに書くべき子をスキップして、親への書き込みを試みることができるメモ帳ウィンドウの子を示していウィンドウのタイトル

SendMessageを使用してウィンドウを直接指定してみてください。ハンドルは16進ではなく10進で記述します。例えばハンドルが3479948の場合は

となります。 SendMessage(new IntPtr(3479948), WM_SETTEXT, IntPtr.Zero, "abcdee");

notepads [0] .MainWindowHandleが正しい値を取得していることを確認することもできます。winspectorに表示されているハンドル。

IntPtr child = FindWindowEx(...)」の行を見ると、「子」が子ハンドルをピックアップしていることを確認できます。

SendMessageを使用して直接書き込むことができます。例えばSendMessage(new IntPtr(1234567), WM_SETTEXT, IntPtr.Zero, "abcdee"); //(winspectorがその子ウィンドウをそのハンドルとして表示している場合)。

関連する問題