Re: Mike: it's Working, more explanation Please.
- From: "Mike Williams" <Mike@xxxxxxxxxxxxxxxxx>
- Date: Mon, 10 Jul 2006 22:13:16 +0100
"Randy Gardner" <RandyGardner@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:6AE4F10D-3C63-4417-BE55-4630DFB21EFA@xxxxxxxxxxxxxxxx
Mike: Wow! Great help, I really apreciate it!!!!!
I could just use your code, BUT, I wouldn't have learn anything,
so would you please explain what is happening in the functions:
pConvertX/Y . . . . .
The ScaleMode of a picture box can be set to a number of different things, for example inches, centimeters, twips and quite a few others. Taking the simple example of a borderless picture box, if you set its ScaleMode to vbInches then x coordinate 2 (for example) would be 2 logical inches from the left side of the picture box. The number of available inches across the width of the box would therefore depend on its logical size, so a picture box that is 6 logical inches wide would have coordinate 0 at the left side, coordinate 6 at its right side and coordinate 3 in the middle (measured horizontally). If you consider an inch to be a "unit" then the number of units across the width of the picture box depends on its logical width. However, in our case we are settings the ScaleWidth of the picture box to a specified number of units (35.75 units in the specific example because we set it to 30 + 30/8 + 30/15 to allow for some white space at both sides of the graph itself). Setting the ScaleWidth to a specific value automatically causes the ScaleMode to be set to vbUser, and it means that the picture box will be 35.75 "units" wide, regardless of its actual physical width. This means we can make the picture box any physical size we wish and it will always be 35.75 "units" wide. The actual width of a unit will therefore depend on the actual width of the picture box, but whatever the size of the picture box there will always be 35.75 units across its width.
When we draw a dot, for example, we use the picture box coordinates to specify the dot's location (because this allows us to use units which suit our graph scale) - but - and this is the important thing - we do not actually draw the dot into the picture box. If you look at the code you will see that the dot is actually drawn directly to the chosen output device (printer or Form or whatever). Nothing is ever actually drawn into the picture box. However, the Form (or the printer) is not set to vbUser scalemode. It is actually set (in the specific example) to vbInches. This is so that you can use familiar units to draw other things into the Form (or the printer) as well as your graph. So, in order to draw the dot at the correct location on the Form (or the printer) we need to convert between the "easy to use for our graph" coordinates of the vbUser scalemode picture box (Picture1) to the "easy to use for everything else" coordinates of our vbInches scalemode Form (or printer). That is what the pConvertX and pConvertY functions do.
If you look for example at the pConvertX function you will see that there are basically three separate parts to the conversion. (Note that if you're printing your graph to the printer then currentObject will be the printer object). The first part . . .
Picture1.ScaleX(x, Picture1.ScaleMode, currentObject.ScaleMode)
.. . . converts the coordinate from "Picture1 picture box units" (vbUser) to "currentObject units" (vbInches in the example). If that is all we did then the coordinate would still be wrong, because the ScaleLeft setting of the picture box effectively "shifts" coordinate zero to somewhere else other than the left edge of the Picture1 picture box (because in our example we want coordinate zero in Picture1 to specify the location of the bottom left corner of the graph itself), whereas coordinate zero of currentObject (whose ScaleMode is vbInches and whose ScaleLeft property has not been altered from the default of zero) is the left edge of the object. So, the second part of the pConvertX function . . .
Picture1.ScaleX(Picture1.ScaleLeft, Picture1.ScaleMode, currentObject.ScaleMode)
.. . . makes the required adjustment. So far so good. Now we need a further adjustment, otherwise the graph will be printed at the top left corner of currentObject, whereas we in fact want to print it so that our graph can be anywhere on the page . . .
+ pOffsetX
If the destination (currentObject) is a Form or another Picture Box then pOffsetX is the same as the value you passed to the DrawGraph1 Subroutine in its xPos parameter. However, if currentObject is a printer then xPos is slightly different that the xPos parameter. This is because as far as VB is concerned the printer page on most printers is a rectangle that is slightly smaller than the physical size of the *** of paper in the printer, and this rectangle is not positioned at the top left corner of the physical *** of paper, but is usually set a little down from the top and a little across from the left. The required offset to account for this is calculated in the main body of the DrawGraph1 Subroutine as follows:
pOffsetX = xPos: pOffsetY = yPos
If TypeOf Dest Is Printer Then
' find left unprintable margin in printer pixels
borderLeft = GetDeviceCaps(Dest.hdc, PHYSICALOFFSETX)
' convert it to printer scale units and adjust pOffsetX accordingly
pOffsetX = pOffsetX - Dest.ScaleX(borderLeft, _
vbPixels, Dest.ScaleMode)
Hopefully, that explains the reasoning behind the pConvertX and pConvertY functions. I know that you've asked other questions as well in your post, but perhaps it might be wise for me to leave the answers to those questions until you have read and digested the above. Post back when you've got the pConvertX and pConvertY functions under your belt (or ask for clarification is there is anything in the above that you are having problems with) and then we can deal with your other questions.
Mike
.
- Follow-Ups:
- Re: Mike: Better informed, Fundamental questions
- From: Randy Gardner
- Re: Mike: Better informed, Fundamental questions
- References:
- Re: Custom ScaleMode setting problem
- From: Mike Williams
- Re: Mike: it's Working, more explanation Please.
- From: Randy Gardner
- Re: Custom ScaleMode setting problem
- Prev by Date: Re: vb6 bcoming irrelevant?
- Next by Date: Automating sending e-mail
- Previous by thread: Re: Mike: it's Working, more explanation Please.
- Next by thread: Re: Mike: Better informed, Fundamental questions
- Index(es):