将两个控件链接为一

发布于 2024-08-15 03:41:28 字数 2809 浏览 4 评论 0原文

我想知道是否有可能(视觉上和功能上)链接两个控件(组件)? (.NET2)

简化一下,我有两个标签 - 其中一个是主标签(可以用鼠标替换),另一个是描述标签 - 它需要在指定距离上跟随主标签。

此外,描述标签应该能够响应事件,例如鼠标单击等。 也许可以使用 UserControl 但在标签之间我需要有一个“透明”空间。

谢谢。

==编辑1==

我也可以不创建第二个标签控件,而只使用永恒的工具提示。在这种情况下,我想知道无限时间显示它的可能性以及检测工具提示上的点击的可能性。

无论如何,如果我单击标签或工具提示,我将需要向用户显示一个 TextBox 控件(而不是工具提示或标签),以便它能够修改显示的描述(实际上显示时间)

==编辑2==

替代文本http://lh4.ggpht.com/_1TPOP7DzY1E/Sy9Mk8-Z-xI/AAAAAAAACzo/-5huzSd59j4/s800/UserControl.png

这是我的“透明”用户控件设计

替代文本 http://lh5.ggpht.com/_1TPOP7DzY1E/Sy9MlM31jUI/AAAAAAAACzs/xIJ0 hcgOzwo/ s800/UserControlForm.png

这是我处于运行模式的表单(用户控件“透明”区域覆盖按钮)。

这是用户控件的代码:

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

namespace WindowsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020;//WS_EX_TRANSPARENT
                return cp;
            }
        }

        private int opacity;
        public int Opacity
        {
            get { return opacity; }
            set
            {
                opacity = value;
                this.InvalidateEx();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color bk = Color.FromArgb(Opacity, this.BackColor);
            e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);
        }

        protected void InvalidateEx()
        {
            if (Parent == null)
                return;
            Rectangle rc = new Rectangle(this.Location, this.Size);
            Parent.Invalidate(rc, true);
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = this.Location + (Size)e.Location;
            }
        }

        Point cursorDownPoint = Point.Empty;
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            cursorDownPoint = label1.PointToScreen(e.Location);
        }
    }
}

=================

* 描述有点简化。在我的真实案例中,我有一个自定义圆形点组件(:来自 Microsoft.VisualBasic.PowerPacks.OvalShape)。该点代表时间位置的对象 - 在链接标签中我需要指定该点的时间。用户将能够通过单击时间标签来修改点的时间。

I wonder if there is a possibility to (visually and functionally) link two controls(components)? (.NET2)

Simplifying the things, I have two labels - one of them is the main label (it can be deplaced with the mouse) and an other - the description label - it needs to follow the main label on a specified distance.

Also, the description label should be able to respond to the events, like mouse click etc.
Maybe there is a possibility to use a UserControl but between the labels I need to be a "transparent" space.

Thanks.

==EDIT 1==

I could also, instead of creating the second label control, just use a eternal toolTip. In this case i wonder about possibility of displaying it the Infinite time AND also possibility to detect the click on the tooltip.

Anyway, If I click on the label or tooltip, I will need to display to the user a TextBox control(instead of the tooltip or label), in order that it be able to Modify the displayed description (in fact displaying time)

== EDIT 2 ==

alt text http://lh4.ggpht.com/_1TPOP7DzY1E/Sy9Mk8-Z-xI/AAAAAAAACzo/-5huzSd59j4/s800/UserControl.png

this is my "transperent" UserControl design

alt text http://lh5.ggpht.com/_1TPOP7DzY1E/Sy9MlM31jUI/AAAAAAAACzs/xIJ0hcgOzwo/s800/UserControlForm.png

and this is my Form in running mode(user control "transparent" region covering a button).

this is the usercontrol's code:

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

namespace WindowsControlLibrary1
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020;//WS_EX_TRANSPARENT
                return cp;
            }
        }

        private int opacity;
        public int Opacity
        {
            get { return opacity; }
            set
            {
                opacity = value;
                this.InvalidateEx();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color bk = Color.FromArgb(Opacity, this.BackColor);
            e.Graphics.FillRectangle(new SolidBrush(bk), e.ClipRectangle);
        }

        protected void InvalidateEx()
        {
            if (Parent == null)
                return;
            Rectangle rc = new Rectangle(this.Location, this.Size);
            Parent.Invalidate(rc, true);
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = this.Location + (Size)e.Location;
            }
        }

        Point cursorDownPoint = Point.Empty;
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            cursorDownPoint = label1.PointToScreen(e.Location);
        }
    }
}

=================

* The description was a little simplified. In my real case I have a custom circular point component (: from Microsoft.VisualBasic.PowerPacks.OvalShape). The point represents a object in time position - in the linked label I need to specify the point's time. User will be able to modify the point's time by clicking on the time label.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-08-22 03:41:28

创建用户控件。概述了透明度问题的解决方法 这里

Create a User Control. A workaround for the transparency issue is outlined here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文