#AutoIt3Wrapper_UseX64=Y #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include #include #include "Include\WinRT.au3" #include "Include\Classes\Microsoft.UI.Dispatching.DispatcherQueueController.au3" #include "Include\Classes\Microsoft.UI.Dispatching.DispatcherQueue.au3" #include "Include\Classes\Microsoft.UI.Windowing.Appwindow.au3" #include "Include\Classes\Microsoft.UI.Windowing.AppWindowPresenter.au3" #include "Include\Classes\Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource.au3" #include "Include\Classes\Microsoft.UI.Content.DesktopChildSiteBridge.au3" #include "Include\Classes\Microsoft.UI.Content.DesktopSiteBridge.au3" #include "Include\Classes\Microsoft.UI.Xaml.Controls.Button.au3" #include "Include\Classes\Microsoft.UI.Xaml.Controls.Primitives.ButtonBase.au3" #include "Include\Classes\Microsoft.UI.Xaml.Controls.ContentControl.au3" #include "Include\Classes\Windows.Foundation.PropertyValue.au3" #include "Include\Classes\Microsoft.UI.Xaml.RoutedEventArgs.au3" Global $__g_hDllUser32 = DllOpen("user32.dll") Global Const $tagMSG = "struct;hwnd hwnd;uint message;wparam wparam;lparam lparam;dword time;" & $tagPOINT & ";dword lprivate;endstruct" Global $hBootStrapDll = DllOpen("Microsoft.WindowsAppRuntime.Bootstrap.dll") If $hBootStrapDll = -1 Then Exit ConsoleWrite("No bootstapper dll!" & @CRLF) _WinRT_Startup() ;From WindowsAppSDK-VersionInfo.h Global Const $WINDOWSAPPSDK_RELEASE_MAJORMINOR = 0x00010007 ;1.7 Global Const $WINDOWSAPPSDK_RELEASE_VERSION_TAG = "" Global Const $WINDOWSAPPSDK_RELEASE_CHANNEL = "stable" Global Const $WINDOWSAPPSDK_RUNTIME_VERSION_DOTQUADSTRING = "7000.522.1444.0" Global Const $WINDOWSAPPSDK_RUNTIME_VERSION_UINT64 = 0x1B58020A05A40000 ;"7000.522.1444.0" Global Enum $MddBootstrapInitializeOptions_None, _ $MddBootstrapInitializeOptions_OnError_DebugBreak, _ $MddBootstrapInitializeOptions_OnError_DebugBreak_IfDebuggerAttached, _ $MddBootstrapInitializeOptions_OnError_FailFast, _ $MddBootstrapInitializeOptions_OnNoMatch_ShowUI, _ $MddBootstrapInitializeOptions_OnPackageIdentity_NOOP ;Main window proc. Global $hWndProc = DllCallbackRegister("guiProc", "long", "hwnd;uint;wparam;lparam") ;Setup the delegate for handling button pushes. Global $__hQueryInterface = DllCallbackRegister("__QueryInterface", "long", "ptr;ptr;ptr") Global $__hAddRef = DllCallbackRegister("__AddRef", "long", "ptr") Global $__hRelease = DllCallbackRegister("__Release", "long", "ptr") Global $tBtnClick_VTab = DllStructCreate("ptr pFunc[4]") $tBtnClick_VTab.pFunc(1) = DllCallbackGetPtr($__hQueryInterface) $tBtnClick_VTab.pFunc(2) = DllCallbackGetPtr($__hAddRef) $tBtnClick_VTab.pFunc(3) = DllCallbackGetPtr($__hRelease) Global $hBtnClick = DllCallbackRegister("BtnClick", "none", "ptr;ptr;ptr") $tBtnClick_VTab.pFunc(4) = DllCallbackGetPtr($hBtnClick) Global $tBtnClick = DllStructCreate("ptr pVTab;int iRefCnt") $tBtnClick.pVTab = DllStructGetPtr($tBtnClick_VTab) $tBtnClick.iRefCnt = 1 Global $pBtnClick = DllStructGetPtr($tBtnClick) _Main() Func _Main() ;Spin up runtime so _WinRT_GetActivationFactory works natively for Windows App objects. Local $aCall = DllCall($hBootStrapDll, "long", "MddBootstrapInitialize2", _ "uint", $WINDOWSAPPSDK_RELEASE_MAJORMINOR, _ "wstr", $WINDOWSAPPSDK_RELEASE_VERSION_TAG, _ "uint64", $WINDOWSAPPSDK_RUNTIME_VERSION_UINT64, _ "uint", $MddBootstrapInitializeOptions_None ) If $aCall[0] Then Return ConsoleWrite("Can't load the runtime" & @CRLF) ;Create dispacther for window messaging Local $pDispatchQCtrl_Fact = _WinRT_GetActivationFactory("Microsoft.UI.Dispatching.DispatcherQueueController", $sIID_IDispatcherQueueControllerStatics) Local $pDispatchQCtrl = IDispatcherQueueControllerStatics_CreateOnCurrentThread($pDispatchQCtrl_Fact) Local $pDispatchQ = IDispatcherQueueController_GetDispatcherQueue($pDispatchQCtrl) _WinRT_SwitchInterface($pDispatchQ, $sIID_IDispatcherQueue3) IDispatcherQueue3_EnsureSystemDispatcherQueue($pDispatchQ) ;Create the main window Local $pAppWindow_Fact = _WinRT_GetActivationFactory("Microsoft.UI.Windowing.AppWindow", $sIID_IAppWindowStatics) Local $pAppwindow = IAppWindowStatics_Create($pAppwindow_Fact) ;Attach the dispatcher, then flip back to the IAppWindow iface. _WinRT_SwitchInterface($pAppwindow, $sIID_IAppWindow3) IAppWindow3_AssociateWithDispatcherQueue($pAppwindow, $pDispatchQ) _WinRT_SwitchInterface($pAppwindow, $sIID_IAppWindow) ;Set the window proc for the gui. Local $iWinID = IAppWindow_GetID($pAppwindow) ;I believe the iWinID is technically different to a hwnd - but the values are the same. _WinAPI_SetWindowLong(HWnd($iWinID), $GWL_WNDPROC, DllCallbackGetPtr($hWndProc)) ;Set a title and presenter - which can be: Default, CompactOverlay, FullScreen or Overlapped IAppWindow_SetTitle($pAppwindow, "TestWin") IAppWindow_SetPresenter2($pAppwindow, $mAppWindowPresenterKind["Overlapped"]) ;Resize the window and show it. It does initally have size, but I'm just making it smaller. Local $tRect = DllStructCreate("long X;long Y;long Width;long Height") $tRect.X = (@DesktopWidth-400)/2 $tRect.Y = (@DesktopHeight-200)/2 $tRect.Width = 400 $tRect.Height = 200 IAppWindow_MoveAndResize($pAppwindow, $tRect) IAppWindow_Show($pAppwindow) ;Create a host for the XAML control and attach it to the window. Local $pInner Local $pDesktopWinXamlSrc_Fact = _WinRT_GetActivationFactory("Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource", $sIID_IDesktopWindowXamlSourceFactory) Local $pDesktopWinXamlSrc = IDesktopWindowXamlSourceFactory_CreateInstance($pDesktopWinXamlSrc_Fact, 0, $pInner) IDesktopWindowXamlSource_Initialize($pDesktopWinXamlSrc, $iWinID) ;Create a button and attach it to the XamlSource Local $pButton_Fact = _WinRT_GetActivationFactory("Microsoft.UI.Xaml.Controls.Button", $sIID_IButtonFactory) Local $pButton = IButtonFactory_CreateInstance($pButton_Fact, 0, $pInner) IDesktopWindowXamlSource_SetContent($pDesktopWinXamlSrc, $pButton) ;Set Button Content _WinRT_SwitchInterface($pButton, $sIID_IContentControl) Local $pProp_Fact = _WinRT_GetActivationFactory("Windows.Foundation.PropertyValue", $sIID_IPropertyValueStatics) Local $pProp = IPropertyValueStatics_CreateString($pProp_Fact, "Push Me") IContentControl_SetContent($pButton, $pProp) ;CreateButtonPushHdlr _WinRT_SwitchInterface($pButton, $sIID_IButtonBase) IButtonBase_AddHdlrClick($pButton, $pBtnClick) ;you can show/hide/enable/disable the container with this interface. Local $pBridge = IDesktopWindowXamlSource_GetSiteBridge($pDesktopWinXamlSrc) _WinRT_SwitchInterface($pBridge, $sIID_IDesktopSiteBridge) ;Resize/move the container because we can. (Button is aligned at the bottom of the container.) $tRect.X = 10 $tRect.Y = 10 $tRect.Width = 380 $tRect.Height = 180 IDesktopSiteBridge_MoveAndResize($pBridge, $tRect) ;~ ;Manual Msg Loop. To break out of the loop, call _WinUI3_PostQuitMessage(). ;~ Local $tMsg = DllStructCreate($tagMSG), $pMsg = DllStructGetPtr($tMsg) ;~ While _WinUI3_GetMsg($pMsg) > 0 ;~ _WinUI3_TranslateMessage($pMsg) ;~ _WinUI3_DispatchMessage($pMsg) ;~ Sleep(10) ;~ WEnd ;~ RunEventLoop just farms off messages while the dispatcher is alive.. IDispatcherQueue3_RunEventLoop($pDispatchQ) IDispatcherQueueController_ShutdownQueueAsync($pDispatchQCtrl) IClosable_Close(IUnknown_QueryInterface($pDesktopWinXamlSrc, $sIID_IClosable)) IAppWindow_Destroy($pAppwindow) IUnknown_Release($pAppwindow) IUnknown_Release($pAppwindow_Fact) IUnknown_Release($pDispatchQ) IUnknown_Release($pDispatchQCtrl) IUnknown_Release($pDispatchQCtrl_Fact) EndFunc _WinRT_Shutdown() DllClose($__g_hDllUser32) DllClose($hBootStrapDll) Func BtnClick($pThis, $pSender, $pArgs) #forceref $pThis, $pSender, $pArgs ;~ Local $pSrc = IRoutedEventArgs_GetOriginalSource($pArgs) ; $pSrc is the same as $pSender. MsgBox(0, "TestWin", "You are pushing my buttons.") EndFunc Func __QueryInterface($pThis, $pIID, $ppObj) Local $hResult = $S_OK If Not $ppObj Then $hResult = $E_POINTER ElseIf _WinAPI_StringFromGUID($pIID) = $sIID_IUnknown Then DllStructSetData(DllStructCreate("ptr", $ppObj), 1, $pThis) __AddRef($pThis) Else $hResult = $E_NOINTERFACE EndIf Return $hResult EndFunc Func __AddRef($pThis) Local $tThis = DllStructCreate("ptr pVTab;int iRefCnt", $pThis) $tThis.iRefCnt += 1 Return $tThis.iRefCnt EndFunc Func __Release($pThis) Local $tThis = DllStructCreate("ptr pVTab;int iRefCnt", $pThis) $tThis.iRefCnt -= 1 Return $tThis.iRefCnt EndFunc Func _WinRT_SwitchInterface(ByRef $pThis, $sIID) Local $pResult = IUnknown_QueryInterface($pThis, $sIID) If @error Then Return SetError(@error, @extended, $pResult) IUnknown_Release($pThis) $pThis = $pResult EndFunc Func _WinUI3_GetMsg(ByRef $pMsg, $hWnd = 0, $iMsgFilterMin = 0, $iMsgFilterMax = 0) Local $aCall = DllCall($__g_hDllUser32, "bool", "GetMessage", "ptr", $pMsg, "hwnd", $hWnd, "uint", $iMsgFilterMin, "uint", $iMsgFilterMax) If @error Then Return SetError(__WinRT_GetDllError(), 0, -1) Return $aCall[0] = 1 EndFunc Func _WinUI3_DispatchMessage($pMsg) Local $aCall = DllCall($__g_hDllUser32, "lresult", "DispatchMessage", "ptr", $pMsg) If @error Then Return SetError(__WinRT_GetDllError(), 0, -1) Return SetError($aCall[0], 0, ($aCall[0] = 0)) EndFunc Func _WinUI3_TranslateMessage($pMsg) Local $aCall = DllCall($__g_hDllUser32, "bool", "TranslateMessage", "ptr", $pMsg) If @error Then Return SetError(__WinRT_GetDllError(), 0, -1) Return $aCall[0] = 1 EndFunc Func _WinUI3_PostQuitMessage($iExitCode) DllCall($__g_hDllUser32, "none", "PostQuitMessage", "int", $iExitCode) If @error Then Return SetError(__WinRT_GetDllError(), 0, -1) EndFunc Func guiProc($hWnd, $iMsg, $wParam, $lParam) Local $iRet Switch $iMsg Case $WM_DESTROY _WinUI3_PostQuitMessage($ERROR_SUCCESS) $iRet = _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam) Case Else $iRet = _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam) EndSwitch Return $iRet EndFunc