如何从Form返回值?

时间:2015-09-24 20:19:16

标签: c# forms class

课程计划中的

必须检查当前按钮文字是"START"还是"STOP": 形式:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (button1.Text == "START")
        {
            button1.Text = "STOP";
            button1.BackColor = Color.FromArgb(255, 192, 192);
        }
        else if (button1.Text == "STOP")
        {
            button1.Text = "START";
            button1.BackColor = Color.FromArgb(192, 255, 192);
        }


    }
    public string buttonText { get { return button1.Text; } }
}

类:

class regKeys
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;


    public static void Main()
    {
            _hookID = SetHook(_proc);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            UnhookWindowsHookEx(_hookID);



    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        Form1 form = new Form1();
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN && form.buttonText=="STOP")
        {
            int vkCode = Marshal.ReadInt32(lParam);

            if (vkCode == 99)
            {
                SA key = new SA();
                key.checkTiming(vkCode);
            }
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }


    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
}

当它检查按钮文本是否为停止时:

if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN && form.buttonText=="STOP")

程序在这里停止。当我将form.buttonText=="STOP"更改为form.buttonText=="START"时,它可以正常工作,但当我再次点击该按钮时,它不会进行任何更改。

提前谢谢。

2 个答案:

答案 0 :(得分:1)

您正在针对不同的Form1实例进行测试。

执行以下操作以解决问题:

1)在Form1类范围中定义静态regKeys变量,如下所示:

private static Form1 m_Form;

2)在主方法中,创建表单实例,如下所示:

m_Form = new Form1();

而不是这样做:

Application.Run(new Form1());

这样做:

Application.Run(m_Form);

3)在HookCallback方法中,不要创建另一个表单,而是使用类范围中的静态实例,如下所示:

if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN && m_Form.buttonText=="STOP")

答案 1 :(得分:0)

我建议反思你的想法。 我的意思是,当您单击按钮时,您更改了regKey对象的属性。当对象需要信息时,它从内部获取。为什么?

因为按下按钮的方法很少,并且根据按钮标签的更改时间可能会插入计划问题。

如果按钮操作更改了regKey对象的内容,您将确保信息正确无误。