Vertically (only) resizable windows form in C# - c#

I have a situation where it would be beneficial to me to allow my windows form to be resized by the user, but only vertically. After some searching, it seems like there isn't much on this particular subject. Is it possible?

You need to set the form's MinimumSize and MaximumSize properties to two sizes with different heights but equal widths.
If you don't want the horizontal resize cursor to appear at all, you'll need to handle the WM_NCHITTEST message, like this:
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
switch (m.Msg) {
case 0x84: //WM_NCHITTEST
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Left || result == HitTest.Right)
m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.TopRight)
m.Result = new IntPtr((int)HitTest.Top);
if (result == HitTest.BottomLeft || result == HitTest.BottomRight)
m.Result = new IntPtr((int)HitTest.Bottom);
break;
}
}
enum HitTest {
Caption = 2,
Transparent = -1,
Nowhere = 0,
Client = 1,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18
}

Just an idea...
public partial class Form1 : Form {
int _width;
public Form1() {
_width = this.Width;
InitializeComponent();
}
protected override void OnResize(EventArgs e) {
this.Width = _width;
base.OnResize(e);
}
}
EDIT: please note that the min/max size solutions work much better than this hack :)

Set the max & min size for the width of the form only.

Let the FormBorderStyle to Resizable and set MaximumSize and MinimumSize = new Size(this.Width, 0)
Correction:
this.MinimumSize = new Size(this.Width, 0);
this.MaximumSize = new Size(this.Width, Int32.MaxValue);

Yes, it is possible. Just set your form.MinimumSize.Width = form.MaximumSize.Width = 100 (or whatever width you want).

To avoid the "rubber-banding" effect of #orsogufo's solution:
public Form1()
{
InitializeComponent();
this.MinimumSize = new Size(500, 0);
this.MaximumSize = new Size(500, Screen.AllScreens.Max(s => s.Bounds.Height));
}
It won't correctly adjust its maximum height to accommodate a larger screen if you resize the screen bounds, but for static screen sizes it works great.

Related

How to resize/redraw rectangle on paint event?

I have three monitors in the following configuration:
Monitor 1 Monitor 2 Monitor 3
[1280 x 1024] [1200 x 1900] [1280 x 1024]
I use a form to outline a selected monitor (code below), which works great when I initialize or change between Monitors 1 and 3, but results in two rectangles drawn when Monitor 2 is selected :/
I tried to modify the code in many-many different ways, but nothing seems to work. I thought someone might be able to help me understand why two rectangles are being drawn (1280 x 1024 and 1200 x 1900) and how to correct.
Thank you for your time, regards and happy New Year.
P.S. If possible, please keep explanation simple as I am still learning.
public partial class ScreenArea : Form
{
private Pen _pen;
private int screenSelect;
public ScreenArea(int selectScreen = 0)
{
//xInitializeComponent();
TopMost = true;
ShowInTaskbar = true;
FormBorderStyle = FormBorderStyle.None;
BackColor = Color.LightGreen;
TransparencyKey = Color.LightGreen;
_pen = new Pen(Color.Aqua, 5);
Paint += new PaintEventHandler(ScreenArea_Paint);
ScreenSelect = screenSelect;
}
private void ScreenArea_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(_pen, 0, 0, Width, Height);
}
public int ScreenSelect
{
get
{
return screenSelect;
}
set
{
Rectangle screenBounds;
try
{
screenBounds = Screen.AllScreens[value].Bounds;
screenSelect = value;
}
catch (Exception)
{
screenBounds = Screen.AllScreens[screenSelect].Bounds;
}
this.Left = screenBounds.X;
this.Top = screenBounds.Y;
this.Width = screenBounds.Width;
this.Height = screenBounds.Height;
}
}
}

how to set a form in secondary screen [duplicate]

I'm trying to set a Windows Form on secondary monitor, as follows:
private void button1_Click(object sender, EventArgs e)
{
MatrixView n = new MatrixView();
Screen[] screens = Screen.AllScreens;
setFormLocation(n, screens[1]);
n.Show();
}
private void setFormLocation(Form form, Screen screen)
{
// first method
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
// second method
//Point location = screen.Bounds.Location;
//Size size = screen.Bounds.Size;
//form.Left = location.X;
//form.Top = location.Y;
//form.Width = size.Width;
//form.Height = size.Height;
}
The properties of bounds seem correct, but in both methods I've tried, this maximizes the form on the primary monitor. Any ideas?
this.Location = Screen.AllScreens[1].WorkingArea.Location;
this is the Form reference.
Try setting StartPosition parameter as FormStartPosition.Manual inside your SetFormLocation method.
#Gengi's answer is succinct and works well. If the window is maximised it does not move the window. This snippet solves that (although I suspect the windows "normal" dimensions must be smaller than the new screen dimensions for this to work):
void showOnScreen(int screenNumber)
{
Screen[] screens = Screen.AllScreens;
if (screenNumber >= 0 && screenNumber < screens.Length)
{
bool maximised = false;
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
maximised = true;
}
Location = screens[screenNumber].WorkingArea.Location;
if (maximised)
{
WindowState = FormWindowState.Maximized;
}
}
}
Are you sure screens[1] is your secondary? Give screens[0] a try. Your code is basically correct.
Ok, I checked, you will have to do it after the Show():
n.Show();
setFormLocation(n, screens[1]);
which gives some unwanted flicker. But you can probably do:
n.SetBounds(-100, -100, 10, 10); // or similar
n.Show();
setFormLocation(n, screens[1]);
To display form on secondary screen:
Screen primaryFormScreen = Screen.FromControl(primaryForm);
//Use this if you are looking for secondary screen that is not primary
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
//Use this if you are looking for screen that is not being used by specific form
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
//Putting the form on the other screen
secondaryForm.Left = secondaryFormScreen.Bounds.Width;
secondaryForm.Top = secondaryFormScreen.Bounds.Height;
//Recommended to use, You can change it back later to the settings you wish
secondaryForm.StartPosition = FormStartPosition.Manual;
secondaryForm.Location = secondaryFormScreen.Bounds.Location;
Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
secondaryForm.Location = p;
secondaryForm.Show();
If you are looking forward for a specific screen, you can loop on "Screen.AllScreens" and use the process above.
I used this for an XNA 4 Dual Screen Application (Full Screen XNA Game Window + WinForm)
In the Form_Load() method, place the following code:
var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;
this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);
Set form Startup Position property to Manual
public void MoveWindowToProjector ()
{
Rectangle rectMonitor;
// Create New Process
Process objProcess = new Process();
//Get All the screens associated with this Monitor
Screen[] screens = Screen.AllScreens;
// Get Monitor Count
int iMonitorCount = Screen.AllScreens.Length;
// Get Parameters of Current Project
string[] parametros = Environment.GetCommandLineArgs();
// if (parametros.Length > 0)
// {
//objProcess.StartInfo.FileName = parametros[0];
// objProcess.Start();
// }
// Get Window Handle of this Form
IntPtr hWnd = this.Handle;
Thread.Sleep(1000);
if (IsProjectorMode)
{
if (iMonitorCount > 1) // If monitor Count 2 or more
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[1].WorkingArea;
}
else
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
}
else
{
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
if (hWnd != IntPtr.Zero)
{
SetWindowPos(hWnd, 0,
rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
rectMonitor.Height, SWP_SHOWWINDOW);
}
}
This method shows forms on selected screen from left to right:
void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
{
if (forms == null || forms.Length == 0)
return;
var formsCnt = forms.Length;
var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
for (var i = 0; i < formsCnt; i++)
{
var form = forms[i];
if (form == null)
continue;
form.WindowState = FormWindowState.Normal;
form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
form.Size = formSize;
form.BringToFront();
}
}
To solve your problem, you should run:
ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));
Screen[] screens = Screen.AllScreens;
sc aoc = new sc();
aoc.Show();
aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;
FOR MAXIMIZED WINDOW STATE
aoc.WindowState = FormWindowState.Maximized;
FOR ANY X,Y POSITION
aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);

C# Modify Form Size during SizeChanged Event

I'm trying to set the Height of the form while I'm resizing it, if a condition is met. I have it set to only allow the width of the form to be altered manually using the code provided by in this answer.
I have a FlowLayoutPanel displaying a collection of PictureBox controls, each with a fixed Height of 50 pixels. Initially, the form's Height is 38 (Size.Height - ClientSize.Height) + 50 + 6 (Margin.Top + Margin.Bottom of an image) = 94.
If the controls overflow, by default the FlowLayoutPanel pushes them down onto a new line. What I want to do is resize the form when this happens, or when the form width is manually changed which might cause the controls to jump to the next line.
The following code works, and is called whenever a new control is added to the FlowLayoutPanel (itemPanel):
private void ResizeForm()
{
if (itemPanel.Controls.Count < 1) return;
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
// The Form is the correct size, no need to resize it:
if (lastElement.Bottom + lastElement.Margin.Bottom == itemPanel.Height) return;
Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
}
However, when called within my SizeChange event, this method causes the Form to "flash" between the initial Height and the new Height:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
ResizeForm();
}
I'm guessing the reason is because setting Height will fire the SizeChange event again, but I don't know how to resolve this issue. When I print out the values of lastElement.Bottom + lastElement.Margin.Bottom and itemPanel.Height after setting the Height, they are identical, but the code is still somehow reaching that point.
In a nutshell, I want only the form Width to be manually altered, but the Height of the form to change when items are added or the Width is changed, so that all controls inside the FlowLayoutPanel can be viewed.
However, when called within my SizeChange event, this method causes
the Form to "flash" between the initial Height and the new Height
Basically any of the stock "resize" events for your Form will occur too late for you to change the size without it being noticeable.
You'll want to trap the WM_SIZING message:
Sent to a window that the user is resizing. By processing this
message, an application can monitor the size and position of the drag
rectangle and, if needed, change its size or position.
This will allow you to change the size of the Form before it has actually been updated on the screen.
It would look something like this:
public partial class Form1 : Form
{
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
enum HitTest
{
Caption = 2,
Transparent = -1,
Nowhere = 0,
Client = 1,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18
}
private const int WM_SIZING = 0x214;
private const int WM_NCHITTEST = 0x84;
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Top || result == HitTest.Bottom)
m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.BottomLeft)
m.Result = new IntPtr((int)HitTest.Left);
if (result == HitTest.TopRight || result == HitTest.BottomRight)
m.Result = new IntPtr((int)HitTest.Right);
break;
case WM_SIZING:
// Retrieve the "proposed" size of the Form in "rc":
RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
// ... do something with "rc" ...
// this is your code (slightly modified):
if (itemPanel.Controls.Count > 0)
{
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
if (lastElement.Bottom + lastElement.Margin.Bottom != itemPanel.Height)
{
int Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
rc.Bottom = rc.Top + Height; // <--- use "Height" to update the "rc" struct
}
}
// Put the updated "rc" back into message structure:
Marshal.StructureToPtr(rc, m.LParam, true);
break;
}
}
}
Give this a try:
private void ResizeForm()
{
this.SuspendLayout(); // Suspends the layout logic until ResumeLayout() is called (below)
if (itemPanel.Controls.Count < 1) return;
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
// The Form is the correct size, no need to resize it:
if (lastElement.Bottom + lastElement.Margin.Bottom == itemPanel.Height) return;
Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
this.ResumeLayout(); // ADD THIS AS WELL
}

Override resize behavior of winform window

I have a winform window. When I change the size of the screen, the screen immediately increases or decreases.
I would prefer the Resize behavior of the window will be like Split Container, as long as I drag the mouse I see only line that marks what will be the window size, and only in leaving the Resize operation will be made.
I saw several examples that show that by hiding the frame of the window, and then by clicking on the window itself paint frame.
I want that by clicking on the frame of the window(I don't want to hide the frame) and not on the window.
Is there any way to do this? (May override the behavior of the Resize in any way).
I'm pretty sure that you can't find any solution on the Internet. However I've tried a demo for this and it works pretty well.
In winforms and many other UI technologies, you can't render something outside the window itself. To get the effect we want, we have to render some indicative border outside or inside the window depending on how user resizes it. So looks like we're stuck?
BUT there is a kind of technique to do that (I call it layer technique). We need a transparent, non-focused layer to render the indicative border. This layer will have its Size and Location synchronized with the Size and Location (with just a little offset) of the main window. The layer will also be invisible by default and only shows when user resizes and hides when ending resizing.
That's pretty OK with the technique I mentioned. However how to prevent/discard the default resizing when user resizes the window? It's luckily that Win32 supports 2 messages for this to be done easily:
WM_RESIZING : is sent to the window when user starts and keeps resizing. The LParam holds the RECT structure of the current window when resizing. We read this info to render the indicative border correctly. Then we need to modify this RECT to the current Bounds of the window to discard the default resizing effect (the size and location are changed immediately).
WM_EXITSIZEMOVE : is sent to the window when the resizing or moving ends. We need to catch this message to assign the Size and Location of the window based on the Size and Location of the transparent layer and of course hide the layer then.
Now the problem is totally solvable. Here is the demo code I've made. Note that there is a very nasty unsolvable and incomprehensible bug here, it happens when you resize the Top-Left corner, the Size is updated correctly after releasing mouse but the Location is set with an offset. I've debugging but no luck. At some point the Top and Left jumps to unexpected values for no clear reason. However, resizing by all the sides (left, top, right, bottom) and other corners is OK. In fact, resizing by the Top-Left corner is hardly done by user so this solution is acceptable, I think.
//Must add using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Sizing border initialization
SizingBorderWidth = 3;
SizingBorderStyle = DashStyle.Custom;
SizingBorderColor = Color.Orange;
//layer initialization
layer.Owner = this;//especially this one.
layer.Width = Width + SizingBorderWidth * 2;
layer.Height = Height + SizingBorderWidth * 2;
//Paint the border when sizing
layer.Paint += (s, e) => {
using (Pen p = new Pen(SizingBorderColor) { Width = SizingBorderWidth }) {
if (Use3DSizingBorder) {
ControlPaint.DrawBorder3D(e.Graphics, sizingRect.Left, sizingRect.Top, sizingRect.Width, sizingRect.Height, Border3DStyle.Bump, Border3DSide.All);
}
else {
p.DashStyle = SizingBorderStyle;
p.LineJoin = LineJoin.Round;
if(p.DashStyle == DashStyle.Custom)
p.DashPattern = new float[] { 8f, 1f, 1f, 1f };//length of each dash from right to left
e.Graphics.DrawRectangle(p, sizingRect);
}
}
};
//Bind the Location of the main form and the layer form together
LocationChanged += (s, e) => {
Point p = Location;
p.Offset(-SizingBorderWidth, -SizingBorderWidth);
layer.Location = p;
};
//Set the intial Location of layer
Load += (s, e) =>{
Point p = Location;
p.Offset(-SizingBorderWidth, -SizingBorderWidth);
layer.Location = p;
};
}
//Set this to true to use 3D indicative/preview border
public bool Use3DSizingBorder { get; set; }
//Change the indicative/preview border thickness
public int SizingBorderWidth { get; set; }
//Change the indicative/preview border style
public DashStyle SizingBorderStyle { get; set; }
//Change the indicative/preview border color
public Color SizingBorderColor { get; set; }
//hold the current sizing Rectangle
Rectangle sizingRect;
bool startSizing;
bool suppressSizing;
//This is a Win32 RECT struct (don't use Rectangle)
public struct RECT
{
public int left, top, right, bottom;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x214&&!suppressSizing)//WM_SIZING = 0x214
{
RECT rect = (RECT) m.GetLParam(typeof(RECT));
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
sizingRect = new Rectangle() {X = SizingBorderWidth/2, Y = SizingBorderWidth/2,
Width = w, Height = h};
layer.Left = rect.left-SizingBorderWidth;
layer.Top = rect.top-SizingBorderWidth;
layer.Width = w+2*SizingBorderWidth;
layer.Height = h+2*SizingBorderWidth;
if (!startSizing)
{
layer.Show();
startSizing = true;
}
layer.Invalidate();
//Keep the current position and size fixed
rect.right = Right;
rect.bottom = Bottom;
rect.top = Top;
rect.left = Left;
//---------------------------
Marshal.StructureToPtr(rect, m.LParam, true);
}
if (m.Msg == 0x232)//WM_EXITSIZEMOVE = 0x232
{
layer.Visible = false;
BeginInvoke((Action)(() => {
suppressSizing = true;
Left = layer.Left + SizingBorderWidth;
Top = layer.Top + SizingBorderWidth;
Width = layer.Width - 2 * SizingBorderWidth;
Height = layer.Height - SizingBorderWidth * 2;
suppressSizing = false;
}));
startSizing = false;
}
base.WndProc(ref m);
}
//Here is the layer I mentioned before.
NoActivationForm layer = new NoActivationForm();
}
public class NoActivationForm : Form {
public NoActivationForm() {
//The following initialization is very important
TransparencyKey = BackColor;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
//----------------------------------------------
}
protected override bool ShowWithoutActivation {
get { return true; }
}
}
Some screen shots:
EDIT: (This edit was suggested by Hodaya Shalom, the OP (weird :)
I found a solution to the left corner problem :
before the BeginInvoke I save the variables and in the invoke I put the local variable:
int _top = layer.Top + SizingBorderWidth;
int _left = layer.Left + SizingBorderWidth;
int _width = layer.Width - 2 * SizingBorderWidth;
int _height = layer.Height - SizingBorderWidth * 2;
BeginInvoke((Action)(() => {
suppressSizing = true;
Left = _left;
Top = _top;
Width =_width;
Height =_height;
suppressSizing = false;
}));

Showing a Windows form on a secondary monitor?

I'm trying to set a Windows Form on secondary monitor, as follows:
private void button1_Click(object sender, EventArgs e)
{
MatrixView n = new MatrixView();
Screen[] screens = Screen.AllScreens;
setFormLocation(n, screens[1]);
n.Show();
}
private void setFormLocation(Form form, Screen screen)
{
// first method
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
// second method
//Point location = screen.Bounds.Location;
//Size size = screen.Bounds.Size;
//form.Left = location.X;
//form.Top = location.Y;
//form.Width = size.Width;
//form.Height = size.Height;
}
The properties of bounds seem correct, but in both methods I've tried, this maximizes the form on the primary monitor. Any ideas?
this.Location = Screen.AllScreens[1].WorkingArea.Location;
this is the Form reference.
Try setting StartPosition parameter as FormStartPosition.Manual inside your SetFormLocation method.
#Gengi's answer is succinct and works well. If the window is maximised it does not move the window. This snippet solves that (although I suspect the windows "normal" dimensions must be smaller than the new screen dimensions for this to work):
void showOnScreen(int screenNumber)
{
Screen[] screens = Screen.AllScreens;
if (screenNumber >= 0 && screenNumber < screens.Length)
{
bool maximised = false;
if (WindowState == FormWindowState.Maximized)
{
WindowState = FormWindowState.Normal;
maximised = true;
}
Location = screens[screenNumber].WorkingArea.Location;
if (maximised)
{
WindowState = FormWindowState.Maximized;
}
}
}
Are you sure screens[1] is your secondary? Give screens[0] a try. Your code is basically correct.
Ok, I checked, you will have to do it after the Show():
n.Show();
setFormLocation(n, screens[1]);
which gives some unwanted flicker. But you can probably do:
n.SetBounds(-100, -100, 10, 10); // or similar
n.Show();
setFormLocation(n, screens[1]);
To display form on secondary screen:
Screen primaryFormScreen = Screen.FromControl(primaryForm);
//Use this if you are looking for secondary screen that is not primary
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen;
//Use this if you are looking for screen that is not being used by specific form
Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen;
//Putting the form on the other screen
secondaryForm.Left = secondaryFormScreen.Bounds.Width;
secondaryForm.Top = secondaryFormScreen.Bounds.Height;
//Recommended to use, You can change it back later to the settings you wish
secondaryForm.StartPosition = FormStartPosition.Manual;
secondaryForm.Location = secondaryFormScreen.Bounds.Location;
Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y);
secondaryForm.Location = p;
secondaryForm.Show();
If you are looking forward for a specific screen, you can loop on "Screen.AllScreens" and use the process above.
I used this for an XNA 4 Dual Screen Application (Full Screen XNA Game Window + WinForm)
In the Form_Load() method, place the following code:
var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay;
this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width / 2) - (this.Size.Width / 2);
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height / 2) - (this.Size.Height / 2);
Set form Startup Position property to Manual
public void MoveWindowToProjector ()
{
Rectangle rectMonitor;
// Create New Process
Process objProcess = new Process();
//Get All the screens associated with this Monitor
Screen[] screens = Screen.AllScreens;
// Get Monitor Count
int iMonitorCount = Screen.AllScreens.Length;
// Get Parameters of Current Project
string[] parametros = Environment.GetCommandLineArgs();
// if (parametros.Length > 0)
// {
//objProcess.StartInfo.FileName = parametros[0];
// objProcess.Start();
// }
// Get Window Handle of this Form
IntPtr hWnd = this.Handle;
Thread.Sleep(1000);
if (IsProjectorMode)
{
if (iMonitorCount > 1) // If monitor Count 2 or more
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[1].WorkingArea;
}
else
{
//Get the Dimension of the monitor
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
}
else
{
rectMonitor = Screen.AllScreens[0].WorkingArea;
}
if (hWnd != IntPtr.Zero)
{
SetWindowPos(hWnd, 0,
rectMonitor.Left, rectMonitor.Top, rectMonitor.Width,
rectMonitor.Height, SWP_SHOWWINDOW);
}
}
This method shows forms on selected screen from left to right:
void ShowFormsOnScreenLeftToRight(Screen screen, params Form[] forms)
{
if (forms == null || forms.Length == 0)
return;
var formsCnt = forms.Length;
var formSize = new Size(screen.WorkingArea.Size.Width / formsCnt, screen.WorkingArea.Size.Height);
for (var i = 0; i < formsCnt; i++)
{
var form = forms[i];
if (form == null)
continue;
form.WindowState = FormWindowState.Normal;
form.Location = new Point(screen.WorkingArea.Left + screen.WorkingArea.Size.Width / formsCnt * i, 0);
form.Size = formSize;
form.BringToFront();
}
}
To solve your problem, you should run:
ShowFormsOnScreenLeftToRight(n, Screen.AllScreens.First(s => !s.Primary));
Screen[] screens = Screen.AllScreens;
sc aoc = new sc();
aoc.Show();
aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location;
FOR MAXIMIZED WINDOW STATE
aoc.WindowState = FormWindowState.Maximized;
FOR ANY X,Y POSITION
aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION);

Categories

Resources