EmbedSample.Win.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Text;
  3. using Avalonia.Controls.Platform;
  4. using Avalonia.Platform;
  5. using ControlCatalog.Pages;
  6. namespace ControlCatalog.NetCore;
  7. public class EmbedSampleWin : INativeDemoControl
  8. {
  9. private const string RichText =
  10. @"{\rtf1\ansi\ansicpg1251\deff0\nouicompat\deflang1049{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
  11. {\colortbl ;\red255\green0\blue0;\red0\green77\blue187;\red0\green176\blue80;\red155\green0\blue211;\red247\green150\blue70;\red75\green172\blue198;}
  12. {\*\generator Riched20 6.3.9600}\viewkind4\uc1
  13. \pard\sa200\sl276\slmult1\f0\fs22\lang9 <PREFIX>I \i am\i0 a \cf1\b Rich Text \cf0\b0\fs24 control\cf2\fs28 !\cf3\fs32 !\cf4\fs36 !\cf1\fs40 !\cf5\fs44 !\cf6\fs48 !\cf0\fs44\par
  14. }";
  15. public IPlatformHandle CreateControl(bool isSecond, IPlatformHandle parent, Func<IPlatformHandle> createDefault)
  16. {
  17. WinApi.LoadLibrary("Msftedit.dll");
  18. var handle = WinApi.CreateWindowEx(0, "RICHEDIT50W",
  19. @"Rich Edit",
  20. 0x800000 | 0x10000000 | 0x40000000 | 0x800000 | 0x10000 | 0x0004, 0, 0, 1, 1, parent.Handle,
  21. IntPtr.Zero, WinApi.GetModuleHandle(null), IntPtr.Zero);
  22. var st = new WinApi.SETTEXTEX { Codepage = 65001, Flags = 0x00000008 };
  23. var text = RichText.Replace("<PREFIX>", isSecond ? "\\qr " : "");
  24. var bytes = Encoding.UTF8.GetBytes(text);
  25. WinApi.SendMessage(handle, 0x0400 + 97, ref st, bytes);
  26. return new Win32WindowControlHandle(handle, "HWND");
  27. }
  28. }
  29. internal class Win32WindowControlHandle : PlatformHandle, INativeControlHostDestroyableControlHandle
  30. {
  31. public Win32WindowControlHandle(IntPtr handle, string descriptor) : base(handle, descriptor)
  32. {
  33. }
  34. public void Destroy()
  35. {
  36. _ = WinApi.DestroyWindow(Handle);
  37. }
  38. }