本文介绍了如何在桌面上始终保持窗体的四个角可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体,用作桌面应用程序.现在我希望表单在拖动时不应超出桌面边框.我知道整个窗口不会消失,但我想显示所有四个角.我已经设置了边框样式 = 固定工具窗口",并编码为以编程方式移动表单.

所以不是这样:

---------------!!!---------------!!!!!!!---------------!!---------------

我想要这个:

------------------------!!!-------------!!!!!!!!!!-------------!!!------------------------
解决方案

您可以使用 LocationChanged 事件并将其与 Screen.AllScreens[0].Bounds 进行比较,这是主监视器,如果您有多个显示器,您可以更改然后索引以选择您将表单限制到哪个屏幕.

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 

I have a windows form which I am using as a desktop application.Now I want that the form should not go outside desktop borders when I drag it.I know that the whole window doesnt disappear but I want to display all four corners.I have set the "border style = Fixed Tool window",and coded to move form programmatically.

So instead of this:

----------------------
!                    !
!          ---------------
!          !             !
!          !             !
!          ---------------
!                    !
----------------------

I want this:


------------------------
!                      !
!         -------------!
!         !           !!
!         !           !!
!         -------------!
!                      !
------------------------
解决方案

You can use the LocationChanged event and compare it to Screen.AllScreens[0].Bounds this is the primary monitor, if you have multiple monitors you could change then index to select which screen you limit your form to.

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;
}

这篇关于如何在桌面上始终保持窗体的四个角可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:39