Bagikan melalui


Control.ClientSize Properti

Definisi

Mendapatkan atau mengatur tinggi dan lebar area klien kontrol.

public:
 property System::Drawing::Size ClientSize { System::Drawing::Size get(); void set(System::Drawing::Size value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Size ClientSize { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ClientSize : System.Drawing.Size with get, set
Public Property ClientSize As Size

Nilai Properti

Size yang mewakili dimensi area klien kontrol.

Atribut

Contoh

Contoh kode berikut mengubah ukuran kontrol yang ditentukan sehingga kontrol akan mengakomodasi teks yang diformat. Teks yang diformat Text adalah properti dengan kontrol yang ditetapkan Font diterapkan ke teks. Metode AutoSizeControl dalam contoh ini juga memiliki textPadding parameter yang mewakili padding untuk diterapkan ke semua tepi kontrol. Untuk membuat padding muncul sama, ratakan teks dengan ContentAlignment.MiddleCenter nilai , jika kontrol Anda mendukungnya.

private:
   void AutoSizeControl( Control^ control, int textPadding )
   {
      
      // Create a Graphics object for the Control.
      Graphics^ g = control->CreateGraphics();
      
      // Get the Size needed to accommodate the formatted Text.
      System::Drawing::Size preferredSize = g->MeasureString( control->Text, control->Font ).ToSize();
      
      // Pad the text and resize the control.
      control->ClientSize = System::Drawing::Size( preferredSize.Width + (textPadding * 2), preferredSize.Height + (textPadding * 2) );
      
      // Clean up the Graphics object.
      delete g;
   }
private void AutoSizeControl(Control control, int textPadding)
{
   // Create a Graphics object for the Control.
   Graphics g = control.CreateGraphics();

   // Get the Size needed to accommodate the formatted Text.
   Size preferredSize = g.MeasureString(
      control.Text, control.Font).ToSize();

   // Pad the text and resize the control.
   control.ClientSize = new Size(
      preferredSize.Width + (textPadding * 2), 
      preferredSize.Height+(textPadding * 2) );

   // Clean up the Graphics object.
   g.Dispose();
}
Private Sub AutoSizeControl(control As Control, textPadding As Integer)
   ' Create a Graphics object for the Control.
   Dim g As Graphics = control.CreateGraphics()
   
   ' Get the Size needed to accommodate the formatted Text.
   Dim preferredSize As Size = g.MeasureString( _
     control.Text, control.Font).ToSize()
   
   ' Pad the text and resize the control.
   control.ClientSize = New Size( _
     preferredSize.Width + textPadding * 2, _
     preferredSize.Height + textPadding * 2)
   
   ' Clean up the Graphics object.
   g.Dispose()
End Sub

Keterangan

Area klien kontrol adalah batas kontrol, dikurangi elemen nonkelola seperti bilah gulir, batas, bilah judul, dan menu. Metode SetClientSizeCore ini dipanggil untuk mengatur ClientSize properti . Properti ClientSize tidak selalu diubah melalui metodenya set sehingga Anda harus mengambil alih SetClientSizeCore metode untuk memastikan bahwa kode Anda dijalankan saat ClientSize properti diatur.

Properti Size.Width dan Size.Height mewakili lebar dan tinggi area klien kontrol. Anda dapat menggunakan properti ini untuk mendapatkan ukuran area klien kontrol untuk tugas seperti menggambar di permukaan kontrol.

Untuk informasi selengkapnya tentang menggambar pada kontrol, lihat Merender Kontrol Formulir Windows.

Nota

Anda tidak dapat mengikat pengaturan aplikasi ke properti ini. Untuk informasi selengkapnya tentang pengaturan aplikasi, lihat Gambaran Umum Pengaturan Aplikasi.

Berlaku untuk

Lihat juga