saschard
Goto Top

Powershell Mousedown Event

Hallo zusammen,

kennt jemand das Pendat zu diesem Code?:
Private Sub Installer_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
  If (e.Button = Windows.Forms.MouseButtons.Left) Then
    Me.Capture = False
    Me.WndProc(Message.Create(Me.Handle, &HA1, CType(&H2, IntPtr), IntPtr.Zero))
  Else : End If
End Sub
Suche dieselbe Funktion in Powershell (der o.g. Code ist VB).
$mainForm_MouseDown = [System.Windows.Forms.MouseEventHandler] {
  if (condition) {	
  }
Soweit bin ich schon einmal =)

Mit dem VB-Code von oben kann man das Fenster bei einem Mousedown bewegen.

Gruß, Sascha

Content-Key: 239896

Url: https://administrator.de/contentid/239896

Printed on: April 24, 2024 at 21:04 o'clock

Member: colinardo
Solution colinardo Jun 03, 2014 updated at 13:43:11 (UTC)
Goto Top
Hallo Sascha,
kannst du so machen:
$mainForm.add_MouseDown({
  if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left){
        $script:mousedown = $true
        $script:m_offset = new-Object System.Drawing.Point
        $script:m_offset.X = $mainForm.PointToScreen($_.Location).X - $mainForm.Location.X
        $script:m_offset.Y = $mainForm.PointToScreen($_.Location).Y - $mainForm.Location.Y
   }
})
$mainForm.add_MouseUp({
  if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left){
        $script:mousedown = $false
   }
})

$mainForm.add_MouseMove({
    if($script:mousedown){
        $p = new-Object System.Drawing.Point
        $p.X = [System.Windows.Forms.Cursor]::Position.X - $script:m_offset.X
        $p.Y = [System.Windows.Forms.Cursor]::Position.Y - $script:m_offset.Y
        $mainForm.Location = $p
    }
})

hier noch ein komplettes Beispiel, bei dem man die Form bewegen kann wenn man die Maus irgendwo auf der Form gedrückt hält:
(Für den Fall das dir der Zusammenhang fehlt)

function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 272
$System_Drawing_Size.Width = 292
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "Testform"  
$form1.add_MouseDown({
  if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left){
        $script:mousedown = $true
        $script:m_offset = new-Object System.Drawing.Point
        $script:m_offset.X = $form1.PointToScreen($_.Location).X - $form1.Location.X
        $script:m_offset.Y = $form1.PointToScreen($_.Location).Y - $form1.Location.Y
   }
})
$form1.add_MouseUp({
  if ($_.Button -eq [System.Windows.Forms.MouseButtons]::Left){
        $script:mousedown = $false
   }
})

$form1.add_MouseMove({
    if($script:mousedown){
        $p = new-Object System.Drawing.Point
        $p.X = [System.Windows.Forms.Cursor]::Position.X - $script:m_offset.X
        $p.Y = [System.Windows.Forms.Cursor]::Position.Y - $script:m_offset.Y
        $form1.Location = $p
    }
})
#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)


#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

GenerateForm
Grüße Uwe
Member: SaschaRD
SaschaRD Jun 03, 2014 at 13:44:26 (UTC)
Goto Top
Hallo Uwe,

auf einen Beitrag deinerseits habe ich gehofft =)

Dein Code funktioniert einwandfrei.

Vielen Dank und schönen Abend.

Gruß, Sascha