DisplayRectagle - help please

H

Hilton

Hi,

Let's say I create a TabControl and add a tabpage. How can I get its
DisplayRectangle? (this is not supported in the CF) i.e. how can I (for
example) draw a rectangle around the (usable) area? If MS removed
DisplayRectangle, I'm assuming (hoping) that I can somehow calculate the
rectangle; e.g. new Rectangle (TabPage.Border, TabPage.TabHeight, ...) - you
get the idea.

How do I calculate what rectangle I can use?

Hilton
 
A

Alex Feinman [MVP]

Every control has ClientRectangle property. YOu need one returned by TabPage
 
H

Hilton

Alex said:
Every control has ClientRectangle property. YOu need one returned by
TabPage

And therein lies the problem. The ClientRectangle returned by TabPage is
exactly the same as its Bounds. I have included test code below. It
creates the main window, puts a TabControl in it, a TabPage in that, and a
green control in the tab page. The green control is made 20 pixels less
high and wide - you'll only see it get narrower. Even more obvious,
uncomment the MessageBox lines - you see the TabPage's Bounds and
ClientRectangle are equal!!! (that's gotta be a bug - right???) If I
resize the main window by dragging with the mouse (we can't do this in the
CF), then the client rectangle gets set correctly.

If I am doing something wrong in the code, please let me know, however I
believe that the Bounds and ClientRectangle should never be equal (in this
case).

Is there a workaround?

Hilton


Here's the code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestTabRect
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

TabControl tabControl = null;
TabPage tabPage = null;
Control control = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// Create the controls
//
this.tabControl = new TabControl ();
this.tabPage = new TabPage ("Test");
this.control = new Control ();

//
// Create the controls
//
this .Controls.Add (this.tabControl);
this.tabControl.Controls.Add (this.tabPage);
this.tabPage .Controls.Add (this.control);

// Make the control green so that we can see it
this.control.BackColor = Color.Green;

this.SetSize ();
}

protected override void OnResize (EventArgs e)
{
base.OnResize (e);

this.SetSize ();
}

protected void SetSize ()
{
// Make the tabcontrol fill the window
this.tabControl.Bounds = this.ClientRectangle;

// Make the tab page fill the tab control's client rectangle
this.tabPage.Bounds = this.tabControl.ClientRectangle;

// Uncomment these - you'll see the numbers are the same
//MessageBox.Show (this.tabPage.Bounds .ToString (),
"SetSize");
//MessageBox.Show (this.tabPage.ClientRectangle.ToString (),
"SetSize");

// This clearly shows the bug - the green should move in
equally from bottom and right
this.control.Bounds = new Rectangle
(this.tabPage.ClientRectangle.X,

this.tabPage.ClientRectangle.Y,

this.tabPage.ClientRectangle.Width - 20,

this.tabPage.ClientRectangle.Height - 20);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 
A

Alex Feinman [MVP]

Ok. I see what you mean.
Workarounds:
1) When you create TabControl and add tabs, they are sized correctly. Store
the difference beween TabControl.ClientRectangle.Height and
TabPage1.ClientRectangle.Height. This would be your adjustment value
2) The height of the "dangling" part of the tab (label) can be retireved by
sending TCM_GETITEMRECT message to the control
3) The conversion between the inner and outer rectangle of the tab can be
done by sending TCM_ADJUSTRECT. Pass 0 as WParam to convert
TabControl.ClientRectangle to TabPage.Bounds (this is wrong by a few pixels
for some reason. not sure why)


Hilton said:
Alex said:
Every control has ClientRectangle property. YOu need one returned by
TabPage

And therein lies the problem. The ClientRectangle returned by TabPage is
exactly the same as its Bounds. I have included test code below. It
creates the main window, puts a TabControl in it, a TabPage in that, and a
green control in the tab page. The green control is made 20 pixels less
high and wide - you'll only see it get narrower. Even more obvious,
uncomment the MessageBox lines - you see the TabPage's Bounds and
ClientRectangle are equal!!! (that's gotta be a bug - right???) If I
resize the main window by dragging with the mouse (we can't do this in the
CF), then the client rectangle gets set correctly.

If I am doing something wrong in the code, please let me know, however I
believe that the Bounds and ClientRectangle should never be equal (in this
case).

Is there a workaround?

Hilton


Here's the code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestTabRect
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

TabControl tabControl = null;
TabPage tabPage = null;
Control control = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// Create the controls
//
this.tabControl = new TabControl ();
this.tabPage = new TabPage ("Test");
this.control = new Control ();

//
// Create the controls
//
this .Controls.Add (this.tabControl);
this.tabControl.Controls.Add (this.tabPage);
this.tabPage .Controls.Add (this.control);

// Make the control green so that we can see it
this.control.BackColor = Color.Green;

this.SetSize ();
}

protected override void OnResize (EventArgs e)
{
base.OnResize (e);

this.SetSize ();
}

protected void SetSize ()
{
// Make the tabcontrol fill the window
this.tabControl.Bounds = this.ClientRectangle;

// Make the tab page fill the tab control's client rectangle
this.tabPage.Bounds = this.tabControl.ClientRectangle;

// Uncomment these - you'll see the numbers are the same
//MessageBox.Show (this.tabPage.Bounds .ToString (),
"SetSize");
//MessageBox.Show (this.tabPage.ClientRectangle.ToString (),
"SetSize");

// This clearly shows the bug - the green should move in
equally from bottom and right
this.control.Bounds = new Rectangle
(this.tabPage.ClientRectangle.X,

this.tabPage.ClientRectangle.Y,

this.tabPage.ClientRectangle.Width - 20,

this.tabPage.ClientRectangle.Height - 20);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top