最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c# - WinUI 3 Custom Component Authoring - Stack Overflow

matteradmin5PV0评论

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)

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)

Share Improve this question edited Nov 19, 2024 at 2:57 Andrew KeepCoding 14.2k2 gold badges21 silver badges37 bronze badges asked Nov 18, 2024 at 21:16 user3765739user3765739 668 bronze badges 5
  • Can you be more specific about what you mean by "Drawing with GDI commands (not User Control)"? – Andrew KeepCoding Commented Nov 19, 2024 at 2:57
  • Using the Graphic commands like: DrawString, DrawPath, DrawLine instead of gluing other controls – user3765739 Commented Nov 19, 2024 at 18:18
  • About create a custom control in winui3, I suggest you could refer to the Doc: Build XAML controls with C# – Jeaninez - MSFT Commented Nov 20, 2024 at 2:08
  • @Jeaninez-MSFT, 20 years ago I followed a walkthrough from MSFT to authoring customs controls for Windows Forms. It was well documented including graphics, event overloading, packing and testing. That walkthrough was enough to create my first custom control and still is running. Now I need to update it to WinUI3, but I can't find that kind of documentation. – user3765739 Commented Nov 21, 2024 at 0:32
  • If you want to use the winui3 custom control in Windows App SDK app? If so , you could create a a C# component with WinUI 3 custom control, and then consume it from a C++ Windows App SDK app. Refer to the Doc:learn.microsoft/en-us/windows/apps/develop/platform/… – Jeaninez - MSFT Commented Nov 21, 2024 at 6:34
Add a comment  | 

1 Answer 1

Reset to default 0

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);
}
Post a comment

comment list (0)

  1. No comments so far