Search code examples
c#winui-3winui

WinUI 3 Custom Component Authoring


How can I create a Custom Control to be used in WinUI 3?

It must respond to mouse events (click, hoovering, dragging, double click)

Drawing with GDI commands (not User Control)


Solution

  • You can create a user control, which can handle mouse events, and instead of using GDI commands you can use Win2d in it.

    For example:

    <UserControl
      xmlns:win2d="using:Microsoft.Graphics.Canvas.UI.Xaml" ...>
      <win2d:CanvasControl Draw="CanvasControl_Draw" />
    </UserControl>
    
    private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
    {
        args.DrawingSession.DrawText("Hi!", 300, 300, Colors.SkyBlue);
    }