source

윈도 인스턴스의 hWnd를 얻는 방법

ittop 2023. 4. 12. 22:59
반응형

윈도 인스턴스의 hWnd를 얻는 방법

WPF 어플리케이션에는 여러 개의 창이 있습니다.Win32 API 콜에서 사용하려면 각 윈도 인스턴스의 hWnd를 취득할 수 있어야 합니다.

하고 싶은 일의 예:

Window myCurrentWindow = Window.GetWindow(this);
IntPtr myhWnd = myCurrentWindow.hWnd; // Except this property doesn't exist.

어떻게 하면 좋을까요?

WindowInteropHelper 네 친구잖아컨스트럭터에는 다음 명령어를 사용할 수 있습니다.Window파라미터 및 aHandle창 핸들을 반환하는 속성입니다.

Window window = Window.GetWindow(this);
var wih = new WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;

더글러스의 대답을 연장하면, 만약Window아직 표시되지 않았습니다.HWND가 없을 수 있습니다.다음 명령을 사용하여 창을 표시하기 전에 강제로 만들 수 있습니다.

var window = Window.GetWindow(element);

IntPtr hWnd = new WindowInteropHelper(window).EnsureHandle();

주의:Window.GeWindow돌아올 수 있다null그러니까 그것도 꼭 테스트해 보세요.

언급URL : https://stackoverflow.com/questions/10675305/how-to-get-the-hwnd-of-window-instance

반응형