|
-
Feb 28th, 2009, 01:55 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to put form in specified coordinates on other application window?
How to put form in specified coordinates on other application window?
-
Feb 28th, 2009, 06:17 PM
#2
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.
-
Mar 2nd, 2009, 10:27 AM
#3
Thread Starter
Hyperactive Member
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?
-
Mar 2nd, 2009, 01:46 PM
#4
Thread Starter
Hyperactive Member
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?
-
Mar 2nd, 2009, 02:19 PM
#5
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
-
Mar 2nd, 2009, 03:23 PM
#6
Thread Starter
Hyperactive Member
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?
-
Mar 2nd, 2009, 04:03 PM
#7
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.
Last edited by RhinoBull; Mar 2nd, 2009 at 04:06 PM.
-
Mar 3rd, 2009, 07:10 AM
#8
Re: How to put form in specified coordinates on other application window?
 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.
-
Mar 3rd, 2009, 08:47 AM
#9
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.
-
Mar 3rd, 2009, 09:21 AM
#10
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).
-
Mar 3rd, 2009, 10:18 AM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|