C++ 讓程式在幕後執行 (Running a program from behind the scenes)

基於系統管理及維護的必要,有時必須在幕後執行程式,而且執行完立即關閉。實際做法是在別支程式中(暗中)執行此程式。

As the requests of system maintaining jobs , sometimes we need to run programs from behind the scenes , after done then close noiselessly. Pracitically, I  let it run secretly  while other program running .

本文示範以C++程式達成此需求。IDE為BCB6。

The article showing in C++(BCB) to accomplish the purpose.

設計階段的Form (Form in Design stage)

幕後(暗中)執行程式

設定Form1屬性Visible為False (Set Form’s ‘Visible’ attribute as False )

程式碼:(Codes)

void __fastcall TForm1::FormCreate(TObject *Sender) //本Form建立時
{
  //--- 隱匿Form (Set Form to be hid)
  if(CheckBox4->Checked==true)  //若有勾選 'Form隱匿' (Form will hide)
    Visible=false;
  else
    Visible=true;
  //-------------------------- 
  chk_bitbtn->Execute();        //檢查指定的條件是否足夠,以Enabled兩個BitBtn按鈕
  //--- 執行刪除捷徑檔與建立捷徑檔 (Deteling and creating shortcuts)
  if(getcomputername()!="P34"){      //排除電腦名稱為P34的 (computer name 'P34' been exclusived)
    if(CheckBox1->Checked==true &&    //若有勾選 '程式啟動時自動執行'
       BitBtn1->Enabled==true               //而且BitBtn1是Enabled
      )
      BitBtn1->Click();    //執行'刪除捷徑檔'按鈕事件(Deteling shortcuts)
    //-----------------
    if(CheckBox2->Checked==true &&   //若有勾選 '程式啟動時自動執行'
       BitBtn2->Enabled==true              //而且BitBtn2是Enabled
      )
      BitBtn2->Click();    //執行'建立捷徑檔'按鈕事件(creating a shortcut)
  }
  //--- 關閉程式 ---
  if(CheckBox3->Checked==true)    //若有勾選 '程式啟動後立即關閉'
    exit(0);  //關閉程式 (Closing program)
}

//刪除捷徑檔 (Deleting shortcuts codes)
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
...
}

//建立捷徑檔 (Creating a shortcuts codes)
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
...
}

程式執行時視窗隱匿,自動刪除與建立指定的捷徑檔,然後關閉。(Program will hide while launching , deleting and creating shortcuts automatically , then close noiselessly )

若將下方兩個勾選取消,則程式執行時就出現視窗,以供正常操作。(If two below CheckBoxs’ checked are  set as False,form will show while being ran , then we can operate it as usual case.)

 

 

 

 

 

 

 

發表留言