[RESOLVED] How to put form in specified coordinates on other application window?
How to put form in specified coordinates on other application window?
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.
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?
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?
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
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).
And here will be 1 pixel (width and height).
Code:
Me.Move 1 * 15 , 1 * 15
I'm right?
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.
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).
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.
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.
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).
Re: How to put form in specified coordinates on other application window?
Thanks RhinoBull and si_the_geek :thumb:
Solved! :afrog: