Multiscreen with VBA - show VBA form on the dedicated monitor

Multiscreen with VBA - show VBA form on the dedicated monitor

I have a multimonitor system (3 monitors) and have created some VBA forms. When I want to call i.e. the VBA form at the thrid monitor the form always appears on the first monitor :confused:

At the multimonitor configuration the monitor are sperated i.e. as follow:
1. monitor: 0 - 1024
2. monitor: 1024 - 2048
3. monitor: 2048 - 3072
To know from which monitor the form has been requested you can get the actual cursor position with using a WinAPI function. Simply speaking when pressing on the button to show the form you know at which monitor you are working and with giving the form an offset in x-direction you can handle this issue.
At the form you want to show you need this setting:
StartUpPosition -> 0 - manual

Below a sample code:
VBA Code:

-------------------------------------------------------------
Public Sub LeftClickUp_Default(obElem As Element) 'this is the VBA Makro which is linked to the LeftClickUp Event of the element (i.e. text button)
Dim NewForm As New MyForm1 'MyForm1 is the name of the UserForm
NewForm.Init obElem 'hand over all properties to the procedure "Init"
obElem.LeftClickUp 'release the button
End Sub
-------------------------------------------------------------

'in the VBA Form you have to insert following code (you also can handle this in a sperate module)
'declare the WinAPI in order to get curser position
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Sub GetMyCurserPosition()
Dim pTargetPoint As POINTAPI
Dim lRetVal As Long
lRetVal = GetCursorPos(pTargetPoint)
'check if the cursor position is at the 1. screen
If pTargetPoint.x < 1024 Then
MyForm1.Left = 400 'place the form on the 1. screen (depends on the width of the form)
'check if the cursor position is at the 2. screen
ElseIf pTargetPoint.x >= 1024 And pTargetPoint.x < 2048 Then
MyForm1.Left = 1400 'place the form on the 2. screen (depends on the width of the form)
'otherwise its on the 3. screen
Else
MyForm1.Left = 2400 'place the form on the 3. screen (depends on the width of the form)
End If
MyForm1.Top = 200 'set the offset from the top of the form (depends on the height of the form)
MyForm1.Show (0)
End Sub
Sub Init(obElem As Element)
Call UserForm_Load
End Sub
'every time the form is loaded the new position is calculated
Private Sub UserForm_Load()
Call GetMyCurserPosition
End Sub


This is a migrated post! Originally posted on 21.08.2007 by user herberto. Please be aware that information can be outdated.

    Disclaimer

    This document governs the use of our Community Forum. By registering and using the platform, you accept these conditions.

    The COPA-DATA Community Forum serves to encourage the exchange of information and experience about the zenon software between forum users respectively zenon users.

    Please mind that any published information on the Community Forum is the subjective opinion and view based on the experience and the level of knowledge of the author. COPA-DATA does not overtake any responsibility for the content and the accuracy of the shared information.

    Users of the Community Forum are encouraged to share only well-founded experiences and to point out any risks associated with the implementation of proposed solutions to problems. COPA-DATA at its absolute discretion, reserves the right to moderate the forum. In this connection COPA-DATA may remove any information containing false facts, potentially dangerous solutions, bad language or content that may insult, degrade or discriminate others. COPA-DATA may block a non-complying user from forum access if the user violated this provision.

    COPA-DATA reserves the right to change this document from time to time at own discretion.


    Ing. Punzenberger COPA-DATA GmbH
    Karolingerstraße 7b · 5020 Salzburg · Austria
    www.copadata.com