Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePosition a popup form over a PictureBox in Visual Basic 6
DescriptionThis example shows how to position a popup form over a PictureBox in Visual Basic 6.
Keywordspopup, position popup, PictureBox, Visual Basic, ClientToScreen, ShowWindow, SetWindowPos
CategoriesSoftware Engineering, API, Controls
 
When the program's main form loads, the code uses the SetWindowPos API function to make the popup form a topmost window.
 
' Make the popup form topmost.
Private Sub Form_Load()
    SetWindowPos frmPopup.hwnd, _
        HWND_TOPMOST, 0, 0, 0, 0, _
        SWP_NOMOVE + SWP_NOSIZE
End Sub
 
When you click the PictureBox, the following code executes. It uses the ClientToScreen API function to find the TextBox's position in screen coordinates. It calculates the width and height of the PictureBox and converts all dimensions into twips.

If you clicked the rigfht button on the PictureBox, the code expands the area a bit to cover the PictureBox's borders. Otherwise the area only covers the PictureBox's interior.

The code moves the popup form to the calculated location with the calculated size, and uses the ShowWindow API function to display the form without giving it the focus. Click on the popup to make it go away.

 
' Position the popup.
Private Sub picSun_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
Dim pt As POINTAPI

    ' Find the PictureBox's location on the screen.
    pt.X = 0
    pt.Y = 0
    ClientToScreen picSun.hwnd, pt

    ' Calculate the width and height.
    Dim popup_x As Single
    Dim popup_y As Single
    Dim popup_wid As Single
    Dim popup_hgt As Single
    popup_x = ScaleX(pt.X, vbPixels, vbTwips)
    popup_y = ScaleY(pt.Y, vbPixels, vbTwips)
    popup_wid = ScaleX(picSun.ScaleWidth, picSun.ScaleMode, _
        vbTwips)
    popup_hgt = ScaleY(picSun.ScaleHeight, _
        picSun.ScaleMode, vbTwips)

    ' If this is the right mouse button, add a margin
    ' so it covers a bit more than the PictureBox.
    Const MARGIN As Single = 60 ' Twips.
    If Button = vbRightButton Then
        popup_x = popup_x - MARGIN
        popup_y = popup_y - MARGIN
        popup_wid = popup_wid + 2 * MARGIN
        popup_hgt = popup_hgt + 2 * MARGIN
    End If

    ' Position the popup.
    frmPopup.Move popup_x, popup_y, popup_wid, popup_hgt

    ' Display the popup.
    ShowWindow frmPopup.hwnd, SW_SHOWNOACTIVATE
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated