Borderless Form - how to move with Mouse1?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Borderless Form - how to move with Mouse1?

Post by ALIENQuake »

Hello,

does anyone have code example how to move Borderless Form when Mouse1 is pressed? I want to have simple borderless window with 4 labes (Start, Readme, Homepage, Exit) on it and ability to move all form with mouse, that's all.

Any advice?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Borderless Form - how to move with Mouse1?

Post by davidc »

It will require some C# code but it can be done:

https://www.codeproject.com/Articles/11 ... lebar-in-C

You will need to use Add-Type to generate the code for the necessary calls to the Windows API.
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Borderless Form - how to move with Mouse1?

Post by jvierra »

Here is some old code that does the moving. You can add it as a "snippet" and insert whenever needed.

Code: Select all

Add-Type  @'
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

public class User32{
	//const and dll functions for moving form
	public const int WM_NCLBUTTONDOWN = 0xA1;
	public const int HT_CAPTION = 0x2;

	[DllImport("user32.dll")]
	public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
	
	[DllImport("user32.dll")]
	public static extern bool ReleaseCapture();
}
'@



$form1_Load={
	
}

$form1_MouseDown=[System.Windows.Forms.MouseEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    [user32]::ReleaseCapture()
    [user32]::SendMessage($form1.Handle, [user32]::WM_NCLBUTTONDOWN, [user32]::HT_CAPTION, 0);
}
User avatar
ALIENQuake
Posts: 112
Last visit: Mon Jan 29, 2024 7:35 am
Has voted: 4 times

Re: Borderless Form - how to move with Mouse1?

Post by ALIENQuake »

Thank you! It's working.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked