/AvalonEdit/ICSharpCode.AvalonEdit/Utils/Win32.cs

http://github.com/icsharpcode/ILSpy · C# · 100 lines · 59 code · 9 blank · 32 comment · 10 complexity · 4990800ffd4d34095d8ac3011092b478 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Runtime.InteropServices;
  20. using System.Security;
  21. using System.Windows;
  22. using System.Windows.Interop;
  23. using System.Windows.Media;
  24. namespace ICSharpCode.AvalonEdit.Utils
  25. {
  26. /// <summary>
  27. /// Wrapper around Win32 functions.
  28. /// </summary>
  29. static class Win32
  30. {
  31. /// <summary>
  32. /// Gets the caret blink time.
  33. /// </summary>
  34. public static TimeSpan CaretBlinkTime {
  35. get { return TimeSpan.FromMilliseconds(SafeNativeMethods.GetCaretBlinkTime()); }
  36. }
  37. /// <summary>
  38. /// Creates an invisible Win32 caret for the specified Visual with the specified size (coordinates local to the owner visual).
  39. /// </summary>
  40. public static bool CreateCaret(Visual owner, Size size)
  41. {
  42. if (owner == null)
  43. throw new ArgumentNullException("owner");
  44. HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
  45. if (source != null) {
  46. Vector r = owner.PointToScreen(new Point(size.Width, size.Height)) - owner.PointToScreen(new Point(0, 0));
  47. return SafeNativeMethods.CreateCaret(source.Handle, IntPtr.Zero, (int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y));
  48. } else {
  49. return false;
  50. }
  51. }
  52. /// <summary>
  53. /// Sets the position of the caret previously created using <see cref="CreateCaret"/>. position is relative to the owner visual.
  54. /// </summary>
  55. public static bool SetCaretPosition(Visual owner, Point position)
  56. {
  57. if (owner == null)
  58. throw new ArgumentNullException("owner");
  59. HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
  60. if (source != null) {
  61. Point pointOnRootVisual = owner.TransformToAncestor(source.RootVisual).Transform(position);
  62. Point pointOnHwnd = pointOnRootVisual.TransformToDevice(source.RootVisual);
  63. return SafeNativeMethods.SetCaretPos((int)pointOnHwnd.X, (int)pointOnHwnd.Y);
  64. } else {
  65. return false;
  66. }
  67. }
  68. /// <summary>
  69. /// Destroys the caret previously created using <see cref="CreateCaret"/>.
  70. /// </summary>
  71. public static bool DestroyCaret()
  72. {
  73. return SafeNativeMethods.DestroyCaret();
  74. }
  75. [SuppressUnmanagedCodeSecurity]
  76. static class SafeNativeMethods
  77. {
  78. [DllImport("user32.dll")]
  79. public static extern int GetCaretBlinkTime();
  80. [DllImport("user32.dll")]
  81. [return: MarshalAs(UnmanagedType.Bool)]
  82. public static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
  83. [DllImport("user32.dll")]
  84. [return: MarshalAs(UnmanagedType.Bool)]
  85. public static extern bool SetCaretPos(int x, int y);
  86. [DllImport("user32.dll")]
  87. [return: MarshalAs(UnmanagedType.Bool)]
  88. public static extern bool DestroyCaret();
  89. }
  90. }
  91. }