0

I cannot for the life of me work this one out. I have a win32 app with a XAMLIsland that hosts a custom UWP UserControl using C++/WinRT. I am trying to use winrt::Windows::Storage::Pickers::FolderPicker and as such need to call picker.as()->Initialize(hwnd) before I can use the picker.

How do I get the HWND either inside of the usercontrol or passed from the win32 hosting app? I cannot for the life of me work this out. Any hints / tips would be incredibly useful.

I haven't posted the full source as the below is basically the same but without the picker: https://github.com/microsoft/Xaml-Islands-Samples/tree/master/Standalone_Samples/CppWinRT_Desktop_Win32App

Stefan V
  • 33
  • 4
  • Which window handle to you need? Your native Win32's (main) window handle, or the window handle associated with the `CoreWindow` of the hosted XAML islands? – IInspectable Apr 09 '20 at 14:00
  • That's a good question, i had assumed the win32 main window handle. I am wanting to use the FilePicker from within the UserControl that is hosted in the Win32 app. – Stefan V Apr 09 '20 at 14:05
  • 2
    [This answer](https://stackoverflow.com/a/59528480/1889329) explains, how to get the `HWND` given a `CoreWindow` instance. If that's the one you need, you're all set. If it isn't, you can try to walk up the window hierarchy from there by repeatedly calling [GetAncestor](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor), until you reach your top-level window. – IInspectable Apr 09 '20 at 14:07
  • I had tried that with no success but then at this point i had no confidence in it. I hadn't however tried walking up with GetAncestor. I will have a bit more of a play and report back. Thank you again! – Stefan V Apr 09 '20 at 14:11
  • I left this last night at about 2am... Turns out it was working perfectly fine using the HWND from the CoreWindow, i was misinterpreting the exception (likely tired) as a HWND issue..... Turns out if I had bothered to follow up on the exception that it was telling me I needed to set a FileTypeFilter. Thank you so much for your input, it at least gave me the confidence to know I wasn't doing anything wrong. – Stefan V Apr 09 '20 at 14:48

0 Answers0