aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2019-04-09 01:47:48 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2019-04-09 01:47:48 +0300
commit080f1697e97e13461ec6df4d31c8924d01257a1b (patch)
treeb1fe0285de7bc9bc52e9e2195e66fe022bf8f5b3 /Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs
parent1608e69a417bc5e40a607c3958c4a60f19f66f1a (diff)
downloadTango-080f1697e97e13461ec6df4d31c8924d01257a1b.tar.gz
Tango-080f1697e97e13461ec6df4d31c8924d01257a1b.zip
MERGE
Diffstat (limited to 'Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs')
-rw-r--r--Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs85
1 files changed, 85 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs
new file mode 100644
index 000000000..421c35c5a
--- /dev/null
+++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Utils/Win32.cs
@@ -0,0 +1,85 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
+// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
+
+using System;
+using System.Runtime.InteropServices;
+using System.Security;
+using System.Windows;
+using System.Windows.Interop;
+using System.Windows.Media;
+
+namespace Tango.Scripting.Editors.Utils
+{
+ /// <summary>
+ /// Wrapper around Win32 functions.
+ /// </summary>
+ static class Win32
+ {
+ /// <summary>
+ /// Gets the caret blink time.
+ /// </summary>
+ public static TimeSpan CaretBlinkTime {
+ get { return TimeSpan.FromMilliseconds(SafeNativeMethods.GetCaretBlinkTime()); }
+ }
+
+ /// <summary>
+ /// Creates an invisible Win32 caret for the specified Visual with the specified size (coordinates local to the owner visual).
+ /// </summary>
+ public static bool CreateCaret(Visual owner, Size size)
+ {
+ if (owner == null)
+ throw new ArgumentNullException("owner");
+ HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
+ if (source != null) {
+ Vector r = owner.PointToScreen(new Point(size.Width, size.Height)) - owner.PointToScreen(new Point(0, 0));
+ return SafeNativeMethods.CreateCaret(source.Handle, IntPtr.Zero, (int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y));
+ } else {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Sets the position of the caret previously created using <see cref="CreateCaret"/>. position is relative to the owner visual.
+ /// </summary>
+ public static bool SetCaretPosition(Visual owner, Point position)
+ {
+ if (owner == null)
+ throw new ArgumentNullException("owner");
+ HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
+ if (source != null) {
+ Point pointOnRootVisual = owner.TransformToAncestor(source.RootVisual).Transform(position);
+ Point pointOnHwnd = pointOnRootVisual.TransformToDevice(source.RootVisual);
+ return SafeNativeMethods.SetCaretPos((int)pointOnHwnd.X, (int)pointOnHwnd.Y);
+ } else {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Destroys the caret previously created using <see cref="CreateCaret"/>.
+ /// </summary>
+ public static bool DestroyCaret()
+ {
+ return SafeNativeMethods.DestroyCaret();
+ }
+
+ [SuppressUnmanagedCodeSecurity]
+ static class SafeNativeMethods
+ {
+ [DllImport("user32.dll")]
+ public static extern int GetCaretBlinkTime();
+
+ [DllImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
+
+ [DllImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool SetCaretPos(int x, int y);
+
+ [DllImport("user32.dll")]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public static extern bool DestroyCaret();
+ }
+ }
+}