GetStaticRect (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example is taken from TOpenPictureDialog. It calculates the preview rectangle based on the difference between the client area of the dialog and the static area of the standard controls.


Code

#include <ExtDlgs.hpp>

class MyOpenPictureDialog : public TOpenPictureDialog
{
private:	// User declarations
	TPanel *FPicturePanel;  // Cannot use the private versions in TOpenPictureDialog.
	TLabel *FPictureLabel;
	TSpeedButton *FPreviewButton;
	TPanel *FPaintPanel;
	TImage *FImageCtrl;
	UnicodeString FSavedFilename;
protected:	// User declarations
	DYNAMIC void __fastcall DoShow(void);
public:		// User declarations
	__fastcall MyOpenPictureDialog(TComponent* Owner);
	virtual bool __fastcall  Execute(HWND ParentWnd){ return TOpenDialog::Execute(ParentWnd); }
};

__fastcall MyOpenPictureDialog::MyOpenPictureDialog(TComponent* Owner)
	: TOpenPictureDialog(Owner)
{
}

#include <memory>       //For STL auto_ptr class

void __fastcall MyOpenPictureDialog::DoShow(void)
{
//  RECT *PreviewRect = new RECT();
  std::auto_ptr<RECT> PreviewRect(new RECT());
  TRect StaticRect;
  FPicturePanel = new TPanel(this); // The owner will clean this up.
  FPictureLabel = new TLabel(this); // The owner will clean this up.
  FPreviewButton = new TSpeedButton(this); // The owner will clean this up.
  FPaintPanel = new TPanel(this); // The owner will clean this up.
  FImageCtrl = new TImage(this); // The owner will clean this up.
  // Set preview area to the entire dialog.
  GetClientRect(Handle, PreviewRect.get());
  StaticRect = GetStaticRect();
  // Move preview area to the right of the static area
  PreviewRect->left = StaticRect.Left + (StaticRect.Right - StaticRect.Left);
  PreviewRect->top += 4;
  FPicturePanel->BoundsRect.Left = PreviewRect->left;
  FPicturePanel->BoundsRect.Right = PreviewRect->right;
  FPicturePanel->BoundsRect.Top = PreviewRect->top;
  FPicturePanel->BoundsRect.Bottom = PreviewRect->bottom;

  FPreviewButton->Left = FPaintPanel->BoundsRect.Right - FPreviewButton->Width - 2;
  FImageCtrl->Picture = NULL;
  FSavedFilename = "";
  FPaintPanel->Caption = "";
  FPicturePanel->ParentWindow = Handle;
//  TOpenDialog::DoShow();   // The above code is equivalent to the inherited version.
}

TForm1 *Form1;
MyOpenPictureDialog *OpenPictureDialog1;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if (OpenPictureDialog1->Execute(GetParentHandle()))
	  Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  OpenPictureDialog1 =  new MyOpenPictureDialog(Form1); // The owner will clean this up.
}

Uses