如何將從窗體/控件的Handle屬性獲取的句柄轉換爲IWin32Window ^?將IntPtr窗口句柄轉換爲IWin32Window^
回答
(這可以讓你控制的對象,它實現了IWin32Window接口。)
EG。
IntPtr myWindowHandle = IntPtr(someVal);
IWin32Window^ w = Control::FromHandle(myWindowHandle);
請注意,這依賴於「從窗體/控件的Handle屬性獲取的」句柄。你不能在任意的Win32窗口句柄中使用這種技術。
有一個更簡單的方法,直接由.NET框架支持,而無需創建自己的自定義類。您可以使用任意任意窗口句柄。
鑑於ptrWindowHandle類型的IntPtr的:
using System.Windows.Forms;
NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(ptrWindowHandle);
System.Windows.Forms.NativeWindow實現IWin32Window接口。
你也可以使用靜態'NativeWindow.FromHandle(ptrWindowHandle);' – 2013-01-24 16:23:22
@MattSmith我發現它必須被分配。除非事先使用AssignHandle,否則FromHandle不適用於我。例如,這可以工作:'NativeWindow nativeWindow = new NativeWindow(); nativeWindow.AssignHandle(GetConsoleWindow()); box.ShowDialog(NativeWindow.FromHandle(GetConsoleWindow()));(甚至是'box.ShowDialog(nativeWindow);')...但是這個沒有賦值的單行不包含:'box.ShowDialog(NativeWindow。 FromHandle(GetConsoleWindow()));' – Jay 2017-12-14 07:31:16
- 1. 從Selenium webdriver的當前窗口獲取窗口句柄(IntPtr)GUID
- 2. 將筆轉換爲IntPtr
- 3. 將wpf文本框轉換爲iwin32window(winform)
- 4. 從DataGridViewTextBoxCell獲取句柄IntPtr?
- 5. 什麼是句柄? IntPtr
- 6. 將ProcessID轉換爲進程句柄
- 7. 窗口句柄和窗口尺寸
- 8. 將窗口句柄傳遞給變量
- 9. 窗口句柄C#/。NET
- 10. 獲取窗口句柄
- 11. 窗口的主句柄
- 12. 檢索Delphi窗口句柄
- 13. 驗證Win32窗口句柄
- 14. MFC中的窗口句柄?
- 15. Python pyautogui窗口句柄
- 16. 改變一個窗口的背景色,給定一個IntPtr句柄
- 17. 如何將IntPtr/Int轉換爲Socket?
- 18. 如何將IntPtr轉換爲流?
- 19. 如何將IntPtr轉換爲InteropService.SafeHandle?
- 20. 將類類型對象轉換爲Intptr
- 21. 在C#中將IntPtr轉換爲int *?
- 22. 如何將位圖轉換爲intptr C#
- 23. 如何將IntPtr轉換爲SafeHandle?
- 24. 將字符串轉換爲IntPtr
- 25. IntPtr轉換爲字節[] c#
- 26. 幫助將VB.NET「句柄」語句轉換爲C#
- 27. 將XAML WPF窗口轉換爲WinForm
- 28. 將JavaScript窗口對象轉換爲JSON
- 29. 將WPF BitmapSource轉換爲窗口圖標
- 30. 窗口句柄多久重用
James,我在你的答案中編輯了一個資格證書,因爲來自對IntPtr和IWin32Window進行更全面搜索的人可能只會閱讀問題標題而不理解問題細節中的約束條件。希望這沒關係。 – itowlson 2010-03-20 04:21:21
不,這是一個很好的編輯。你可能從幾個小時的頭腦中想起了爲什麼他不能奇蹟般地從原始HWND創建一個Control/IWin32Window對象,從而挽救了一個倒黴的未來程序員。 :) – 2010-03-20 07:54:28