iT邦幫忙

0

C++程式: 偵測及修正Form位置

c c++
  • 分享至 

  • xImage
  •  

本程式乃C++教學的小案例:按左鍵即偵測Form位置,若超出桌面則移回桌面內

示範重點:
(1) 使用MESSAGE_HANDLER即時偵測Form位置大小
(2) 判斷及修正Form位置的方法
(3) if-else三元運算符(三元表達式)的用法


.h

private:	// User declarations
    int w0, h0, t, l, w, h, bb, rr;
    
    void __fastcall WMSize(TWMSize &Msg);
    void __fastcall WMMove(TWMMove &Msg);

    BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER(WM_SIZE, TWMSize, WMSize)
        MESSAGE_HANDLER(WM_MOVE, TWMMove, WMMove)
    END_MESSAGE_MAP(TForm)

.cpp

TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  //執行更新Form大小位置資訊並顯示, execute update Form's size and positin data end showing them
  update_data->Execute();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::update_dataExecute(TObject *Sender)
{
  //更新Form大小位置資訊並顯示, update Form's size and positin data and showing them

  w0=Screen->Width;    lbl_w0->Caption=w0;
  h0=Screen->Height;   lbl_h0->Caption=h0;
  t=Top;               lbl01->Caption=t;
  l=Left;              lbl02->Caption=l;
  w=Width;             lbl03->Caption=w;
  h=Height;            lbl04->Caption=h;
  bb=h0-t-h;           lbl_bb->Caption=bb;
  rr=w0-l-w;           lbl_rr->Caption=rr;

  (t<0) ? lbl01->Font->Color=clRed  : lbl01->Font->Color=clBlue;
  (l<0) ? lbl02->Font->Color=clRed  : lbl02->Font->Color=clBlue;
  (bb<0) ? lbl_bb->Font->Color=clRed  : lbl_bb->Font->Color=clBlue;
  (rr<0) ? lbl_rr->Font->Color=clRed  : lbl_rr->Font->Color=clBlue;
}

//---------------------------------------------------------------------------
void __fastcall TForm1::WMSize(TWMSize &Msg)
{
  //當改變Form大小時, while resizing the Form
  TForm::Dispatch(&Msg); // call default handler

  //執行更新Form大小位置資訊並顯示, execute update Form's size and positin data end showing them
  update_data->Execute();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::WMMove(TWMMove &Msg)
{
  //當移動Form時, while moving the Form
  TForm::Dispatch(&Msg);

  //執行更新Form大小位置資訊並顯示, execute update Form's size and positin data end showing them
  update_data->Execute();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  //按滑鼠左鍵執行修正Form位置使在桌面內, correcting the Form's position when mouse click the Form
  if(Button==mbLeft)
    adjust->Execute();
}

//---------------------------------------------------------------------------
void __fastcall TForm1::adjustExecute(TObject *Sender)
{
  //修正Form位置使在桌面內, correcting the Form's position

  Width  =  (w > w0) ? w0 : w;  //Form寬 adjust the forem width
  Height =  (h > h0) ? h0 : h;  //Form高 adjust the forem height

  Left = ((l+w)>w0) ? w0-w : l; //右邊 adjust right side border
  Left = (l<0) ? 0 : l;         //左邊 adjust left side border
  Top = ((t+h)>h0) ? h0-h : t;  //底部 adjust bottom side border
  Top = (t<0) ? 0 : t;          //頂部 adjust left side border
}

//---------------------------------------------------------------------------

文章在此


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言