C 遍歷程序獲取主視窗控制代碼

2022-01-17 22:28:25 字數 1545 閱讀 3517

當我們啟動乙個程式,用 process process = process.start(path);//path是程式的絕對路徑

啟動時,獲取的process.handle其實是程序的控制代碼,並不是視窗的控制代碼,而有時process.mainwindowhandle卻等於0

此時就需要用列舉來獲取啟動程序的主視窗控制代碼了,**如下

/// /// 用於枚舉子窗體是的委託

///

/// 窗體控制代碼

/// 自定義

///

public delegate bool enumchildwindow(intptr windowhandle, string num);

/// /// 獲取指定窗體的所有子窗體

///

/// 窗體控制代碼

/// **委託

/// 自定義

///

[dllimport("user32.dll")]

public static extern int enumchildwindows(intptr winhandle, enumchildwindow ecw, string name);

/// /// 獲取指定窗體的標題

///

/// 窗體控制代碼

/// 緩衝區取用於儲存標題

/// 緩衝區大小

///

[dllimport("user32.dll")]

public static extern int getwindowtext(intptr winhandle, stringbuilder title, int size);

/// /// 獲取窗體型別

///

/// 窗體控制代碼

/// 型別

/// 緩衝區大小

///

[dllimport("user32.dll")]

public static extern int getclassname(intptr winhandle, stringbuilder type, int size);

/// /// 根據控制代碼獲得程序id值

///

/// 控制代碼

///

///

[dllimport("user32")]

private static extern int getwindowthreadprocessid(intptr handle, out int pid);

intptr mainhwnd = intptr.zero;//登入視窗控制代碼

string typename = string.empty;//啟動程式的視窗標題

/// /// 列舉窗體

///

///

///

///

private bool enumchild(intptr handle, string num)

return true;

}//**呼叫

pubilc booltest()

return true;

}

C 獲取程序的主視窗控制代碼的實現方法

通過呼叫win32 api實現。複製 如下 public class user32api dllimport user32.dll entrypoint en setlasterror true public static extern bool enumwindows wndenumproc lp...

如何根據程序id獲取該程序的主視窗控制代碼

最近剛好有個功能,需要這個邏輯,話不多說,上 struct handle data bool ismainwindow hwnd handle hwnd findmainwindow unsigned long process id bool callback enumwindowscallback...

已知程序控制代碼,如何知道此程序的主視窗控制代碼

已知程序控制代碼,如何知道此程序的主視窗控制代碼,在此說明兩種方法 1.呼叫findwindow 前提是必須知道該程序中視窗類名和視窗名,返回後的控制代碼可以通過iswindow 函式來驗證是否是有效的視窗控制代碼.hwnd hwnd findwindow szclassname,szwndname...