自定义控件里怎么捕捉到屏幕上位置改变的事件?

神奇的章鱼哥 2017-02-13 04:03:15
可能是点了容器的滚动条,也可能是鼠标中键滚动,也可能拖动窗体,会导致控件在屏幕中位置移动。

自定义控件里什么事件代表在屏幕上位置移动了?

LocationChanged是不行的,因为在容器中相对位置没变,
...全文
317 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
socg 2017-02-15
  • 打赏
  • 举报
回复
这几个事件你都需要绑定,至于容器滚动,你应该会收到刷新事件
larry_wen 2017-02-15
  • 打赏
  • 举报
回复
引用 10 楼 imwjb 的回复:
[quote=引用 4 楼 crystal_lz 的回复:] 还不如直接说要干嘛。。。。
说来搞笑,请人做了一个combobox控件,下拉框控件,结果发现有个问题,这个控件和其他一些控件放在一个固定的panel里时,panel设置为可上下滚动,panel本身位置大小是不变的 然而怪异的事情发生了,当下拉框列表弹开时,如果此时按鼠标中键滚动,这个下拉列表位置不动,一直在那,而这个下拉框那个输入框部分正常滚动到别的地方了,下拉列表不会跟着走。 用鼠标点击滚动条下拉倒不会出现这情况,因为下拉框失去焦点,下拉列表就关闭了 所以现在就想监测到在屏幕上位置变化时把下拉列表关掉得了,然而这个事件监测不到。panel的鼠标中键事件居然也没触发。[/quote] 上面给你的代码应该可以检测到的,你可以试一试先
larry_wen 2017-02-14
  • 打赏
  • 举报
回复
如果你嵌套了多层容器,遍历一下父级,依次绑定
  [ToolboxItem(true)]

    public partial class UserControl1 : UserControl
    {
        Point _location = new Point(0, 0);

        public UserControl1()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();
        }
        ~UserControl1()
        {
            if (Parent != null)
                RemoveBinding(Parent);
        }
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (Parent != null)
                AddBinding(Parent);
        }

        protected virtual void AddBinding(object parent)
        {
            if (parent is Control)
            {
                var cn = (Control)parent;
                cn.LocationChanged += UserControl1_LocationChanged;
                if (cn.Parent != null)
                    AddBinding(cn.Parent);
            }
        }
        protected virtual void RemoveBinding(object parent)
        {
            if (parent is Control)
            {
                var cn = (Control)parent;
                cn.LocationChanged -= UserControl1_LocationChanged;
                if (cn.Parent != null)
                    RemoveBinding(cn.Parent);
            }
        }
        private void UserControl1_LocationChanged(object sender, EventArgs e)
        {
            _location = ((Control)sender).Location;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawString(string.Format("{0},{1}", _location.X, _location.Y), this.Font, Brushes.Black, new Point(this.Width/2, this.Height /2));
        }
        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            _location = this.Location;
            this.Invalidate();
        }
    }
larry_wen 2017-02-14
  • 打赏
  • 举报
回复
首选,如果你的控件在容器里面,滚动滚动条、鼠标中轮,locationchanged是会触发的。
 [ToolboxItem(true)]

    public partial class UserControl1 : UserControl
    {
        Point _location = new Point(0, 0);

        public UserControl1()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            InitializeComponent();
        }
        ~UserControl1()
        {
            if (this.ParentForm != null)
                ParentForm.LocationChanged -= Parentform_LocationChanged;
        }
        private void Parentform_LocationChanged(object sender, EventArgs e)
        {
            _location = ((Form)sender).Location;
            this.Invalidate();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (this.ParentForm != null)
                ParentForm.LocationChanged += Parentform_LocationChanged;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawString(string.Format("{0},{1}", _location.X, _location.Y), this.Font, Brushes.Black, new Point(this.Width/2, this.Height /2));
        }
        protected override void OnLocationChanged(EventArgs e)
        {
            base.OnLocationChanged(e);
            _location = this.Location;
            this.Invalidate();
        }
    }
john_QQ:2335298917 2017-02-14
  • 打赏
  • 举报
回复
对,为什么有这样的需求呢
crystal_lz 2017-02-14
  • 打赏
  • 举报
回复
还不如直接说要干嘛。。。。
神奇的章鱼哥 2017-02-14
  • 打赏
  • 举报
回复
引用 4 楼 crystal_lz 的回复:
还不如直接说要干嘛。。。。
说来搞笑,请人做了一个combobox控件,下拉框控件,结果发现有个问题,这个控件和其他一些控件放在一个固定的panel里时,panel设置为可上下滚动,panel本身位置大小是不变的 然而怪异的事情发生了,当下拉框列表弹开时,如果此时按鼠标中键滚动,这个下拉列表位置不动,一直在那,而这个下拉框那个输入框部分正常滚动到别的地方了,下拉列表不会跟着走。 用鼠标点击滚动条下拉倒不会出现这情况,因为下拉框失去焦点,下拉列表就关闭了 所以现在就想监测到在屏幕上位置变化时把下拉列表关掉得了,然而这个事件监测不到。panel的鼠标中键事件居然也没触发。
神奇的章鱼哥 2017-02-14
  • 打赏
  • 举报
回复
引用 3 楼 larry_wen 的回复:
可以这样做,自定义控件构造函数里获得父级窗口对象,绑定它的locationchanged事件
感谢回复,因为这个控件可能和其他一堆的控件在一个panel里,panel位置和大小不变,里面内容可以上下滚动,这样他的父容器并不会触发locationchanged事件
神奇的章鱼哥 2017-02-14
  • 打赏
  • 举报
回复
引用 2 楼 hanjun0612 的回复:
窗体的事件里就有Move事件
也有可能是所在的容器滚动了,这个move事件触发不了。
larry_wen 2017-02-13
  • 打赏
  • 举报
回复
可以这样做,自定义控件构造函数里获得父级窗口对象,绑定它的locationchanged事件
正怒月神 2017-02-13
  • 打赏
  • 举报
回复
窗体的事件里就有Move事件
cpycpy000 2017-02-13
  • 打赏
  • 举报
回复
拖动窗体的写在窗体的LocationChanged事件 鼠标中键的,写在容器的MouseWheel事件 点击滚动条的话,我不清楚

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧