Lock Form1& Form2在一起 [英] Lock Form1 & Form2 together

查看:95
本文介绍了Lock Form1& Form2在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的程序时,会出现两个表单。

Form1和Form2。

现在我需要的是将form1的位置移动到屏幕的任何位置通过鼠标,form2移动到form1去的地方。

我的意思是我需要Form2才能锁定Form1。

如何????



谢谢。

When I run my program, two forms comes up.
Form1 and Form2.
Now what I need is when I move the placement of form1 to anywhere of screen via mouse, form2 moves where ever form1 goes.
I mean I need Form2 to be lock on Form1.
How????

Thanks.

推荐答案

看看这里:如何:设置Windows窗体的屏幕位置 [ ^ ]

使用表格事件 [ ^ ]: LocationChanged [ ^ ];)
Have a look here: How to: Set the Screen Location of Windows Forms[^]
Use form's event[^]: LocationChanged[^] ;)


你不能做的一件事常规的WinForms(没有进入低级窗口事件并使用平台Invoike)是对一个Form进行持续(增量)跟踪移动,而另一个Form通过在TitleBar上单击拖动来移动:这与其固有的行为有关TitleBar区域。



此处显示的示例处理您希望跟踪的表单可能会更改的情况......并重叠其后的表单...两种方式:



1.通过点击拖动表格的标题栏,或者在代码中更改表格的位置。



2.由于表格的大小通过直接行动......或代码更改。



One thing you can't do in "regular" WinForms (without getting into low-level Window Events and using Platform Invoike) is to have constant (incremental) tracking of one Form moving as another moves by click-dragging on the TitleBar: this has to do with the innate behavior of the TitleBar area.

The example shown here handles the case that the Form you wish tracked may change ... and overlap the Form that follows it ... in either of two ways:

1. As the Location of the Form is changed by click-dragging the TitleBar of the Form ... or, in code.

2. As the Size of the Form is changed by direct-action ... or, in code.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Form2 f2 = new Form2();

        private void Form1_Load(object sender, EventArgs e)
        {
            f2.Show();

            // the Form2 instance will use the same EventHandlers
            // that the Form1 instance (Main Form) uses
            f2.LocationChanged += Form1_SizeAndLocationChanged;
            f2.SizeChanged += Form1_SizeAndLocationChanged;

            // need to adjust the position on Application start
            // note: you could just as well call this with
            // both arguments being 'null
            Form1_SizeAndLocationChanged(sender, e);
        }

        // convenience method
        private void PositionForm2(int fLeft, int fTop)
        {
            f2.Location = new Point(fLeft, fTop);
        }

        // Both the Form1 instance LocationChanged,
        // and SizeChanged Events use this EventHandler
        private void Form1_SizeAndLocationChanged(object sender, EventArgs e)
        {
            // experiment using one of these method calls

            // Form2 left, top docked Form1 right, top
            PositionForm2(this.Right, this.Top);

            // Form2 right, top docked Form1 left, top
            //PositionForm2(this.Left - f2.Width, this.Top);

            // Form2 left, top docked Form1 right, Bottom
            // PositionForm2(this.Right, this.Bottom);

            // Form2 right, top docked Form1 left, bottom
            //PositionForm2(this.Left - f2.Width, this.Bottom);
        }

        // try experimenting with this with the code in
        // Form1_SizeAndLocationChanged commented out
        // and observe the differences 
        //private void Form1_MouseMove(object sender, MouseEventArgs e)
        //{
            //Form1_SizeAndLocationChanged(sender, e);
        //}
    }
}

讨论:



1。此代码要求您在设计时将Form1 LocationChanged和SizeChanged事件的EventHandlers分配给Form1_SizeAndLocationChanged方法。



2.显示的简单示例存在一些问题这里应该解决生产质量代码:



a。如果目标是从不将Form2的任何部分移出屏幕边界怎么办?



b。如果Form1被最小化或最大化会发生什么?

Discussion:

1. This code requires you assign at design-time the EventHandlers for both Form1 LocationChanged and SizeChanged Events to the Form1_SizeAndLocationChanged method.

2. There are some issues with the simple example shown here that should be addressed for production quality code:

a. What if the goal is to never have any part of Form2 moved outside the Screen boundaries ?

b. What should happen if Form1 is Minimized, or Maximized ?


通过Delegate.I认为通过委托移动一个表单的方式,修改另一个表单的位置
By Delegate.I think the way through the commission to move when one form, modify the position of another form


这篇关于Lock Form1& Form2在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆