Programm mit Button starten (?)
-
LaKo15 schrieb:
Ja, ich weiß. Habe gelesen, dass andere Schwierigkeiten haben wenn sie ihre Prgramme auf anderen PCs laufen lassen!
Das hat damit aber nichts zu tun...
-
Nicht? Womit dann? Diesen Link kopiert er vlgw. häufig in Foren. Und zwar auch aus diesem Grund. (?)
-
"Der WinForms-Designer ist miserabel"
"C++/CLI ist primär als InterOp Sprache" man wird dazu verleitet nitroglyzer (c++) und rin (.Net) zu vermischen. Und wenn man nicht vorsichtig ist,
gibt es eine riesen Sauerei.
-
Ja, zur Kenntnis genommen!
Wie kann man es schaffen, dass das Programm (hier der Tastendruck) erst nach z.B. 10 Sekunden nach Öffnen der .exe-Datei (im Windows-Explorer) "greift", startet, anfängt die Funktion auszuführen?
Ich habe es mit
Sleep(10000);
versucht, undzwar an den unterschiedlochsten Stellen - komme aber nicht zum erwünschten Ergebnis.
-
Benutze die Klasse Timer aus System::Windows::Forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
-
Danke für deine Antwort!
In welche Datei (Form1.h oder Tastendruck-Simulation.cpp) füge ich was genau ein? Ich komme nicht ganz dahinter, sry!
-
In der Form1.
-
Ich habe es mal so eingetragen, aber ich bekomme einige Fehler beim Kompilieren angeziegt und der "Form1.h [Entwurf]" ist nicht mehr zu sehen. In die .cpp-Datei habe ich nichts Weiteres eingetragen!
#pragma once using namespace System; using namespace System::Threading; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace TastendruckSimulation { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public ref class Timer : public Component { public: Timer() // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Label^ label1; private: System::ComponentModel::IContainer^ components; protected: private: #pragma region Windows Form Designer generated code // ... // ... // ...
-
Mach einfach ein Member von Typ Timer.
Am besten ziehst Du einfach ein Timer aus der Toolbox auf deine Form!
-
Ich bekomme dann leider einen Fehler angezeigt und ich weiß nicht wie ich das beheben kann!
Fehler 1 error C2872: 'Timer': Mehrdeutiges Symbol
-
Tja, dann zeigmal den relevanten Code.
Edit: Probiers mal mit
System::Windows::Forms::Timer^ timer;
-
Ja, kein Problem!
.cpp-Datei:
// Tastendruck-Simulation.cpp: Hauptprojektdatei. #pragma comment(lib, "user32.lib") #include <windows.h> #include "Form1.h" #include "stdafx.h" using namespace TastendruckSimulation; void SetNumlock(BOOL bState) { BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState); if( (bState && !(keyState[VK_NUMPAD3] & 1)) || (!bState && (keyState[VK_NUMPAD3] & 1)) ) { Timer; // Simulate a key press keybd_event( VK_NUMPAD3, 0x63, KEYEVENTF_EXTENDEDKEY | 0, 0 ); // Simulate a key release keybd_event( VK_NUMPAD3, 0x63, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } int main() { SetNumlock( TRUE ); // Aktivieren visueller Effekte von Windows XP, bevor Steuerelemente erstellt werden Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Hauptfenster erstellen und ausführen Application::Run(gcnew Form1()); return 0; }
Form1.h:
#pragma once using namespace System; using namespace System::Threading; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace TastendruckSimulation { public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Timer^ timer1; private: System::ComponentModel::IContainer^ components; protected: private: #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->button1 = (gcnew System::Windows::Forms::Button()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->timer1 = (gcnew System::Windows::Forms::Timer(this->components)); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(202, 87); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(64, 21); this->button1->TabIndex = 0; this->button1->Text = L"Start"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::Form1_Load); // // textBox1 // this->textBox1->Location = System::Drawing::Point(37, 87); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(148, 20); this->textBox1->TabIndex = 1; // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Underline)), System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->Location = System::Drawing::Point(20, 9); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(250, 31); this->label1->TabIndex = 2; this->label1->Text = L"Tasten-Simulation"; // // timer1 // this->timer1->Interval = 3000; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::Color::Silver; this->ClientSize = System::Drawing::Size(290, 120); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Tasten-Simulation"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; }
-
Ja, und was ist jetzt das Problem?
-
Beim Kompilieren exakt dieses Quellcodes, zeigt er mir einen Fehler an. Der lautet wie folgt:
Fehler 1 error C2872: 'Timer': Mehrdeutiges Symbol
Ich kann mir das eigentlich nicht erklären...
-
Entferne
using namespace System::Threading;
-
Danke, es funkioniert!
-
Aber jetzt kommt das, wenn ich Timer(5000); [man muss doch einen Wert angeben, oder?] schriebe:
**error C2440: '<function-style-cast>': 'int' kann nicht in 'System::Windows::Forms::Timer' konvertiert werden
**Form1.h
#pragma once using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace TastendruckSimulation { public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Timer^ timer1; private: System::ComponentModel::IContainer^ components; protected: private: #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->button1 = (gcnew System::Windows::Forms::Button()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->timer1 = (gcnew System::Windows::Forms::Timer(this->components)); this->SuspendLayout(); // // button1 // this->button1->Cursor = System::Windows::Forms::Cursors::Hand; this->button1->DialogResult = System::Windows::Forms::DialogResult::Yes; this->button1->FlatAppearance->MouseOverBackColor = System::Drawing::Color::DeepSkyBlue; this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->button1->Location = System::Drawing::Point(206, 79); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(64, 33); this->button1->TabIndex = 1; this->button1->Text = L"Start"; this->button1->UseVisualStyleBackColor = true; // // textBox1 // this->textBox1->Location = System::Drawing::Point(43, 87); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(148, 20); this->textBox1->TabIndex = 1; // // label1 // this->label1->AutoSize = true; this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20, static_cast<System::Drawing::FontStyle>((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Underline)), System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0))); this->label1->Location = System::Drawing::Point(20, 9); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(250, 31); this->label1->TabIndex = 2; this->label1->Text = L"Tasten-Simulation"; // // timer1 // this->timer1->Enabled = true; this->timer1->Interval = 5000; this->timer1->Tick += gcnew System::EventHandler(this, &Form1::Form1_Load); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->BackColor = System::Drawing::Color::SlateGray; this->ClientSize = System::Drawing::Size(290, 120); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Tasten-Simulation"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } }; }
.cpp
// Tastendruck-Simulation.cpp: Hauptprojektdatei. #pragma comment(lib, "user32.lib") #include <windows.h> #include "Form1.h" #include "stdafx.h" using namespace TastendruckSimulation; void SetNumlock(BOOL bState) { Timer(keyb_event); BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState); if( (bState && !(keyState[VK_NUMPAD3] & 1)) || (!bState && (keyState[VK_NUMPAD3] & 1)) ) { Timer(5000); // Simulate a key press keybd_event( VK_NUMPAD3, 0x63, KEYEVENTF_EXTENDEDKEY | 0, 0 ); // Simulate a key release keybd_event( VK_NUMPAD3, 0x63, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } int main() { SetNumlock( TRUE ); // Aktivieren visueller Effekte von Windows XP, bevor Steuerelemente erstellt werden Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Hauptfenster erstellen und ausführen Application::Run(gcnew Form1()); return 0; }
-
Irgendwie hat das alles keinen Sinn...
Kauf Dir ein Buch.. oder noch besser kauf Dir ein Buch zu C# und mach das ganze mit C#...
-