Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使windows窗体的四个角在桌面上始终可见?_C#_Winforms - Fatal编程技术网

C# 如何使windows窗体的四个角在桌面上始终可见?

C# 如何使windows窗体的四个角在桌面上始终可见?,c#,winforms,C#,Winforms,我有一个windows窗体,我正在使用它作为桌面应用程序。 现在,我希望在拖动表单时,表单不应超出桌面边界。 我知道整个窗口不会消失,但我想显示所有四个角。 我已经设置了“border style=Fixed Tool window”,并编写了以编程方式移动表单的代码 因此,与此相反: ---------------------- ! ! ! --------------- ! ! ! !

我有一个windows窗体,我正在使用它作为桌面应用程序。 现在,我希望在拖动表单时,表单不应超出桌面边界。 我知道整个窗口不会消失,但我想显示所有四个角。 我已经设置了“border style=Fixed Tool window”,并编写了以编程方式移动表单的代码

因此,与此相反:

---------------------- ! ! ! --------------- ! ! ! ! ! ! ! --------------- ! ! ---------------------- ---------------------- ! ! ! --------------- ! ! ! ! ! ! ! --------------- ! ! ---------------------- 我想要这个:

------------------------ ! ! ! -------------! ! ! !! ! ! !! ! -------------! ! ! ------------------------ ------------------------ ! ! ! -------------! ! ! !! ! ! !! ! -------------! ! ! ------------------------
如果我理解这个问题,那就好了

是否要避免窗口(winforms MainForm)不离开屏幕? 如果是这种情况,您不能在窗体的事件移动中处理它吗 移动时,检查左上角的属性,如果这些属性变为负数,则返回方法。 若你们知道你们的表格有多大,你们的分辨率,你们可以计算出右边和底部

private void Move(object sender, EventArgs e)
    {
        var f = sender as Form;

        var l = f.Left;
        var t = f.Top;

        var h = f.Height;
        var w = f.Width;
        var sh = Screen.GetWorkingArea(this).Height;
        var sw = Screen.GetWorkingArea(this).Width;
        if(t<0 || t+h > sh) return;
        if (l < 0 || l+w > sw) return;
    }
private void移动(对象发送方,事件参数e)
{
var f=发送方作为表单;
var l=左f;
var t=f.顶部;
var h=f.高度;
var w=f.宽度;
var sh=屏幕.GetWorkingArea(this).Height;
var sw=屏幕.GetWorkingArea(此).Width;
如果(t sh)返回;
如果(l<0 | | l+w>sw)返回;
}

像这样的未测试。

将表单与边界进行比较

例如:

    private void Form1_Move(object sender, EventArgs e)
    {
        KeepBounds();
    }

    private void KeepBounds()
    {
        if (this.Left < SystemInformation.VirtualScreen.Left)
            this.Left = SystemInformation.VirtualScreen.Left;

        if (this.Right > SystemInformation.VirtualScreen.Right)
            this.Left = SystemInformation.VirtualScreen.Right - this.Width;

        if (this.Top < SystemInformation.VirtualScreen.Top)
            this.Top = SystemInformation.VirtualScreen.Top;

        if (this.Bottom > SystemInformation.VirtualScreen.Bottom)
            this.Top = SystemInformation.VirtualScreen.Bottom - this.Height;
    }
private void Form1\u Move(对象发送方,事件参数e)
{
KeepBounds();
}
私有void KeepBounds()
{
if(this.LeftSystemInformation.VirtualScreen.Right)
this.Left=SystemInformation.VirtualScreen.Right—this.Width;
if(this.TopSystemInformation.VirtualScreen.Bottom)
this.Top=SystemInformation.VirtualScreen.Bottom—this.Height;
}
这将在屏幕中保留窗体的“四角”

您可以使用该事件并将其与
屏幕进行比较。所有屏幕[0]。边界
这是主监视器,如果您有多个监视器,则可以更改索引以选择将窗体限制到哪个屏幕

private void Form1_LocationChanged(object sender, EventArgs e)
{
    if ((this.Left + this.Width) > Screen.AllScreens[0].Bounds.Width)
        this.Left = Screen.AllScreens[0].Bounds.Width - this.Width;

    if (this.Left  < Screen.AllScreens[0].Bounds.Left)
        this.Left = Screen.AllScreens[0].Bounds.Left;

    if ((this.Top + this.Height) > Screen.AllScreens[0].Bounds.Height)
        this.Top = Screen.AllScreens[0].Bounds.Height - this.Height;

    if (this.Top < Screen.AllScreens[0].Bounds.Top)
        this.Top = Screen.AllScreens[0].Bounds.Top;
}
private void Form1\u LocationChanged(对象发送方,事件参数e)
{
if((this.Left+this.Width)>Screen.AllScreens[0].Bounds.Width)
this.Left=Screen.AllScreens[0].Bounds.Width-this.Width;
if(this.LeftScreen.AllScreens[0].Bounds.Height)
this.Top=Screen.AllScreens[0].Bounds.Height—this.Height;
if(this.Top
图像显示不正确,请避免它们。谢谢。我在这里看到的是什么?我不同意按照用户的期望更改Windows的行为。通常,用户可以将窗口滑离屏幕(或将其移动到另一个屏幕),更改该行为可能会让用户感到不安。因此,除非有很好的理由在windows上更改windows的预期行为,否则我不会这样做。对于多个监视器,可以使用
Screen.FromHandle(Handle)
使用窗口当前所在的屏幕。@MarkHall这是winforms应用程序中“我的表单对象是静态的”时出现的堆栈流异常。@BurningFire确保您没有递归调用该函数。@MarkHall但它将被递归调用,因为即使向左也会被设置为值此Form1\u LocationChanged方法将被调用。