大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > C#技巧 > 常用Api代码

常用Api代码

关键词:常用Api代码  阅读(693) 赞(15)

[摘要]本文是对常用Api代码的讲解,对学习C#编程技术有所帮助,与大家分享。

     #region 隐藏系统滚动条
     protected override void WndProc(ref System.Windows.Forms.Message m)
     {
       ShowScrollBar(this.Handle, 3, false);//0:horizontal,1:vertical,3:both
       base.WndProc(ref m);
     }

     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     private static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow);
     #endregion

     #region 得到光标在屏幕上的位置
    [DllImport("user32")]
     public static extern bool GetCaretPos(out Point lpPoint);
     [DllImport("user32.dll")]
     private static extern IntPtr GetForegroundWindow();
     [DllImport("user32.dll")]
     private static extern IntPtr GetFocus();
     [DllImport("user32.dll")]
     private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);
     [DllImport("user32.dll")]
     private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
     [DllImport("kernel32.dll")]
     private static extern IntPtr GetCurrentThreadId();
     [DllImport("user32.dll")]
     private static extern void ClientToScreen(IntPtr hWnd, ref Point p);
 
     private Point CaretPos()
     {
         IntPtr ptr = GetForegroundWindow();
         Point p = new Point();
 
         //得到Caret在屏幕上的位置   
       if (ptr.ToInt32() != 0)
         {
             IntPtr targetThreadID = GetWindowThreadProcessId(ptr, IntPtr.Zero);
             IntPtr localThreadID = GetCurrentThreadId();
 
             if (localThreadID != targetThreadID)
             {
                 AttachThreadInput(localThreadID, targetThreadID, 1);
                 ptr = GetFocus();
                 if (ptr.ToInt32() != 0)
                 {
                     GetCaretPos(out   p);
                     ClientToScreen(ptr, ref   p);
                 }
                 AttachThreadInput(localThreadID, targetThreadID, 0);
             }
         }
         return p;
     }
     #endregion


相关评论