公開原始碼(本post內)。 但不包含 StatPlusAPI.h/StatPlusAPI.cpp 的部分,也就是以下函式:
ApplicationSetTopMost();
WindowSetTopMost();
ApplicationClearTopMost();
WindowClearTopMost();
這些函式的作法在本討論區應該可以找到。 ■testTimeUpUnit.h
//--------------------------------------------------------------------------- #ifndef testTimeUpUnitH
#define testTimeUpUnitH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
TPanel *Panel1;
TButton *Button1;
TLabel *Label1;
TEdit *EditPassword;
TRadioGroup *TimeList;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
■testTimeUpUnit.cpp
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "testTimeUpUnit.h"
#include "StatPlusAPI.h"
#include "StatPlusTimeUpUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
for(int i=5;i<=200;i =5)
TimeList->Items->AddObject(String(i) String("分鐘"),(TObject*)i);
TimeList->ItemIndex=5;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(MessageBox(NULL,"確定要開始了嗎?\n"
"結束時如果忘記密碼可是很難回到正常畫面喔...",
"真的確定嗎?",
MB_OKCANCEL)!=IDOK) return;
FormStatPlusTimeUp->Password=EditPassword->Text;
Timer1->Interval=60000*(int)TimeList->Items->Objects[TimeList->ItemIndex];
Timer1->Enabled=true;
this->Hide();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled=false;
Application->BringToFront();
ApplicationSetTopMost();
FormStatPlusTimeUp->Show();
FormStatPlusTimeUp->SetFocus();
WindowSetTopMost(FormStatPlusTimeUp->Handle);
}
//---------------------------------------------------------------------------
■StatPlusTimeUpUnit.h
//--------------------------------------------------------------------------- #ifndef StatPlusTimeUpUnitH
#define StatPlusTimeUpUnitH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include
//---------------------------------------------------------------------------
class TFormStatPlusTimeUp : public TForm
{
__published: // IDE-managed Components
TLabel *Label;
TTimer *Timer1;
TTimer *Timer2;
void __fastcall FormKeyPress(TObject *Sender, char &Key);
void __fastcall Timer1Timer(TObject *Sender);
void __fastcall FormShow(TObject *Sender);
void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
void __fastcall Timer2Timer(TObject *Sender);
private: // User declarations
char KeyString[256];
unsigned char KeyCount;
public: // User declarations
__fastcall TFormStatPlusTimeUp(TComponent* Owner);
AnsiString DefaultMessage;
AnsiString Password;
POINT LastMousePos;
bool CanCloseNow;
};
//---------------------------------------------------------------------------
extern PACKAGE TFormStatPlusTimeUp *FormStatPlusTimeUp;
//---------------------------------------------------------------------------
#endif
■StatPlusTimeUpUnit.cpp
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "StatPlusTimeUpUnit.h"
#include "StatPlusAPI.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormStatPlusTimeUp *FormStatPlusTimeUp;
//---------------------------------------------------------------------------
__fastcall TFormStatPlusTimeUp::TFormStatPlusTimeUp(TComponent* Owner)
: TForm(Owner)
{
this->DoubleBuffered=true;
DefaultMessage="時間到了! 換人了!\n\nㄕˊ ㄐㄧㄢ ㄉㄠˋ ˙ㄌㄜ!\n\nㄏㄨㄢˋ ㄖㄣˊ ˙ㄌㄜ!\n\n";
this->Label->Caption=DefaultMessage;
KeyCount=0;
CanCloseNow=true;
}
//---------------------------------------------------------------------------
void __fastcall TFormStatPlusTimeUp::FormKeyPress(TObject *Sender,
char &Key)
{
if(Key==0x1B) // Esc
{
KeyCount=0;
this->Label->Caption=DefaultMessage;
}
else if(Key==0x08) // BackSpace
{
if(KeyCount!=0) KeyCount--;
this->Label->Caption=DefaultMessage AnsiString::StringOfChar('*', KeyCount);
}
else if(Key==0x0D) // Enter
{
if(Password==AnsiString(KeyString,KeyCount))
{
Timer2Timer(Sender); // Close
}
KeyCount=0;
this->Label->Caption=DefaultMessage;
}
else // 一般字元
{
KeyString[KeyCount]=Key;
KeyCount ;
this->Label->Caption=DefaultMessage AnsiString::StringOfChar('*', KeyCount);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormStatPlusTimeUp::Timer1Timer(TObject *Sender)
{ // 每 50mS 把視窗設為前景並設定一次滑鼠位置
Application->BringToFront();
ApplicationSetTopMost();
SetCursorPos(Screen->Width/2,8);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TFormStatPlusTimeUp::FormShow(TObject *Sender)
{
GetCursorPos(&LastMousePos);
Timer1->Enabled=true;
Timer2->Enabled=true; // 最多等 10 分鐘就關閉
CanCloseNow=false; // 無法按 Alt F4
}
//---------------------------------------------------------------------------
void __fastcall TFormStatPlusTimeUp::FormCloseQuery(TObject *Sender,
bool &CanClose)
{
CanClose=CanCloseNow;
}
//---------------------------------------------------------------------------
// 最多不讓使用者操作 10 分鐘,以免無法回到正常畫面
void __fastcall TFormStatPlusTimeUp::Timer2Timer(TObject *Sender)
{
this->Timer1->Enabled=false;
this->Timer2->Enabled=false;
CanCloseNow=true;
ApplicationClearTopMost();
WindowClearTopMost(this->Handle);
Close();
Application->MainForm->Show();
SetCursorPos(Application->MainForm->Left 100,Application->MainForm->Top 20);
}
//---------------------------------------------------------------------------
C# 初學者請多指教
< href="http://free.greenworld.com.tw/~dllee/" target="blank">吃軟也吃硬 dllee.ktop.com.tw
StatPlus 系統資源監測器 @ KTOP dllee's sharespace Fieldbus