C++Builder Tips


フォーム・移動制限(水平・垂直・固定)


フォームの移動を制限するサンプルです。


//---------------------------------------------------------------------------
// Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE 管理のコンポーネント
  TRadioButton *RadioButton1;
  TRadioButton *RadioButton2;
  TRadioButton *RadioButton3;
private:
  void __fastcall WMMOVING(TMessage & msg); // ユーザー宣言
public: // ユーザー宣言
  __fastcall TForm1(TComponent* Owner);
protected:
  
BEGIN_MESSAGE_MAP
    VCL_MESSAGE_HANDLER(WM_MOVING, TMessage, WMMOVING)
  END_MESSAGE_MAP(TForm)

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------
// Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::WMMOVING(TMessage & msg)
{
  TRect *pRect = (TRect*)msg.LParam;

  //垂直方向の移動制限
  if (RadioButton1->Checked){
    //今いる位置(Top)から移動しようとする位置(pRect->Top)の差をオフセットする
    OffsetRect((RECT*)pRect, 0, Top - pRect->Top);
  }

  //水平方向の移動制限
  if (RadioButton2->Checked){
    //今いる位置(Left)から移動しようとする位置(pRect->Left)の差をオフセットする
    OffsetRect((RECT*)pRect, Left - pRect->Left, 0);
  }

  //位置固定
  if (RadioButton3->Checked){
    OffsetRect((RECT*)pRect, Left - pRect->Left, Top - pRect->Top);
  }

}
//---------------------------------------------------------------------------

DownLoad bcbtips067.lzh 4KB(BCB5)