Results 1 to 11 of 11

Thread: [RESOLVED] How to put form in specified coordinates on other application window?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Resolved [RESOLVED] How to put form in specified coordinates on other application window?

    How to put form in specified coordinates on other application window?

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to put form in specified coordinates on other application window?

    You'll need two api function: FindWindow and SetParent.
    Here is to make your VB form child of a Notepad:
    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Declare Function SetParent Lib "user32" _
        (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    
    
    Private Sub Command1_Click()
    Dim parentHwnd As Long
    
        parentHwnd = FindWindow(vbNullString, "Untitled - Notepad")
        
        SetParent Me.hwnd, parentHwnd
        Me.Move 0, 0
        Me.Refresh
    
    End Sub
    Note: if you don't see your form then click notepad window somewhere in the top left corner.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to put form in specified coordinates on other application window?

    Everything looks fine except one thing, if I want to change form position on window I have to use really big values in Me.Move and this values don't reflect real dimensions of window, why?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to put form in specified coordinates on other application window?

    Also with different screen resolutions form is in different places on the window even if Me.Move is the same, how can I avoid it?

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to put form in specified coordinates on other application window?

    Try using MoveWindow api but that's essentially the same as Me.Move:
    Code:
    Private Declare Function MoveWindow Lib "user32" 
        (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, 
        ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to put form in specified coordinates on other application window?

    It will be 15 times smaller scale than 1 pixel (width and height).
    Code:
    Me.Move 1, 1
    And here will be 1 pixel (width and height).
    Code:
    Me.Move 1 * 15 , 1 * 15
    I'm right?

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to put form in specified coordinates on other application window?

    Yes and No. VB uses Twips by default so unless you change ScaleMode to 3 (pixels) nothing is going to change anyway (Me.Move will still use twips).
    Windows api use Pixels so if you want to use pixels then like I said try using MoveWindow:
    Code:
    Option Explicit
    
    Private Declare Function MoveWindow Lib "user32" _
        (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
        ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    
    Private Sub Form_Load()
        MoveWindow Me.hwnd, _
                   1000 / Screen.TwipsPerPixelX, _
                   1000 / Screen.TwipsPerPixelY, _
                   Me.Width / Screen.TwipsPerPixelX, _
                   Me.Height / Screen.TwipsPerPixelY, 1
    End Sub
    "Play" with X/Y/width/height to see the difference.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to put form in specified coordinates on other application window?

    Quote Originally Posted by Lauriux1
    It will be 15 times smaller scale than 1 pixel (width and height).
    Code:
    Me.Move 1, 1
    And here will be 1 pixel (width and height).
    Code:
    Me.Move 1 * 15 , 1 * 15
    I'm right?
    In most cases it will be right, but not always.

    While there are usually 15 twips per pixel, there are some things that can change it - such as the monitor in use, and the display settings. There are even occasions where the amount vertically is different to the amount horizontally.

    Luckily there is an easy way to detect it, using Screen.TwipsPerPixelX and Screen.TwipsPerPixelY as RhinoBull used, so a safe version of your code would be:
    Code:
    Me.Move 1 * Screen.TwipsPerPixelX , 1 * Screen.TwipsPerPixelY
    Alternatively you can also use the ScaleX and ScaleY methods of a form.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How to put form in specified coordinates on other application window?

    But form's Move method uses Twips anyway so instead of adjusting form position by 15 pixels (1 * 15) it will adjust it by 15 twips.
    If you want to use pixels then [as I already mentioned] use MoveWindow api function instead.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to put form in specified coordinates on other application window?

    I know, I was just pointing out the correction to Lauriux1's code so that (if wanted) the .Move method can be used with a pixel value (1 * Screen.TwipsPerPixelX will be 1 pixel).

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to put form in specified coordinates on other application window?

    Thanks RhinoBull and si_the_geek

    Solved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width