C#划词

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Collections.Specialized;
namespace TPHYCDSS
{
    public enum HookType : int
    {
        WH_KEYBOARD_LL = 13,
        WH_MOUSE_LL = 14
    }
    public class SweepInfo
    {
        public static frmPageMain _frmMain = null;
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        protected static extern int UnhookWindowsHookEx(IntPtr hhook);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        protected static extern bool GetWindowInfo(IntPtr hwnd, ref tagWindowInfo pwi);
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string lpModuleName);
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, Int32 dwFlags, Int32 dwExtraInfo);
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out CursorPostion _cursor);
        [DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(CursorPostion _cursor);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        protected static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
        private const int KEYEVENTF_KEYUP = 2;
        private static IntPtr hHook = IntPtr.Zero;
        private static IntPtr kHook = IntPtr.Zero;
        public static IntPtr hwnd;
        public static HookProc MouseHookProcedure;
        public static HookProc KeyBoardHookProcedure;
        public static int[] mouseState = new int[3];
        public static bool _IsCopy = false;
        public static object textControl = null;
        public static void Hook_Start(object txt,object form1)
        {
            // 安装键盘钩子
            if (hHook.ToInt64() == 0)
            {
                MouseHookProcedure = new HookProc(MouseHookProc);
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
                //hHook = SetWindowsHookEx(HookType.WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                //kHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
                textControl = txt;
                pForm = form1 as Form;
            }
        }
        public static void FontClose()
        {
            if (hHook.ToInt32() != 0)
            {
                UnhookWindowsHookEx(hHook);
                hHook = IntPtr.Zero;
            }
        }
        public static void FontOpen()
        {
            IntPtr model = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
            hHook = SetWindowsHookEx(HookType.WH_MOUSE_LL, MouseHookProcedure, model, 0);
        }
        public static void HotKeyOpen()
        {
            IntPtr model = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
            kHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, KeyBoardHookProcedure, model, 0);
        }
        public static void HotKeyClose()
        {
            if (kHook.ToInt32() != 0)
            {
                UnhookWindowsHookEx(kHook);
                kHook = IntPtr.Zero;
            }
        }
        //public static void Hook_Clear()
        //{
        //    int flag = 1;
        //    if (hHook.ToInt64() != 0)
        //    {
        //        flag = UnhookWindowsHookEx(hHook);
        //        hHook = IntPtr.Zero;
        //    }
        //    if (flag == 0)
        //    {
        //        throw new Exception("UnhookWindowsHookEx failed.");
        //    }
        //}
        static IntPtr _pos;
        static IntPtr _form;
        static tagWindowInfo downInfo = new tagWindowInfo();
        static tagWindowInfo upInfo = new tagWindowInfo();
        public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            CursorPostion _cursor = new CursorPostion();
            GetCursorPos(out _cursor);
            _pos = WindowFromPoint(_cursor);
            switch (wParam.ToInt32())
            {
                case 512:
                    if (mouseState[0] == 513)
                        mouseState[1] = 512;
                    else
                        mouseState[1] = 0;
                    break;
                case 513:
                    mouseState[0] = 513;
                    GetWindowInfo(_pos, ref downInfo);
                    //Console.WriteLine(downInfo.rcWindow.ToString());
                    break;
                case 514:
                    if (mouseState[0] == 513 && mouseState[1] == 512)
                        mouseState[2] = 514;
                    else
                        mouseState[0] = mouseState[1] = mouseState[2] = 0;
                    GetWindowInfo(_pos, ref upInfo);
                    //Console.WriteLine(upInfo.rcWindow.ToString());
                    break;
                default:
                    break;
            }
            //if (wParam.ToInt32() == 513)
            //{
            //    CursorPostion _cursor = new CursorPostion();
            //    GetCursorPos(out _cursor);
            //    _pos = WindowFromPoint(_cursor);
            //    //_form = _flag.panel1.Handle;
            //    //if (_pos.ToInt32() != _form.ToInt32())
            //    //{
            //    //    _flag.Hide();
            //    //    if (_flag.menu != null)
            //    //    {
            //    //        _flag.menu.Hide();
            //    //        _txtGjz2.Text = "";
            //    //    }
            //    //}
            //}
            if (mouseState[0] == 513 && mouseState[1] == 512 && mouseState[2] == 514&&upInfo.rcWindow.ToString()==downInfo.rcWindow.ToString())
            {
                _IsCopy = true;
                mouseState[0] = mouseState[1] = mouseState[2] = 0;
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(GetMessage));
                try
                { }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    t.SetApartmentState( System.Threading.ApartmentState.STA);// = System.Threading.ApartmentState.STA;
                    t.Start(t);
                }
            }
            return 0;
        }
        static string clipText = "";
        static StringCollection clipFileList = null;
        static Image clipImage = null;
        public static void GetMessage(object o)
        {
            if (_IsCopy)
            {
                clipText = Clipboard.GetText();
                clipFileList = Clipboard.GetFileDropList();
                clipImage = Clipboard.GetImage();
                try
                {
                    Clipboard.SetText(" ");
                }
                catch
                { }
                keybd_event((byte)162, 0, 0, 0);                   //按下ctrl
                keybd_event((byte)Keys.C, 0, 0, 0);                //按下C
                keybd_event((byte)162, 0, KEYEVENTF_KEYUP, 0);     //松开ctrl
                keybd_event((byte)Keys.C, 0, KEYEVENTF_KEYUP, 0);  //松开C
                _IsCopy = false;
                System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(GetMessage1));
                try
                {
                }
                catch (Exception)
                {
                }
                finally
                {
                    t.SetApartmentState(System.Threading.ApartmentState.STA);// = System.Threading.ApartmentState.STA;
                    System.Threading.Thread.Sleep(100);
                    t.Start(t);
                }
            }
        }
        public static void GetMessage1(object o)
        {
            string strCopy = Clipboard.GetText();
            try
            {
                if (clipText != "")
                {
                    Clipboard.SetText(clipText);
                }
                else if (clipFileList != null && clipFileList.Count > 0)
                {
                    Clipboard.SetFileDropList(clipFileList);
                }
                else if (clipImage != null)
                {
                    Clipboard.SetImage(clipImage);
                }
            }
            catch
            { }
            finally
            {
                clipText = "";
                clipFileList = null;
                clipImage = null;
            }
            if (strCopy.Trim() != "")
            {
                pFunCopy pCopy = new pFunCopy(GetText1);
                //while (pForm.IsHandleCreated)
                //{
                pForm.Invoke(pCopy, strCopy);
            }
            //}
        }
        static Form pForm = null;
        delegate void pFunCopy(string str);
        //static string param = "";
        static void GetText1(string str)
        {
            ((TextBox)textControl).Text = "";
            ((TextBox)textControl).Text = str;
        }
        /// <summary>
        ///  错误信息输出
        /// </summary>
        /// <param name="strErrorSql">发生错误的Sql</param>
        /// <param name="strErrorMsg">错误信息</param>
        public static void ErrorPrint(string strErrorSql, string strErrorMsg)
        {
            string strFilePath = Environment.CurrentDirectory + "\\Errorinf.txt";
            StreamWriter sw = null;
            try
            {
                if (!File.Exists(strFilePath))
                {
                    sw = File.CreateText(strFilePath);
                    sw.Close();
                }
                byte[] bytes = File.ReadAllBytes(strFilePath);
                if (bytes.Length >= (500 * 1024))
                {
                    File.WriteAllText(strFilePath, "");
                }
                sw = File.AppendText(strFilePath);
                if (strErrorSql.Trim().Length > 0)
                {
                    sw.WriteLine("发生错误的Sql语句或存储过程名:" + strErrorSql + "\r\n");
                }
                sw.WriteLine("错误信息:" + strErrorMsg + "\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
        }
        static int ScreenWidth = SystemInformation.PrimaryMonitorMaximizedWindowSize.Width;   //屏幕的宽度
        static int ScreenHeight = SystemInformation.PrimaryMonitorMaximizedWindowSize.Height; //屏幕的高度
        private static bool bl = true;
        public static int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            KeyBoardHookStruct input = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
            if (input.vkCode == (int)Keys.S && ((int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Shift))
            {
                return 1;
            }
            if(input.vkCode == (int)Keys.LControlKey)
            {
                bl = false;
            }
            if (wParam.ToInt32() != 256)//表示无论遇到任何键弹出都释放变量
                bl = true;
            if(!bl)
            {
                if (input.vkCode == (int)Keys.W)
                {
                    bl = true;
                    if (_frmRight != null)
                    {
                        if (_frmRight.cbHotKey.Checked)
                        {
                            if (_frmRight.tipMenu == null)
                            {
                                frmTipMenus = new frmTipMenu();
                                _frmRight.tipMenu = frmTipMenus;
_frmRight.tipMenu.Location = new Point((ScreenWidth - _frmRight.tipMenu.Width) / 2, (ScreenHeight - _frmRight.tipMenu.Height) / 2);
                            }
                            _frmRight.tipMenu.Show();
                        }
                    }
                }
                if (input.vkCode == (int)Keys.F)
                {
                    bl = true;
                }
                if (input.vkCode == (int)Keys.H)
                {
                    bl = true;
                }
                //bl = true;               
            }
            return CallNextHookEx(hHook,nCode,wParam,lParam);
        }
        public static string SplitStrUrl(string url,string indexStr)
        {
            string str = url.Substring(url.IndexOf(indexStr) + indexStr.Length, url.Length - (url.IndexOf(indexStr) + indexStr.Length));
            if (str.Contains('='))
            {
                str = str.Substring(0, str.IndexOf('='));
            }
            return str;
        }
        public static frmTipMenu frmTipMenus = null;
        //public static frmTipInfo TipInfo = null;
        public static frmMain _frmRight = null;
        public static frmPageMain PageMain = null;
        public static frmDiagInfer DiagInfer = null;
        public static frmCCSSTip ccssTip = null;
        public static frmLittleLogin littleLogin = null;
        public static frmEmInfo emfrm = null;
        public static frmProfession proc = null;
        public static frmErrorMsg errorMsg = null;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct KeyBoardHookStruct
    {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }
    public struct CursorPostion
    {
        public int _X;
        public int _Y;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct tagWindowInfo
    {
        public uint cbSize;
        public tagRect rcWindow;
        public tagRect rcClient;
        public uint dwExStyle;
        public uint dwWindowStatus;
        public uint cxWindowBorders;
        public uint cyWindowBordres;
        public ushort atomWindowType;
        public ushort wCreatorVersion;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct tagRect
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
        public override string ToString()
        {
            return left.ToString() + ":" + top.ToString() + ":" + right.ToString() + ":" + bottom.ToString();
        }
    }

}

 

拷贝 ,到一个类中 ,直接调用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值