首页 > 解决方案 > 如何在 GUI 中实现我的 while 循环以使用 Visual Studio 在 C/C++ 中保持按键

问题描述

我有在 CMD 上工作的代码,我可以在其中键入 1 或 2 来运行 C 脚本以无限单击或无限按 F。我正在尝试制作一个 GUI,但代码对我来说似乎真的很时髦和新,可能是因为它更基于 C++。gui 的目标是输入 1 或 2,然后按下按钮让 while 循环运行。

#include <windows.h>
#include <stdio.h>
#include <random>


void LeftClick();
namespace Project6 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^ label1;
    private: System::Windows::Forms::TextBox^ textBox1;
    private: System::Windows::Forms::Button^ button1;


    protected:

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(37, 63);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(197, 13);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Press 1 for mouse click or 2 for key click";
            this->label1->Click += gcnew System::EventHandler(this, &MyForm::label1_Click);
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(72, 79);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 20);
            this->textBox1->TabIndex = 1;
            this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(82, 105);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 2;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;

            // 
            // MyForm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
            this->ClientSize = System::Drawing::Size(271, 203);
            this->Controls->Add(this->button1);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            this->Name = L"MyForm";
            this->Text = L"Auto Press";
            this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
    }
    private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
    }
    private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
    }


    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

        String^ in = textBox1->Text;

        int ini = System::Convert::ToInt16(in);

        INPUT ip;
        while (TRUE)
        {   // Pause for 1 seconds.
            int output = rand() % 1000 + 1;
            Sleep(output);
            // Set up a generic keyboard event.
            ip.type = INPUT_KEYBOARD;
            ip.ki.wScan = 0; // hardware scan code for key
            ip.ki.time = 0;
            ip.ki.dwExtraInfo = 0;

            if (ini == 1)
            {
                ip.ki.wVk = 0x41;//F
            }
            if (ini == 2)
            {
                LeftClick();
            }
            ip.ki.dwFlags = 0; // 0 for key press
            SendInput(1, &ip, sizeof(INPUT));

            // Release the "A" key
            ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
            SendInput(1, &ip, sizeof(INPUT));
        }
    }
    };

}
void LeftClick()
{
    INPUT    Input = { 0 };
    // left down 
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    ::SendInput(1, &Input, sizeof(INPUT));

    // left up
    ::ZeroMemory(&Input, sizeof(INPUT));
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    ::SendInput(1, &Input, sizeof(INPUT));
}

错误1:

LNK2028:在函数“void __cdecl LeftClick(void )" (?LeftClick@@$$FYAXXZ)

错误2:

error LNK2019: unresolved external symbol "extern "C" unsigned int __stdcall SendInput(unsigned int,struct tagINPUT *,int)" (?SendInput@@$$J212YGIIPAUtagINPUT@@H@Z) referenced in function "void __cdecl LeftClick(void)" (?LeftClick@@$$FYAXXZ)

标签: user-interfacec++-clisendinput

解决方案


您需要将该功能导入您的项目:-

[DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint numberOfInputs, INPUT[] inputs, int sizeOfInputStructure);

将上述内容放入您的代码中,如下所示通过 user32.dll 中的 SendInput 发送密钥

作为一个单独的问题,将 while 循环(或任何其他类型的永远循环代码)放在控件的事件中是一个坏主意。

而是启动一个计时器(或其他在后台工作的东西),这样事件就不会被锁定在一个永久循环中。

锁定事件将使您的整个表单对几乎所有其他用户输入都没有响应。


推荐阅读