c#程序中的文本框名称访问不到窗体文本框

namespace keyrecord

{

    public partial class WDF1 : Form

    {

        public WDF1()

        {

            InitializeComponent();

            KeyRecord kr = new KeyRecord();

            kr.Start();

        }


        public void Form1_Load(object sender, EventArgs e) { }


        public class KeyRecord

        {

            public List<string> _Record = new List<string>();


            private const int WM_KEYDOWN = 0x100;

            private const int WM_KEYUP = 0x101;

            private const int WM_SYSKEYDOWN = 0x104;

            private const int WM_SYSKEYUP = 0x105;

            public event KeyEventHandler OnKeyDownEvent;

            public event KeyEventHandler OnKeyUpEvent;

            public event KeyPressEventHandler OnKeyPressEvent;

            static int hKeyboardHook = 0;

            public const int WH_KEYBOARD_LL = 13;

            HookProc KeyboardHookProcedure;

            [StructLayout(LayoutKind.Sequential)]

            public class KeyboardHookStruct

            {

                public int vkCode;

                public int scanCode;

                public int flags;

                public int time;

                public int dwExtraInfo;

            }

            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

            public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

            public static extern bool UnhookWindowsHookEx(int idHook);

            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

            public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);

            [DllImport("user32")]

            public static extern int GetKeyboardState(byte[] pbKeyState);

            public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

            public KeyRecord()

            {

                this.OnKeyPressEvent += new KeyPressEventHandler(KeyBordHook_OnKeyPressEvent);

                Start();

            }

            public void Start()

            {

                if (hKeyboardHook == 0)

                {

                    KeyboardHookProcedure = new HookProc(KeyboardHookProc);

                    Module m = Assembly.GetExecutingAssembly().GetModules()[0];

                    IntPtr itp = Marshal.GetHINSTANCE(m);

                    hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, itp, 0);

                    if (hKeyboardHook == 0) Stop();

                }

            }

            public void Stop()

            {

                bool retKeyboard = true;

                if (hKeyboardHook != 0)

                {

                    retKeyboard = UnhookWindowsHookEx(hKeyboardHook);

                    hKeyboardHook = 0;

                }

            }

            private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)

            {

                if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))

                {

                    KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));

                    if (OnKeyPressEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))

                    {

//RTB1是窗体上的文本框,这里访问不到>>   RTB1.AppendText("KeyCode:" + MyKeyboardHookStruct.vkCode + "| Time:" + DateTime.Now + "\r");

                    }

                }

                return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);

            }

            private void KeyBordHook_OnKeyPressEvent(object sender, KeyPressEventArgs e) { }

        }


    }

   

}


asp.net 浏览量194 回复量0 关注量0 悬赏金额1.00 分享
回答 2
知识点 0
关注者 0

写回答