473,856 Members | 1,711 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 3514
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
32075
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
2722
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
2578
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
10336
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
1726
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
3390
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
3790
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
2922
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
3538
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
11048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10692
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
10770
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
10376
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
9526
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7928
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7084
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3194
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.