To center an image in Form 
Author Message
 To center an image in Form
I need to center in a Form of 6.0 VB an image that it previously loaded in
the Picture property of the same Form. I am trying with BitBlt but I do not
have great results.

Thanks, Javier O. Chrcoles

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.*-*-*.com/ ).
Version: 6.0.497 / Virus Database: 296 - Release Date: 04/07/2003



Sat, 24 Dec 2005 01:11:48 GMT  
 To center an image in Form

Quote:
> I need to center in a Form of 6.0 VB an image that it previously loaded in
> the Picture property of the same Form. I am trying with BitBlt but I do
not
> have great results.

The quick and cheesy way of doing this is to simply hijack the picture while
the form is running and draw it to the form when it repaints of rescales.
The problem with this is that you'll get a {*filter*} trail all over the form of
where it _used_ to be drawn, which is a pain.  The easiest way of getting
rid of it is to just clear the form before repainting the picture:

'***
Dim m_Picture As StdPicture

Private Sub Form_Load()
    Set m_Picture = Form1.Picture
    Set Form1.Picture = Nothing
End Sub

Private Sub Form_Paint()
    Call ReDraw
End Sub

Private Sub Form_Resize()
    Call ReDraw
End Sub

Private Sub ReDraw()
    Dim ThisX As Long, ThisY As Long
    Dim PicWidth As Long, PicHeight As Long

    If (Not (m_Picture Is Nothing)) Then
        With Form1 ' Get the picture locations in the form's scale mode
            PicWidth = .ScaleX(m_Picture.Width, vbHimetric, .ScaleMode)
            PicHeight = .ScaleY(m_Picture.Height, vbHimetric, .ScaleMode)
            ThisX = (Form1.ScaleWidth - PicWidth) \ 2
            ThisY = (Form1.ScaleHeight - PicHeight) \ 2
            Call .Cls
        End With

        Call PaintPicture(m_Picture, ThisX, ThisY)
    End If
End Sub
'***

This however gives you a {*filter*} flickering effect because for a split second
you see just the background colour before the image has been drawn.  To
bypass this you can use a number of methods such as double buffering the
background graphic or masking the previous image before drawing which have
their own benefits and drawbacks.  IMO the best way of doing this is to only
cover up the area that needs to be cleared since the last draw, for this you
can use GDI's region API's to create the area to be asked and simply fill
with the form's back-colour.  Here's the new code which shouldn't flicker
now (You may however get some tearing, but that's not something we can fix
easily in windows GDI programming, if it's a particular problem, look into
DirectX instead):

'***
Private Declare Function CreateRectRgn Lib "gdi32" _
    (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
    ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" _
    (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, _
    ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" _
    (ByVal hObject As Long) As Long
Private Declare Function FillRgn Lib "gdi32" _
    (ByVal hDC As Long, ByVal hRgn As Long, _
    ByVal hBrush As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" _
    (ByVal crColor As Long) As Long
Private Declare Function GetSysColor Lib "user32" _
    (ByVal nIndex As Long) As Long

Dim m_Picture As StdPicture
Dim LastRgn As Long

Private Const RGN_XOR As Long = 3
Private Const RGN_DIFF As Long = 4

Private Sub Form_Load()
    Set m_Picture = Form1.Picture
    Set Form1.Picture = Nothing
End Sub

Private Sub Form_Paint()
    Call ReDraw
End Sub

Private Sub Form_Resize()
    Call ReDraw
End Sub

Private Sub ReDraw()
    Dim ThisX As Long, ThisY As Long
    Dim PicWidth As Long, PicHeight As Long
    Dim DrawBrush As Long
    Dim ThisRgn As Long

    If (Not (m_Picture Is Nothing)) Then
        With Form1 ' Get the picture locations in the form's scale mode
            PicWidth = .ScaleX(m_Picture.Width, vbHimetric, .ScaleMode)
            PicHeight = .ScaleY(m_Picture.Height, vbHimetric, .ScaleMode)
            ThisX = (Form1.ScaleWidth - PicWidth) \ 2
            ThisY = (Form1.ScaleHeight - PicHeight) \ 2
        End With

        Call PaintPicture(m_Picture, ThisX, ThisY)

        With Form1 ' Convert the scale mode to pixels for the API
            PicWidth = .ScaleX(PicWidth, .ScaleMode, vbPixels)
            PicHeight = .ScaleY(PicHeight, .ScaleMode, vbPixels)
            ThisX = .ScaleX(ThisX, .ScaleMode, vbPixels)
            ThisY = .ScaleY(ThisY, .ScaleMode, vbPixels)
        End With

        ThisRgn = CreateRectRgn(ThisX, ThisY, ThisX + PicWidth, ThisY +
PicHeight)

        DrawBrush = CreateSolidBrush(EvalCol(Form1.BackColor))
        Call CombineRgn(LastRgn, LastRgn, ThisRgn, RGN_DIFF)
        Call FillRgn(Form1.hDC, LastRgn, DrawBrush)
        Call DeleteObject(DrawBrush)

        Call DeleteObject(LastRgn)
        LastRgn = ThisRgn
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call DeleteObject(LastRgn)
End Sub

Private Function EvalCol(ByVal inCol As Long) As Long
    ' Return the literal RGB value for any colour (System colour aware)
    If ((inCol And &HFFFFFF00) = &H80000000) Then _
        EvalCol = GetSysColor(inCol And &HFF) Else EvalCol = inCol
End Function
'***

If you want you can remove the need for PaintPicture() (And having to do the
double scale-mode conversion - Just go straight to pixels rather than going
via the form's scale-mode) you can create a temporary DC, select the image
into it and BitBlt from there.
Hope this helps,

    Mike

 - Microsoft Visual Basic MVP -

WWW: http://www.*-*-*.com/



Sat, 24 Dec 2005 01:58:04 GMT  
 To center an image in Form

Quote:
> I need to center in a Form of 6.0 VB an image that it previously loaded in
> the Picture property of the same Form. I am trying with BitBlt but I do not
> have great results.

Load it into a picturebox instead of the form:

Private Sub Form_Load()
  Picture1.AutoSize = True
  Picture1.AutoRedraw = True
  Picture1.BorderStyle = 0
End Sub

Private Sub Form_Resize()
  Picture1.Move (ScaleWidth - Picture1.Width) \ 2, _
                (ScaleHeight - Picture1.Height) \ 2
End Sub

The Image control suffers from tear issues when large pictures are
moved around.  The picturebox does not appear to have the same
problem which is why I suggested loading your image into a picturebox.

HTH
LFS



Sat, 24 Dec 2005 03:15:24 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Centering a form doesn't center the form

2. Center Image on MDI Form...Please help

3. Center Form Over Another Form?

4. Center Form over Form?

5. How to Centering a form's caption in the form

6. Center form within MIDI form

7. Centering Image ???

8. Centering an image on another

9. Centering paintpicture image in a picture box - HELP

10. Centering Image controls?

11. Centering images.

12. Displaying Images "Centered"

 

 
Powered by phpBB® Forum Software