VC++/CLI 2005
windows Form Application で「アイドル処理」を行いたいと思っているのですが
そのようなイベント等を見つけきれずにいます。
どのような方法がありますでしょうか。教えていただきたいです。
お願いします。
ref class IdleRunner {
public:
void OnIdle(Object^ sender, EventArgs^ e) {
// アイドル時の処理
}
};
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// この2行追加。
IdleRunner idle;
Application::Idle += gcnew EventHandler(%idle,&IdleRunner::OnIdle);
// メイン ウィンドウを作成して、実行します
Application::Run(gcnew Form1());
return 0;
}
επιστημηさん、ありがとうございます。
IdleRunnerクラスを追加し、[プロジェクト名].cppに
IdleRunner idle;
Application::Idle += gcnew EventHandler(%idle,&IdleRunner::OnIdle);
の2行を追加することで目的の処理が行えました。ありがとうございます。
行えたのですが新たに疑問点が出てきたのでお答え願えないでしょうか。
アイドル処理を行った回数を数え、button1が押されるとその回数を
MessageBoxで表示させたいとおもっています。
そしてMessageBoxを閉じたあと、アイドル処理を新たに始める方法としてど
のようなものがあるでしょうか。
回数を数えるためにIdleRunnerにprivateメンバ[Counter]をつくり
//アイドル時の処理
this->Counter += 1;
としてカウントしているのですが、そこからどうbutton1のクリックイベント
につなげ、新たに始めさせるのか、というところです。
よろしくお願いします。
最初の不明点は無事行えましたので「解決」とさせていただきます。
FormからIdleRunnerクラスのCounterメンバの値をとれるようにする。
○delegateをつかう方法
public ref class IdleRunner
{
int counter;
public:
IdleRunner() : counter(0) {}
System::Void OnIdle(System::Object^ sender, System::EventArgs^ e)
{
++this->counter;
}
int GetCounter() { return this->counter; }
};
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
IdleRunner idle;
Application::Idle += gcnew EventHandler(%idle, &IdleRunner::OnIdle);
// メイン ウィンドウを作成して、実行します
Form1 frm;
frm.getcounter = gcnew Form1::GetCounterDelegate(%idle, &IdleRunner::GetCounter);
Application::Run(%frm);
return 0;
}
Form.h
public:
delegate int GetCounterDelegate();
GetCounterDelegate^ getcounter;
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (this->getcounter != nullptr)
{
MessageBox::Show(this->getcounter().ToString());
}
}
};
Blueさん、ありがとうございます。
ばっちり解決できました。
ただ、[delegate]は初めて知った言葉なので何がなんだか。
delegate int GetCounterDelegate(); //???
変数?関数? 勉強が必要ですね。
Form1だけがアイドルイベントを扱うので良いのであれば、
Form1.h
int counter;
public:
System::Void OnIdle(System::Object^ sender, System::EventArgs^ e)
{
++this->counter;
}
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show(this->counter.ToString());
}
XXX.cpp
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// メイン ウィンドウを作成して、実行します
Form1 frm;
Application::Idle += gcnew EventHandler(%frm, &Form1::OnIdle);
Application::Run(%frm);
return 0;
}
とすれば十分でしょう。
Blueさん、ありがとうございます。
おっしゃるとおり、Form1だけで扱うため、上記の方法を参考にさせていただきます。
大変参考になりました。ありがとうございました。
ツイート | ![]() |