1

自分のプロパティなどを追加できるように、継承できるNotifyIconを作成しようとしています。他の誰かが書いたコンポーネントクラスを見ると、以下に示すようにある程度の進歩があり、コンポーネントをフォームにドラッグできます。正直なところ、私は自分が何をしているのかほとんどわかりません。インターネット上のどこにも、まったく役に立たないチュートリアルはないようです。

表示されているPrepareIcon方法では、デザイナーのデフォルトの。から値を変更しようとしましたが、ポップアップするメッセージボックスは空白で表示されますnotifyIconInheritable1。デザイナーにいる間にNotifyIconが表示されるのを見たので、これがどのように機能するかについて完全に混乱しました。

質問は; これの何が問題なのか、何が間違っているのか、そして私は時間を無駄にしているので、これを試みるべきではありませんか?

namespace AwesomeNotifyIcon
{
    [DefaultProperty("Text"), DefaultEvent("Click"), Description("Displays an icon in the notification area, on the right side of the Windows taskbar, during run time")]
    public partial class NotifyIconInheritable : Component
    {
        private NotifyIcon notifyIcon;

        public NotifyIconInheritable()
        {
            //PrepareIcon();
            InitializeComponent();
        }

        public NotifyIconInheritable(IContainer container)
        {
            if (container != null)
            {
                container.Add(this);
            }
            PrepareIcon();
            InitializeComponent();
        }

        [Category("Appearance"), Description("The icon to associate with the balloon ToolTip."), DefaultValue(ToolTipIcon.None)]
        public ToolTipIcon BalloonTipIcon { get; set; }

        [Category("Appearance"), Description("The text to associate with the balloon ToolTip.")]
        public string BalloonTipText { get; set; }

        [Category("Appearance"), Description("The title of the balloon ToolTip.")]
        public string BalloonTipTitle { get; set; }

        [Category("Appearance"), Description("The icon to display in the system tray.")]
        public Icon Icon { get; set; }

        [Category("Appearance"), Description("The text that will be displayed when the mouse hovers over the icon.")]
        public string Text { get; set; }

        [Category("Behaviour"), Description("The shortcut menu to show when the user right-clicks the icon.")]
        public ContextMenuStrip ContextMenuStrip { get; set; }

        [Category("Behaviour"), Description("Determines whether the control is visible or hidden."), DefaultValue(false)]
        public bool Visible { get; set; }

        [Category("Data"), Description("User-defined data associated with the object.")]
        public object Tag { get; set; }

        [Category("Action"), Description("Occurs when the component is clicked.")]
        public event EventHandler Click;

        private void PrepareIcon()
        {
            notifyIcon = new NotifyIcon();
            notifyIcon.Dispose();
            MessageBox.Show(this.Text);

            if (Click != null)
                notifyIcon.Click += Click;
        }
    }
}

デザイナに表示されるプロパティは次のとおりです。

http://cl.ly/1vIF/content http://cl.ly/1vIF/content

4

1 に答える 1

3

デザイナを使用してコントロールを追加すると、次のようなコードが生成されます。

NotifyIconInheritable icon = new NotifyIconInheritable();
icon.Text = "Some text";
parent.Controls.Add(icon);

コンストラクターが実行されるまでプロパティが設定されていないことがわかります。したがって、を呼び出した時点ではPrepareIconText実際にはnullです。

Dispose()その他のアドバイス:ヘンクが言うように、コードのこの時点で実際に呼び出すべきではありません。また、コンストラクターからのメッセージボックスを表示するべきではありませんが、テスト目的で表示されることを願っています。最後に、PrepareIconコンストラクターから呼び出しているため、.Clickは常にになりますnull

于 2010-08-09T09:11:42.510 に答える