سؤال

وأنا أكتب تطبيق بسيط هو أن وأود أن السيطرة مع أمثلة على notifyIcon بدلا من نموذج، لقد follwed لقد وجدت من خلال جوجل، ولكن بلدي notifyIcon لا تظهر. ما أفعل الخطأ؟


    static class MainEntryClass
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            C2F TestApp = new C2F();
            Application.Run();
            TestApp.Dispose();
        }
    }

    class C2F 
    {
        public C2F()
        {
            InitializeComponent();
            loadSettings();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(C2F));
            this.niC2F = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.separatorToolStripMenuItem = new System.Windows.Forms.ToolStripSeparator();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            // 
            // niC2F
            //
            this.niC2F.BalloonTipText = "MyApp";
            this.niC2F.Icon = ((System.Drawing.Icon)(Clipboard2File.Properties.Resources.ResourceManager.GetObject("MyIcon.ico")));
            this.niC2F.Text = "MyApp";
            this.niC2F.ContextMenuStrip = this.contextMenuStrip1;
            this.niC2F.ShowBalloonTip(5);
            this.niC2F.Visible = true;
            this.niC2F.MouseClick += new System.Windows.Forms.MouseEventHandler(this.niC2F_MouseClick);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.settingsToolStripMenuItem,
            this.separatorToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(153, 76);
            // 
            // settingsToolStripMenuItem
            // 
            this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
            this.settingsToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.settingsToolStripMenuItem.Text = "Settings";
            this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click);
            // 
            // separatorToolStripMenuItem
            // 
            this.separatorToolStripMenuItem.Name = "separatorToolStripMenuItem";
            this.separatorToolStripMenuItem.Size = new System.Drawing.Size(149, 6);
            this.separatorToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem1
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem1";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "Exit";
        }

        private System.ComponentModel.IContainer components = null;
        private Form1 frmSettings = new Form1();
        private Settings C2FSettings = new Settings();
        private System.Windows.Forms.NotifyIcon niC2F;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem settingsToolStripMenuItem;
        private System.Windows.Forms.ToolStripSeparator separatorToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
     }
هل كانت مفيدة؟

المحلول

وأنا فعلا انتهى لتوه من المشروع الذي بدأت بوصفها NotifyIcon. التعليمات البرمجية (انا التخمين التي قدمتها مجرد قصاصة) يشبه بشكل لا يصدق لإزالة الألغام.

وراجعت التعليمات البرمجية الخاصة بك، والتغيير الوحيد كان لزاما على لحملها على العمل وتغيير الطريقة التي تسمى رمز إلى:

this.niC2F.Icon = new System.Drawing.Icon(@"C:\PathToIcon\iconfile.ico");

وفيما يلي عينة العمل مع بزر الماوس الأيمن فوق القائمة وانقر نقرا مزدوجا فوق وظائف:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TestApp
{
    static class MainEntryClass
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            C2F TestApp = new C2F();
            Application.Run();
        }
    }

    class C2F
    {
        System.ComponentModel.Container component;
        System.Drawing.Icon icon;
        ContextMenuStrip rightClickMenu = new ContextMenuStrip();
        NotifyIcon trayIcon;
        ToolStripMenuItem options = new ToolStripMenuItem();
        ToolStripMenuItem restore = new ToolStripMenuItem();
        ToolStripMenuItem exit = new ToolStripMenuItem();
        ToolStripSeparator seperator = new ToolStripSeparator();

        public C2F()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            icon = new System.Drawing.Icon(@"C:\PathToIcon\iconfile.ico");
            component = new System.ComponentModel.Container();
            trayIcon = new NotifyIcon(component);
            trayIcon.Text = "Bill Reminder";
            trayIcon.Icon = icon;
            trayIcon.Visible = true;
            trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
            trayIcon.ContextMenuStrip = rightClickMenu;

            rightClickMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
            {
                options,
                seperator,
                restore,
                exit
            });

            options.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            options.Text = "Options";
            options.Click += new EventHandler(options_Click);

            restore.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            restore.Text = "Restore Window";
            restore.Click += new EventHandler(restore_Click);

            exit.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            exit.Text = "Exit";
            exit.Click += new EventHandler(exit_Click);
        }

        void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void restore_Click(object sender, EventArgs e)
        {
            FormName showWindow = new FormName();
            showWindow.Show();
        }

        void options_Click(object sender, EventArgs e)
        {
            Settings_Window settings = new Settings_Window();
            settings.Show();
        }

        void trayIcon_DoubleClick(object sender, EventArgs e)
        {
            FormName showWindow = new FormName();
            showWindow.Show();
        }
    }

}

وآمل أن يساعد هذا لكم، واسمحوا لي أن أعرف إذا كان لديك أي أسئلة!

نصائح أخرى

واحد من الأسباب لماذا لا يظهر NotifyIcon هو في حالة تشغيل ويندوز إكسبلورر مع امتيازات مرتفعة في حين تطبيق صينية ليست (فقط على أنظمة مع UAC طبعا).

وهذا يمكن أن يحدث إذا تحطمت EXPLORER.EXE أو قتل، ومن ثم إعادة تشغيل المستخدم يدويا من إدارة المهام مرتفعة.

يستخدم

والسيطرة NotifyIcon Shell_NotifyIcon أسلوب الأصلي الداخل، ولكن لا يتم التحقق من قيمة الإرجاع. إذا يعود Shell_NotifyIcon FALSE، فلن يتم إخطار من أي وقت مضى.

واضطررت الى نقطة مع WinDbg على Shell_NotifyIcon وأعطى يقوم GetlastError لي ERROR_ACCESS_DENIED. لذلك أنا أدرك أن هناك مشكلة إذن، ويمكن أن يكون سببه ارتفاع المستكشف إعادة تشغيله. وأكد مزيد من الاختبارات هذا الافتراض.

ولكن هذا هو حالة نادرة إلى حد ما.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top