2016-12-13 3 views
0

ToolTipと同様の情報を表示したい - 十分なカスタマイズができず、マウス全体がControl以上であることを示す必要があるため、ToolTipを使用できません。 。
Cursor.Sizeは32x32に設定されていますが、カーソルの表示部分は12x19ピクセルしかないので、表示される部分と表示したい情報の間に大きなギャップ(20x13ピクセル)があります他のWindowsのバージョンやマウスカーソルの設定によって異なる場合があります)。Windowsカーソルが表示されている部分のサイズ

私は、もっと大きなサイズのカーソル(クロック/疑問符、手などの矢印)があることを知っていますが、目に見える部分のサイズを計算する方法はありますか?
ToolTipがカーソルのすぐ下に表示されるため、何らかの方法があります。

これはToolTipが示されている方法です。
standard tooltip
これは私の情報が表示されている方法です。
my tooltip

+0

を呼び出します.Size'? –

+0

@ m.rogalski同じ結果を返します。 – Artholl

+0

ちょうど確信したかったです。 [この例](http://stackoverflow.com/questions/3509951/how-to-get-mouse-cursor-icon-vs-c)にチェックを入れ、最も右下のピクセルオフセットを見つけることができます。今すぐ簡単なアイデア。編集:間違ったリンク、固定 –

答えて

1

m.rogalskiのおかげで(this questionに私を指摘)とthis answerthis answerthis article私ができましたマウスカーソルの可視部分を数えるコードを作成します。管理されていないコードを使用しているので、最大の問題はメモリ管理であり、最後にすべてを解放することを願っています。私に知らせてください。
外部

using System; 
using System.Runtime.InteropServices; 

namespace MouseCursorHelper 
{ 
    /// <summary> 
    /// Source: https://www.codeproject.com/kb/cs/desktopcapturewithmouse.aspx 
    /// + DestroyIcon, DeleteObject 
    /// </summary> 
    class ExternalDlls 
    { 
     #region Class Variables 

     public const Int32 CURSOR_SHOWING = 0x00000001; 

     [StructLayout(LayoutKind.Sequential)] 
     public struct ICONINFO 
     { 
      public bool fIcon;   // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies 
      public Int32 xHotspot;  // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot 
      public Int32 yHotspot;  // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot 
      public IntPtr hbmMask;  // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon, 
      public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this 
     } 
     [StructLayout(LayoutKind.Sequential)] 
     public struct POINT 
     { 
      public Int32 x; 
      public Int32 y; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public struct CURSORINFO 
     { 
      public Int32 cbSize;  // Specifies the size, in bytes, of the structure. 
      public Int32 flags;   // Specifies the cursor state. This parameter can be one of the following values: 
      public IntPtr hCursor;   // Handle to the cursor. 
      public POINT ptScreenPos;  // A POINT structure that receives the screen coordinates of the cursor. 
     } 

     #endregion 

     #region Class Functions 

     [DllImport("user32.dll", EntryPoint = "GetCursorInfo")] 
     public static extern bool GetCursorInfo(out CURSORINFO pci); 

     [DllImport("user32.dll", EntryPoint = "CopyIcon")] 
     public static extern IntPtr CopyIcon(IntPtr hIcon); 

     [DllImport("user32.dll", EntryPoint = "GetIconInfo")] 
     public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo); 

     [DllImport("user32.dll", EntryPoint = "DestroyIcon")] 
     public static extern bool DestroyIcon(IntPtr hIcon); 

     [DllImport("gdi32.dll")] 
     public static extern bool DeleteObject(IntPtr hObject); 

     #endregion 
    } 
} 

サイズ計算が

その後、
using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 

namespace MouseCursorHelper 
{ 
    class CursorActualSize 
    { 
     public static Size GetActualSize() 
     { 
      Bitmap bmp; 
      IntPtr hicon; 
      ExternalDlls.CURSORINFO ci = new ExternalDlls.CURSORINFO(); 
      ExternalDlls.ICONINFO icInfo; 
      ci.cbSize = Marshal.SizeOf(ci); 
      if (ExternalDlls.GetCursorInfo(out ci)) 
      { 
       if (ci.flags == ExternalDlls.CURSOR_SHOWING) 
       { 
        hicon = ExternalDlls.CopyIcon(ci.hCursor); 
        if (ExternalDlls.GetIconInfo(hicon, out icInfo)) 
        { 
         bmp = Bitmap.FromHbitmap(icInfo.hbmMask); 

         var x = 0; 
         var y = 0; 

         for (var i = 0; i < bmp.Width; i++) 
         { 
          for (var j = 0; j < bmp.Height; j++) 
          { 
           var a = bmp.GetPixel(i, j); 

           if (a.R == 0 && a.G == 0 && a.B == 0) 
           { 
            if (i > x) 
            { 
             x = i; 
            } 

            if (j > y) 
            { 
             y = j; 
            } 
           } 
          } 
         } 

         bmp.Dispose(); 
         if (hicon != IntPtr.Zero) 
         { 
          ExternalDlls.DestroyIcon(hicon); 
         } 
         if (icInfo.hbmColor != IntPtr.Zero) 
         { 
          ExternalDlls.DeleteObject(icInfo.hbmColor); 
         } 
         if (icInfo.hbmMask != IntPtr.Zero) 
         { 
          ExternalDlls.DeleteObject(icInfo.hbmMask); 
         } 
         if (ci.hCursor != IntPtr.Zero) 
         { 
          ExternalDlls.DeleteObject(ci.hCursor); 
         } 

         return new Size(x, y); 
        } 

        if (hicon != IntPtr.Zero) 
        { 
         ExternalDlls.DestroyIcon(hicon); 
        } 
        if (icInfo.hbmColor != IntPtr.Zero) 
        { 
         ExternalDlls.DeleteObject(icInfo.hbmColor); 
        } 
        if (icInfo.hbmMask != IntPtr.Zero) 
        { 
         ExternalDlls.DeleteObject(icInfo.hbmMask); 
        } 
       } 
      } 

      if (ci.hCursor != IntPtr.Zero) 
      { 
       ExternalDlls.DeleteObject(ci.hCursor); 
      } 

      return new Size(0, 0); 
     } 
    } 
} 

そして単にたぶん代わりに `あなたがチェックすることができCursor.Size`` Cursor.Currentの

Size cursorSize = CursorActualSize.GetActualSize(); 
関連する問題