How can i get the VISIBLE bounds of a control. [modified]
-
I can give the rectangle of a control by bounds.but I don't know how get the visble rectangle when a control not display whole.
Rectangle r = ctrl.Bounds;
ctrl.DisplayRectangle;:confused:
modified on Thursday, June 11, 2009 12:25 AM
What do you mean by not displayed whole? Windows keeps track of what parts of a control need to be repainted and joins them together into a 'clip rectangle' which is passed the the OnPaint handler.
Regards, Rob Philpott.
-
What do you mean by not displayed whole? Windows keeps track of what parts of a control need to be repainted and joins them together into a 'clip rectangle' which is passed the the OnPaint handler.
Regards, Rob Philpott.
Thanks! I have two container controls like follow "a" and "b" ┌────┐ | | | a | | | └────┘ ┌───────┐ | | | b | | | └───────┘ b is bigger than a And I drag b into a,so b only display the visible area and I want to get that,not b.bounds ┌────┐ | a┌──┤ | | | | | b | └─┴──┘ how can I get it?
-
Thanks! I have two container controls like follow "a" and "b" ┌────┐ | | | a | | | └────┘ ┌───────┐ | | | b | | | └───────┘ b is bigger than a And I drag b into a,so b only display the visible area and I want to get that,not b.bounds ┌────┐ | a┌──┤ | | | | | b | └─┴──┘ how can I get it?
Ok,I get it,follow
ctrlP = ctrl.Parent;
Rectangle r = ctrl.Bounds;
Rectangle rP = new Rectangle(0, 0, ctrlP.Width, ctrlP.Height);
r.Intersect(rP);but I have new issue,I want to draw the border of the "display bound"
Graphics g = ctrlP.CreateGraphics();
Pen p = new Pen(Color.Red);
p.Width = 1.5f;g.DrawRectangle(p, r);
g.Dispose();It draw the top and the left line fine,but the right and the bottom line were clear,because OnPaint event of ctrl? and this method was fired in Form_Paint(object sender, PaintEventArgs e).
-
Ok,I get it,follow
ctrlP = ctrl.Parent;
Rectangle r = ctrl.Bounds;
Rectangle rP = new Rectangle(0, 0, ctrlP.Width, ctrlP.Height);
r.Intersect(rP);but I have new issue,I want to draw the border of the "display bound"
Graphics g = ctrlP.CreateGraphics();
Pen p = new Pen(Color.Red);
p.Width = 1.5f;g.DrawRectangle(p, r);
g.Dispose();It draw the top and the left line fine,but the right and the bottom line were clear,because OnPaint event of ctrl? and this method was fired in Form_Paint(object sender, PaintEventArgs e).
Yup, that looks good using the intersection of the two rectangles. The right and bottom line are probably not appearing because they're outside the clip rectangle. The Rectangle method draws a rectangle where the dimensions you supply are the area inside and not including the lines. Try
deflate()
ing the rectangle a bit and you should see all four sides.Regards, Rob Philpott.
-
Yup, that looks good using the intersection of the two rectangles. The right and bottom line are probably not appearing because they're outside the clip rectangle. The Rectangle method draws a rectangle where the dimensions you supply are the area inside and not including the lines. Try
deflate()
ing the rectangle a bit and you should see all four sides.Regards, Rob Philpott.
Thanks again!
Rob Philpott wrote:
The right and bottom line are probably not appearing because they're outside the clip rectangle
I use the
Inflate(-2, -2)
to make sure the Rectangle was inside the clip rectangle,but they're still not appearing. follow is the test code.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace draw
{
public partial class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.groupBox1); this.panel1.Location = new System.Drawing.Point(192, 141); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(200, 100); this.panel1.TabIndex = 0; // // groupBox1 // this.groupBox1.Location = new System.Drawing.Point(91, 43); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 100); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "groupBox1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(6
-
Thanks again!
Rob Philpott wrote:
The right and bottom line are probably not appearing because they're outside the clip rectangle
I use the
Inflate(-2, -2)
to make sure the Rectangle was inside the clip rectangle,but they're still not appearing. follow is the test code.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace draw
{
public partial class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.groupBox1); this.panel1.Location = new System.Drawing.Point(192, 141); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(200, 100); this.panel1.TabIndex = 0; // // groupBox1 // this.groupBox1.Location = new System.Drawing.Point(91, 43); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 100); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "groupBox1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(6
I think the cause is the graphics,which belong to panel1(the parent of groupbox1),we drawed the rectangle on the panel1 but it just under the groupbox1.So the rectangle we drawed were cover by the groupbox1.
-
Thanks again!
Rob Philpott wrote:
The right and bottom line are probably not appearing because they're outside the clip rectangle
I use the
Inflate(-2, -2)
to make sure the Rectangle was inside the clip rectangle,but they're still not appearing. follow is the test code.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace draw
{
public partial class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.panel1.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.Add(this.groupBox1); this.panel1.Location = new System.Drawing.Point(192, 141); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(200, 100); this.panel1.TabIndex = 0; // // groupBox1 // this.groupBox1.Location = new System.Drawing.Point(91, 43); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(200, 100); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "groupBox1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(6
I'm not sure I like the look of this. You appear to be painting on the groupbox in the paint event of the form. Really you should only use that to paint areas of the form that don't have controls on them. You'd need to do that paint in the groupbox's paint event, not the form one. Likelyhood is that the groupbox paints itself immediately after the form. Do you get flicker at all?
Regards, Rob Philpott.
-
I'm not sure I like the look of this. You appear to be painting on the groupbox in the paint event of the form. Really you should only use that to paint areas of the form that don't have controls on them. You'd need to do that paint in the groupbox's paint event, not the form one. Likelyhood is that the groupbox paints itself immediately after the form. Do you get flicker at all?
Regards, Rob Philpott.