特定のプロセスのウィンドウ クラス名を取得するにはどうすればよいですか? これをC#で実現したい。
c# でプロセス クラスを試しましたが、プロセスのウィンドウ名しか取得できません。
ありがとう
プロセスのメインウィンドウのクラス名を取得したいということだと思います。
これを行うにMainWindowHandle
は、Process
オブジェクトの を使用してメイン ウィンドウへのハンドルを取得し、次の相互運用メソッドを使用してクラス名を取得する必要があります。
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
サンプル コードについてはpinvoke.netを、関数の詳細についてはMSDNを参照してください。
また、 windows ui 自動化フレームワークを使用して、pinvoke を使用せずにこれを実現することもできます。
int pidToSearch = 316;
//Init a condition indicating that you want to search by process id.
var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty,
pidToSearch);
//Find the automation element matching the criteria
AutomationElement element = AutomationElement.RootElement.FindFirst(
TreeScope.Children, condition);
//get the classname
var className = element.Current.ClassName;