2017-05-09 11 views
0

他のアプリケーションでプログラムでコンボボックスを更新しようとしています。私が更新しているアプリケーションには、コンボボックス、テキストボックス、およびいくつかのチェックボックスがあります。 コンボボックスで選択されている項目によっては、特定のチェックボックスが有効または無効になります。私が抱えている問題は、コンボボックスの現在の項目を変更することはできますが、guiはそれに応じてチェックボックスを更新していないようです。ここでC# - WINAPI CB_SETCURSELが期待通りに機能しない

は私のコードは

[DllImport("User32.dll")] 
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); 

[DllImport("user32.dll")] 
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 


const int CB_SETCURSEL = 0x014E; 

static void Main(string[] args) 
{ 

     var startinfo = new ProcessStartInfo("path"); 

     // Get an object that contains all the process resources 
     Process rtugen = Process.Start(startinfo); 

     // wait until the process has a main window handle 
     while (rtugen.MainWindowHandle == IntPtr.Zero) 
     { 
      rtugen.Refresh(); 
     } 


     IntPtr hWnd = rtugen.MainWindowHandle; 

     IntPtr controllerType = FindWindowEx(hWnd, IntPtr.Zero, "ComboBox", null); 

     SendMessage(controllerType, CB_SETCURSEL, 12, ""); 

     int send_cbn_selchange = MakeWParam((int)controllerType, CBN_SELCHANGE); 
     int i = SendMessage(hWnd, 0x111, send_cbn_selchange,0); 

} 

    static int MakeWParam(int loWord, int hiWord) 
    { 
     return (loWord & 0xFFFF) + ((hiWord & 0xFFFF) << 16); 
    } 

あるので、上記のコードは、コンボボックスを更新していますが、GUIは更新が行われた後のチェックボックスを更新していないようです。私はspy ++のコンボボックスの変更を監視し、同じコマンドが送信されたことを私に示しました。何が間違っているのかについてのアイデア?

+0

UIAutomationを使用すると、これは大幅に簡単になります。親ウィンドウにはCBN_SELCHANGE通知が必要です。これは変更を識別するために使用されているものが変更されたことです。 –

+0

さて、私はUIAutomationを調べることを検討します。親ウィンドウにCBN_SELCHANGE通知を送信することを示すコードを編集しましたが、まだ動作しません。 CBN_SELCHANGE通知を送信する正しい方法にいくつかのコードを提供できますか? –

+0

正しい方法はUIオートメーションを使用することです –

答えて

-1

最終的に私はこのソリューションをオンラインで見つけて、アプリケーションがその変更を認識できるようにコンボボックスの値を変更しました。コンボボックスのハンドルを取得したら、WM_KEYDOWNメッセージを使用してキーダウンアクションをシミュレートできます。

[DllImport("User32.dll")] 
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); 

[DllImport("user32.dll")] 
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 


const int CB_SETCURSEL = 0x014E; 
const int VK_DOWN = 0x28; 
const int WM_KEYDOWN = 0x0100; 
static void Main(string[] args) 
{ 

     var startinfo = new ProcessStartInfo("path"); 

     // Get an object that contains all the process resources 
     Process rtugen = Process.Start(startinfo); 

     // wait until the process has a main window handle 
     while (rtugen.MainWindowHandle == IntPtr.Zero) 
     { 
      rtugen.Refresh(); 
     } 


     IntPtr hWnd = rtugen.MainWindowHandle; 

     IntPtr controllerType = FindWindowEx(hWnd, IntPtr.Zero, "ComboBox", null); 

     SendMessage(controllerType, CB_SETCURSEL, 0, ""); 
     for (int i = 0; i < Index; i++) 
      SendMessage(controllerType, WM_KEYDOWN, VK_DOWN, 0); 

    } 
+0

この種のハッカーはすべてオンラインに普及していますが、それを行う方法ではありません。 UIオートメーションを使用します。 –

+0

UIオートメーションについて知る良い情報源はありますか?どこで学びましたか? –

+0

いくつかのwebsearchで始める –

関連する問題