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

2022-09-26 09:33:08 字數 1276 閱讀 3751

通過呼叫win32 api實現。

複製** **如下:

public class user32api

}[dllimport("user32.dll", entrypoint = "en", setlasterror = true)]

public static extern bool enumwindows(wndenumproc lpenumfunc, uint lparam);

[dllimport("user32.d程式設計客棧ll", entrypoint = "getparent", setlasterror = true)]

public static extern intptr getparent(intptr hwnd);

[dllimport("user32.dll", entrypoint = "getwindowthreadprocessid")]

public static extern uint getwindowthreadprocessid(intptr hwnd, ref uint lpdwprocessid);

[dllimport("user32.dll", entrypoint = "iswindow")]

public static extern bool iswindow(intptr hwnd);

[dllimport("kernel32.dll", entrypoint = "setlasterror")]

public static extern void setlasterror(uint dwerrcode);

public static intptr getcurrentwindowhandle()

else

}bool bresult = enumwindows(new wndenumproc(enumwindowsproc), uipid);

// 列舉視窗返回 false 並且沒有錯誤號時表明獲取成功

if (!bresult && marshal.getlastwin32error() == 0)

}return ptrwnd;

}private static bool enumwindowsproc(intptr hwnd, uint lparam)

}return true;

}} 呼叫user32api.getcurrentwindowhandle()即可返回當前程序的主視窗控制代碼,如果獲取失敗則返回intptr.zero。

--eof--

本文標題: c#獲取程序的主視窗控制代碼的實現方法

本文位址:

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

當我們啟動乙個程式,用 process process process.start path path是程式的絕對路徑 啟動時,獲取的process.handle其實是程序的控制代碼,並不是視窗的控制代碼,而有時process.mainwindowhandle卻等於0 此時就需要用列舉來獲取啟動程序...

如何根據程序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...