Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can i get the VISIBLE bounds of a control. [modified]

How can i get the VISIBLE bounds of a control. [modified]

Scheduled Pinned Locked Moved C#
question
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hwswin
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • H hwswin

      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

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      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.

      H 1 Reply Last reply
      0
      • R 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.

        H Offline
        H Offline
        hwswin
        wrote on last edited by
        #3

        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?

        H 1 Reply Last reply
        0
        • H hwswin

          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?

          H Offline
          H Offline
          hwswin
          wrote on last edited by
          #4

          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).

          R 1 Reply Last reply
          0
          • H hwswin

            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).

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            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.

            H 1 Reply Last reply
            0
            • R 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.

              H Offline
              H Offline
              hwswin
              wrote on last edited by
              #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
              
              H R 2 Replies Last reply
              0
              • H hwswin

                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
                
                H Offline
                H Offline
                hwswin
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • H hwswin

                  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
                  
                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  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.

                  H 1 Reply Last reply
                  0
                  • R 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.

                    H Offline
                    H Offline
                    hwswin
                    wrote on last edited by
                    #9

                    change the p.Width = 1.5f; to p.Width = 10f; you can see the left and top line were drawed.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups