Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forms Graphics Origin 2

Status
Not open for further replies.

comaboy

Programmer
Nov 6, 2001
261
GB
I've search high and low for this one...

Is there a way to move the form's graphics origin so that (0, 0) could be set to the centre of the form, rather than the corner?
 
You have to set the scale mode to user, and then fiddle with the scale properties.

Create a new project and paste this in to the form's code section.

Code:
Private Sub Form_Load()
    
    Me.AutoRedraw = True
    Me.ScaleMode = vbUser
    Me.ScaleLeft = -100
    Me.ScaleTop = 100
    Me.ScaleWidth = 200
    Me.ScaleHeight = -200
    
    
    Me.PSet (0, 0), vbRed
    Me.Circle (0, 0), 10, vbRed
    
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
George you absoulte hero!

I knew it must be one of those "Easy when you already know questions" !

Now I play with some good old-fashioned graphics algorythms (and by Ol-d-Fashioned... BBC Model B anyone...)

Thanks again, George... have a star!
 
PS...Note to self...Proof-read twice before hitting submit!
 
Glad to help.

One thing to watch out for... There will be considerable distortion if your form is not square. To test this... Set the shape of the form so that it is much higher than it is wide. Then run the code. The circle looks like an elipse.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Or you can use the SetViewportOrgEx API call, eg
Code:
[blue]' Move graphics origin to position X,Y pixels
' On return X and Y contain previous graphics origin
Private Sub MoveGraphicsOrigin(Target As PictureBox, ByRef X As Long, ByRef Y As Long)
    Dim PreviousOrigin As POINTAPI
    Dim PreviousScaleMode As Long
    
    PreviousScaleMode = Target.ScaleMode
    Target.ScaleMode = vbPixels
    SetViewportOrgEx Target.hdc, X, Y, PreviousOrigin ' Works in device units, i.e pixels
    Target.ScaleMode = PreviousScaleMode
    X = PreviousOrigin.X
    Y = PreviousOrigin.Y
End Sub[/blue]
 
Whoops, forgot the declarations:

Private Declare Function SetViewportOrgEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
 
strongm, now you're just showing off.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
heh!

Funnily enough, I dont get distortion, the circle "clips out" of the form (um if you see what I mean). Actually, I'll probably end up using strongm's suggestion.

Actually, scratch that!

PictureBox, not form. In the great tradition of analysing code and checking function descriptions and documentation*, I just went in and hacked out the following code...

And ended up with a "screenshot" VB Form! Explain THAT one, strongm! WTF is happening (or not) to the Form Window? It may not be the intended effect (and may not work on different flavours of Windows), but it's cool on XP Pro SP2...

Here's a quick copy/paste to a blank form. Hit run, drag the form around and go WOW!

Code:
Option Explicit

Private Declare Function SetViewportOrgEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpPoint As POINTAPI) As Long
Private Type POINTAPI
        X As Long
        Y As Long
End Type

' Move graphics origin to position X,Y pixels
' On return X and Y contain previous graphics origin
Private Sub MoveGraphicsOrigin(Target As [b]Form[/b], ByRef X As Long, ByRef Y As Long)
    Dim PreviousOrigin As POINTAPI
    Dim PreviousScaleMode As Long
    
    PreviousScaleMode = Target.ScaleMode
    Target.ScaleMode = vbPixels
    SetViewportOrgEx Target.hdc, X, Y, PreviousOrigin ' Works in device units, i.e pixels
    Target.ScaleMode = PreviousScaleMode
    X = PreviousOrigin.X
    Y = PreviousOrigin.Y
End Sub

Private Sub Form_Load()
    Show
    MoveGraphicsOrigin Me, ScaleWidth / 2, ScaleHeight / 2
' the following two lines don't quite work...
    PSet (0, 0), vbRed
    Line (0, 0)-(100, 100), vbRed
End Sub

* Doc's? this is Microsoft, :)

PS Have a star, strongm
 
1) I assume that the scale mode of the form is set to pixels?

2) You should generally try and avoid doing any form painting during a Form_Load event - especially if bypassing the safetynet provided by VB paint/draw routines as we are with SetViewportOrgEx

But if you insist on doing it in the Load event, try:
Code:
[blue]
Private Sub Form_Load()
    [green]' Remember: MoveGraphicsOrigin requires X and Y passsed as pixel value
    ' so, if we are going to use ScaleWidth and Scaleheight we need to ensure
    ' ScaleMode is vbPixels[/green]
    [b]ScaleMode = vbPixels[/b]
    Show
    [b]Refresh [green]' Give form chance to update itself *before* we muck about with it ...[/green][/b]
    MoveGraphicsOrigin Me, ScaleWidth / 2, ScaleHeight / 2
    [green]' the following two lines don't quite work...[/green]
    PSet (0, 0), vbRed
    Line (0, 0)-(100, 100), vbRed
End Sub[/blue]


 
Strongm,

Thanks for the explanation - you're quite right, of course!

I did mention that code was a hack-job! (the "real" drawing gets fired by a Timer event). It's just the "screen-grab" effect is pretty funky! (Especially when you're got a few apps open to jump between!)

Thanks again!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top