Page 2 of 2

Re: Xaml? (v2.0-beta.2)

Posted: 24 Aug 2022, 00:52
by jNizM
Found it too after reading docs

https://microsoft.github.io/microsoft-ui-xaml/about.html
Note that support for Xaml Islands in WinUI 3 is coming next year.

Re: Xaml? (v2.0-beta.2)

Posted: 24 Aug 2022, 07:29
by kczx3
WinUI2 is far different because it was tied in with the OS. WinUI3 is decoupled from the OS and ships with the Windows App SDK. I'm not sure how that would play into using it with AHK. You'd have to ship the SDK or at least install it to use the controls. I have yet to figure out how to install the Microsoft Community Toolkit to use other controls such as DataGrid in Lexikos' script.

Re: Xaml? (v2.0-beta.2)

Posted: 24 Aug 2022, 09:09
by jNizM
I tried to rebuild a gui of mine (original: https://github.com/jNizM/HashCalc). But everything without functionality in the background.

Image
Code


I still need to figure out how to include the C# functions in the AHK script. e.G:
Spoiler


And I get a lot of error while testing
Spoiler

Re: Xaml? (v2.0-beta.2)

Posted: 24 Aug 2022, 12:52
by kczx3
@jNizM Using the base provided by Lexikos in the xaml.ahk example...

Code: Select all

#Include ../windows.ahk

xg := BasicXamlGui('+Resize')

; Xaml follows the user's dark mode preference by default, but the window frame
; is pure Win32, so we need to handle that unless we want black-on-white:
DarkModeIsActive() && SetDarkWindowFrame(xg.hwnd, true)

xg.Content := stk := Windows.UI.Xaml.Markup.XamlReader.Load("
    (
        <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Margin="10" Spacing="5" Orientation="Horizontal" VerticalAlignment="Top">
            <PasswordBox
                x:Name="passwordBoxWithRevealmode"
                Width="250"
                AutomationProperties.Name="Sample password box"
            />
            <CheckBox
                x:Name="revealModeCheckBox"
                Content="Show password"
                IsChecked="False"
            />
        </StackPanel>
    )"
)

xg["revealModeCheckBox"].add_click(togglePasswordReveal)

xg.Show("w600 h" ((stk.ActualHeight + (stk.Margin.Top * 2) + (stk.Padding.Top * 2)) * A_ScreenDPI / 96))

togglePasswordReveal(cb, *) {
    xg["passwordBoxWithRevealmode"].PasswordRevealMode := cb.IsChecked.Value ? "Visible" : "Hidden"
}

class BasicXamlGui extends Gui {
    __new(opt:='', title:=unset) {
        super.__new(opt ' -DPIScale', IsSet(title) ? title : A_ScriptName, this)

        this.OnEvent('Size', '_OnSize')

        static _ := (
            OnMessage(0x100, BasicXamlGui._OnKeyDown.Bind(BasicXamlGui)),
            OnMessage(0x102, BasicXamlGui._OnChar.Bind(BasicXamlGui))
        )

        this._dwxs := dwxs := Windows.UI.Xaml.Hosting.DesktopWindowXamlSource()

        ; IDesktopWindowXamlSourceNative is a traditional COM interface.
        dwxsn := ComObjQuery(dwxs, '{3cbcf1bf-2f76-4e9c-96ab-e84b37972554}')
        ComCall(AttachToWindow := 3, dwxsn, 'ptr', this.hwnd)
        ComCall(get_WindowHandle := 4, dwxsn, 'ptr*', &xwnd := 0)
        this._xwnd := xwnd
    }

    Content {
        get => this._dwxs.Content
        set => this._dwxs.Content := value
    }

    __Item[name] => this._dwxs.Content.FindName(name) ; FIXME: needs WinRT() to "down-cast" from UIElement to actual class

    ; reason: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.hosting.xamlsourcefocusnavigationreason?view=winrt-22621
    NavigateFocus(reason) =>
        this._dwxs.NavigateFocus(
            Windows.UI.Xaml.Hosting.XamlSourceFocusNavigationRequest(reason))

    _OnSize(minMax, width, height) {
        ; Must use SetWindowPos! Does not work with ControlMove+ControlShow!
        DllCall('SetWindowPos', 'ptr', this._xwnd, 'ptr', 0
            , 'int', 0, 'int', 0, 'int', width, 'int', height, 'int', 0x40)
    }

    static _OnKeyDown(wParam, lParam, nmsg, hwnd) {
        if !(wnd := GuiFromHwnd(hwnd, true)) || !(wnd is BasicXamlGui)
            return
        try
            native2 := ComObjQuery(wnd._dwxs, "{e3dcd8c7-3057-4692-99c3-7b7720afda31}")
        catch ; Older than Windows 10 v1903?
            return
        kmsg := Buffer(48, 0)
        NumPut('ptr', hwnd, 'ptr', nmsg, 'ptr', wParam, 'ptr', lParam
            , 'uint', A_EventInfo, kmsg)
        ; IDesktopWindowXamlSourceNative2::PreTranslateMessage
        ComCall(5, native2, 'ptr', kmsg, 'int*', &processed:=false)
        if processed
            return 0
    }

    static _OnChar(wParam, lParam, nmsg, hwnd) {
        if !(wnd := GuiFromHwnd(hwnd, true)) || !(wnd is BasicXamlGui)
            return
        ; Xaml islands (tested on 10.0.22000) do not respond to WM_GETDLGCODE
        ; correctly, so WM_CHAR messages are consumed by IsDialogMessage() and
        ; not dispatched to the island unless we do it directly.
        if WinGetClass(hwnd) = 'Windows.UI.Input.InputSite.WindowClass'
            return SendMessage(nmsg, wParam, lParam, hwnd)
    }
}

DarkModeIsActive() {
    return isColorLight(Windows.UI.ViewManagement.UISettings().GetColorValue('Foreground'))
    ; Algorithm from https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes
    isColorLight(clr) =>
        ((5 * clr.G) + (2 * clr.R) + clr.B) > (8 * 128)
}

SetDarkWindowFrame(hwnd, dark) {
    if VerCompare(A_OSVersion, "10.0.17763") >= 0 {
        attr := 19
        if VerCompare(A_OSVersion, "10.0.18985") >= 0
            attr := 20 ; DWMWA_USE_IMMERSIVE_DARK_MODE is officially defined for 10.0.22000 as 20.
        DllCall("dwmapi\DwmSetWindowAttribute", "ptr", hwnd, "int", attr, "int*", dark, "int", 4)
    }
}


Re: Xaml? (v2.0-beta.2)

Posted: 25 Aug 2022, 02:34
by jNizM
Useful Playgrounds:
WinUI 2 Gallery (Microsoft Store)
WinUI 3 Gallery (Microsoft Store)
Windows Community Toolkit Sample App (Microsoft Store)
XAML Studio (Microsoft Store)

Re: Xaml? (v2.0-beta.2)

Posted: 02 Jan 2024, 03:58
by xMaxrayx
Hi, thanks I love xaml in WPF ,is there a way to build GUI or just Msgbox() in Xaml looking and not Winforms without need to do complicated code?

Re: Xaml? (v2.0-beta.2)

Posted: 15 Apr 2024, 19:17
by xMaxrayx
sorry but where I can find this windows.ahk #Include ../windows.ahk?

Re: Xaml? (v2.0-beta.2)

Posted: 15 Apr 2024, 19:36
by xMaxrayx
xMaxrayx wrote:
15 Apr 2024, 19:17
sorry but where I can find this windows.ahk #Include ../windows.ahk?
ok I found it here
https://github.com/Lexikos/winrt.ahk