如何在 GUI 中实现我的 while 循环以使用 Visual Studio 在 C/C++ 中保持按键

Posted

技术标签:

【中文标题】如何在 GUI 中实现我的 while 循环以使用 Visual Studio 在 C/C++ 中保持按键【英文标题】:How to implement my while loop into a GUI to keep pressing a key in C/C++ using visual studios 【发布时间】:2020-02-14 17:39:37 【问题描述】:

我有在 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”中引用的未解析令牌 (0A00028F)“extern "C" unsigned int __stdcall SendInput(unsigned int,struct tagINPUT *,int)" (?SendInput@@$$J212YGIIPAUtagINPUT@@H@Z) 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)

【问题讨论】:

不相关:您可能会发现“输出”选项卡的完整构建输出中的错误消息(位于“错误列表”选项卡附近)在 Stack Overflow 帖子中效果更好,因为它是纯文本。作为额外的奖励,完整的构建输出通常包含额外的信息,您可以使用它来简化您的工作。 欢迎来到 Stack Overflow。请阅读the help pages,获取the SO tour,了解how to ask good questions,以及this question checklist。最后请学习如何创建minimal reproducible example 向我们展示,重点是minimal部分。 所以我发现 SendInput 给了我错误,但我不知道如何防止这种情况 【参考方案1】:

你需要将函数导入到你的项目中:-

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

将以上内容放入您的代码中,如下所示Send keys through SendInput in user32.dll

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

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

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

【讨论】:

【参考方案2】:

你需要这样的东西:

#pragma once

#include "Windows.h"
#pragma comment(lib, "user32.lib")

namespace .....

【讨论】:

以上是关于如何在 GUI 中实现我的 while 循环以使用 Visual Studio 在 C/C++ 中保持按键的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C 中实现我自己的基本 unix shell?

如何在单页移动 Web 应用程序中实现我自己的历史堆栈?

如何使用 matplotlib 在 while 循环中实时绘图?

循环#import 参考

消费者/生产者锁定 GUI 线程

如何在处理时更改 GUI