473,734 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DrawRectangle positioning

How does DrawRectangle position a rectangle w.r.t to the Rectangle passed?
Lets suppose I use a pen of width 5. Are the lines centered about the passed
Rectangle or is it enclosed or offset? Are there ways to change the default
behavior?

The reason I ask is cause when I do

g.DrawRectangle (P, this.ClientRect angle) for drawing a rectangle as the
border for a control(button) and the width of P is larger than 1(or even
when its not it still looks wrong) the lines thickness is not what its
suppose to be,

Basically my code is

g.FillRegion(B, this.Region);

g.DrawRectangle (S, r1);

g.DrawRectangle (H, r2);

which draws two rectangles as the "border" for a button control. S and H are
pens for shadow and highlight.

my code for the rectangles is something like

Rectangle r1 = new Rectangle(2, 2, this.Width-x-2, this.Height-x-2);

Rectangle r2 = new Rectangle(1 + x+1+1, 1 + x+1+1, this.Width - x - 1 -
x-y-2-2, this.Height - x - 1 - x-y-2-2);

were x is the pens with. the values are not correct now as I was playing
around with them to see try and get it right.

Basicaly my first border would be (0,0, this.Width, this.Height) but because
of the way its draw I have to offset it some. the second border would then
be draw on the inside of this rectangle.

What I essentially want is to draw a rectangle that is interior to the pass
rectangle parameter and not where the lines of the rectangle are centered on
it. For larger pens it will should still be on the interior.

Its not to hard to convert from one method to the other if I knew which one
it used. Although there is the slight problem with rounding for pens of odd
width.

Thanks,

Jon
Oct 26 '06 #1
2 3503
Hi Jon,

I'm not surprised that you've had difficulty with this issue, as it is not
well-documented in the .Net SDK. The crux of the matter is this: When you
draw a rectangle with a pen, the width of the pen must be taken into account
with regards to the size of the target rectangle. There are 3 basic logical
possibilities:

1. The entire line is drawn *inside* the rectangle.
2. The width of the line is split, so that the edge of the rectangle is
centered in the line.
3. The entire line is drawn *outside* the rectangle.

It turns out that number *2* is the method employed with GDI+.

The following is an excellent and detailed article on the subject:

http://www.bobpowell.net/beginnersgdi.htm

--
HTH,

Kevin Spencer
Microsoft MVP
Short Order Coder
http://unclechutney.blogspot.com

The devil is in the yada yada yada
"Jon Slaughter" <Jo***********@ Hotmail.comwrot e in message
news:gQ******** *********@newss vr14.news.prodi gy.com...
How does DrawRectangle position a rectangle w.r.t to the Rectangle passed?
Lets suppose I use a pen of width 5. Are the lines centered about the
passed Rectangle or is it enclosed or offset? Are there ways to change the
default behavior?

The reason I ask is cause when I do

g.DrawRectangle (P, this.ClientRect angle) for drawing a rectangle as the
border for a control(button) and the width of P is larger than 1(or even
when its not it still looks wrong) the lines thickness is not what its
suppose to be,

Basically my code is

g.FillRegion(B, this.Region);

g.DrawRectangle (S, r1);

g.DrawRectangle (H, r2);

which draws two rectangles as the "border" for a button control. S and H
are pens for shadow and highlight.

my code for the rectangles is something like

Rectangle r1 = new Rectangle(2, 2, this.Width-x-2, this.Height-x-2);

Rectangle r2 = new Rectangle(1 + x+1+1, 1 + x+1+1, this.Width - x - 1 -
x-y-2-2, this.Height - x - 1 - x-y-2-2);

were x is the pens with. the values are not correct now as I was playing
around with them to see try and get it right.

Basicaly my first border would be (0,0, this.Width, this.Height) but
because of the way its draw I have to offset it some. the second border
would then be draw on the inside of this rectangle.

What I essentially want is to draw a rectangle that is interior to the
pass rectangle parameter and not where the lines of the rectangle are
centered on it. For larger pens it will should still be on the interior.

Its not to hard to convert from one method to the other if I knew which
one it used. Although there is the slight problem with rounding for pens
of odd width.

Thanks,

Jon


Oct 26 '06 #2

"Kevin Spencer" <sp**@uce.govwr ote in message
news:OV******** ********@TK2MSF TNGP02.phx.gbl. ..
Hi Jon,

I'm not surprised that you've had difficulty with this issue, as it is not
well-documented in the .Net SDK. The crux of the matter is this: When you
draw a rectangle with a pen, the width of the pen must be taken into
account with regards to the size of the target rectangle. There are 3
basic logical possibilities:

1. The entire line is drawn *inside* the rectangle.
2. The width of the line is split, so that the edge of the rectangle is
centered in the line.
3. The entire line is drawn *outside* the rectangle.

It turns out that number *2* is the method employed with GDI+.

The following is an excellent and detailed article on the subject:

http://www.bobpowell.net/beginnersgdi.htm
Thanks. I figured it was 2 because the other 2 I tried didn't work. At first
I thought it was a mixture of 1 and 2(the left and top being on the inside
and the bottom and right being on the outside) but none of my calculations
worked and I couldn't find any info regarding it.

Thanks again,
Jon
Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
32062
by: Bryan R. Meyer | last post by:
Hello Everyone, The problem of browser resizing has become an issue for me. While redesigning my webpage, I set the left and right margins to be auto so that my content would be centered. However, there are images that I used absolute positioning in order to place them in the appropriate location on my page. When I do a browser resize, these images do not move (obviously). However, the rest of my content is auto centered as...
4
2715
by: Jane Withnolastname | last post by:
I am trying to re-work an old site by replacing the html with css. On the main page, I have a logo image which I needed centred on the initial screen. I found the solution here: http://www.wpdfd.com/editorial/wpd0103.htm#toptip (the second example) The problem is, under the image is a large table. But using the above positioning, now the table starts at the top of the page and runs underneath the image.
1
2572
by: Siri Krishna | last post by:
Hi, We have a custom control into which we add controls like labels and move them at run time. The problem we have is that when a child control is selected and moved, we draw a rectangle around it to identify it as selected. The DrawRectangle works fine when the child controls is on left half of the custom control area but fails to draw the rectangle when moved to right half of the custom control. When placed some where in between we see...
3
10324
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case shapeType Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10) Case 2 'draw a circle Case 3 'draw a triangle Case 4 'draw other shape Case 5 'draw other shape
5
1718
by: Cristian | last post by:
Hi. I've made so: Imports System.Drawing Imports System.Drawing.Drawing2D Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
2
3378
by: Dan | last post by:
I want to use Graphics.DrawRectangle to draw a rectangle at a position on the screen, and then a few seconds later, erase it. How do you erase a rectangle that you just drew (without affecting the original background of the window(s) behind the rectangle)? Thanks... Dan
1
3773
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw the rectangle using the right mouse button to represent the area of the chart they want to zoom on. I haev been able to draw the rectangle on a blank form but I cannot get it to draw on top of other controls. I have pasted in the code i am using ....
6
2916
by: Mark | last post by:
hi, i'm trying to position something in the top right corner of a container, but i can't seem to figure out how to get it working. here's the html <div class='thumb'><a href='image.jpg'><img src='photos/thumbs/ bigsmile.jpg'></a><a class='del' href='?p=gallery&del=2'>x</a></div> where 'thumb' is my container, and 'del' should be aligned to the top right. here's the css
1
3532
by: t f | last post by:
Hi FillRectangle allows me to use a rectanglef as a parameter, why doesnt DrawRectangle? Even though I can use floats as parameters? Did M$ forget to add it? Thanks t f
0
8956
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4819
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3267
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.