在用户定义的控件上添加鼠标事件 [英] Adding Mouse Events on the User defined Controls

查看:33
本文介绍了在用户定义的控件上添加鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI
我有一个用户定义的控件,用于播放视频流.
我想在控件内单击鼠标时将鼠标光标更改为手形符号.
不幸的是,没有为该控件编写任何鼠标事件.
但我想使用MouseMove,MouseDown,MouseUp事件.

我尝试委托方法MouseMove,MouseDown,MouseUp,但我仍然无法在执行事件时触发事件.

我该如何克服呢?

HI
i have a some user defined control,which is used for playing a video stream.
and i want to change the mouse cursor to hand symbol when mouse is clicked inside the control.
unfortunately there is no mouse events written for this control.
but i want to use MouseMove,MouseDown,MouseUp events.

i have tried to delegate the methods MouseMove,MouseDown,MouseUp but still i cound not get the events triggerred when performing the events.

how can i overcome this?

推荐答案

如果您的控件是从Control派生的,则这些事件已经可用,因此我认为这是从UserControl派生的,那么它将不起作用,除非您定义/公开了一些事件并使用子事件来实现它们.

一个简单的方法是:使某些子控件内部或公开,然后用户可以使用子控件的事件.但这会违反正确的封装,并且对设计器将不可用.

因此,更彻底的方法是:假设子级之一是Panel,并且您要将其事件MouseDown公开给Designer.

执行此操作的方法之一:
If your control is derived from Control, these events are already available, so I assume this is derived from UserControl, then it won''t work unless you define/expose some events and implement them using the events of children.

A trivial approach is: make some child controls internal or public, then the user can use the events of a child control. But this would violate proper encapsulation and won''t be available to the Designer.

So, more thorough approach is this: suppose one of the children is Panel and you want to expose its event MouseDown to the Designer.

One of the ways to do this:
using System;
using System.Windows.Forms;

namespace EventsOfUserControl {

    public partial class MyUserControl : UserControl {

        public MyUserControl() {
            InitializeComponent();
            panelMyChild.MouseDown += (sender, eventArgs) => {
                if (PanelKeyDown != null)
                    PanelKeyDown.Invoke(this, eventArgs);
            };
        } //MyUserControl

        public event EventHandler<MouseEventArgs> PanelKeyDown;

    } //class MyUserControl

} //namespace EventsOfUserControl



这段代码是为Forms编写的(再次,标记您的UI库和应用程序类型非常重要),但是该想法可以用于不同的UI库.子控件的预设事件处理程序将引发事件的处理委托给引入的事件PanelKeyDown.您可以将多个子项的事件合并为一个暴露的事件,也可以创建其他任何更复杂的处理.

—SA



This code was written for Forms (again, tagging your UI library and type of application is very important), but this idea can be used for different UI libraries. The preset event handler of a child control delegates processing of a raised event to a introduced event PanelKeyDown. You can combine the events of several children in one exposed event or create any other more complex handling.

—SA


这篇关于在用户定义的控件上添加鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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