PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 19using System;
 20using System.Runtime.InteropServices;
 21using System.Security;
 22using System.Windows;
 23using System.Windows.Interop;
 24using System.Windows.Media;
 25
 26namespace ICSharpCode.AvalonEdit.Utils
 27{
 28	/// <summary>
 29	/// Wrapper around Win32 functions.
 30	/// </summary>
 31	static class Win32
 32	{
 33		/// <summary>
 34		/// Gets the caret blink time.
 35		/// </summary>
 36		public static TimeSpan CaretBlinkTime {
 37			get { return TimeSpan.FromMilliseconds(SafeNativeMethods.GetCaretBlinkTime()); }
 38		}
 39		
 40		/// <summary>
 41		/// Creates an invisible Win32 caret for the specified Visual with the specified size (coordinates local to the owner visual).
 42		/// </summary>
 43		public static bool CreateCaret(Visual owner, Size size)
 44		{
 45			if (owner == null)
 46				throw new ArgumentNullException("owner");
 47			HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
 48			if (source != null) {
 49				Vector r = owner.PointToScreen(new Point(size.Width, size.Height)) - owner.PointToScreen(new Point(0, 0));
 50				return SafeNativeMethods.CreateCaret(source.Handle, IntPtr.Zero, (int)Math.Ceiling(r.X), (int)Math.Ceiling(r.Y));
 51			} else {
 52				return false;
 53			}
 54		}
 55		
 56		/// <summary>
 57		/// Sets the position of the caret previously created using <see cref="CreateCaret"/>. position is relative to the owner visual.
 58		/// </summary>
 59		public static bool SetCaretPosition(Visual owner, Point position)
 60		{
 61			if (owner == null)
 62				throw new ArgumentNullException("owner");
 63			HwndSource source = PresentationSource.FromVisual(owner) as HwndSource;
 64			if (source != null) {
 65				Point pointOnRootVisual = owner.TransformToAncestor(source.RootVisual).Transform(position);
 66				Point pointOnHwnd = pointOnRootVisual.TransformToDevice(source.RootVisual);
 67				return SafeNativeMethods.SetCaretPos((int)pointOnHwnd.X, (int)pointOnHwnd.Y);
 68			} else {
 69				return false;
 70			}
 71		}
 72		
 73		/// <summary>
 74		/// Destroys the caret previously created using <see cref="CreateCaret"/>.
 75		/// </summary>
 76		public static bool DestroyCaret()
 77		{
 78			return SafeNativeMethods.DestroyCaret();
 79		}
 80		
 81		[SuppressUnmanagedCodeSecurity]
 82		static class SafeNativeMethods
 83		{
 84			[DllImport("user32.dll")]
 85			public static extern int GetCaretBlinkTime();
 86			
 87			[DllImport("user32.dll")]
 88			[return: MarshalAs(UnmanagedType.Bool)]
 89			public static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
 90			
 91			[DllImport("user32.dll")]
 92			[return: MarshalAs(UnmanagedType.Bool)]
 93			public static extern bool SetCaretPos(int x, int y);
 94			
 95			[DllImport("user32.dll")]
 96			[return: MarshalAs(UnmanagedType.Bool)]
 97			public static extern bool DestroyCaret();
 98		}
 99	}
100}