我有适用于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:无法解析的令牌(0A00028F)“外部” C“ unsigned int __stdcall SendInput(unsigned int,struct tagINPUT *,int)”(?SendInput @@ $$ J212YGIIPAUtagINPUT @@ H @ Z)在函数“ 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)
您需要将函数导入您的项目:-
[[DllImport(“ user32.dll”,SetLastError = true)]私有静态外部uint SendInput(uint numberOfInputs,INPUT []输入,int sizeOfInputStructure);
将以上内容放入您的代码中,如下所示Send keys through SendInput in user32.dll
作为一个单独的问题,将while循环(或其他任何形式的永久循环代码)放入控件事件中是一个坏主意。
相反,启动计时器(或其他在后台运行的计时器),以使事件不会在永久循环中锁定。
锁定该事件将使您的整个表单无法响应几乎所有其他用户的输入。