using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Net.Sockets;namespace RemoteControl
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        private void niInfo_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Right)
                Menu.Show(Cursor.Position);
        }
        private void Main_Load(object sender, EventArgs e)
        {
            StartListener();
        }
        private void StartListener()
        {
            TcpListener listener;
            try
            {
                listener = new TcpListener(60000);
            }
            catch
            {
                Console.WriteLine("Falt to Creat Listener Port");
                return;
            }
            listener.Start();
            bool loop = true;
            while (loop)
            {
                Socket s = listener.AcceptSocket();
                NetworkStream ns = new NetworkStream(s);
                StreamReader r = new StreamReader(ns);
                string command = r.ReadLine();
                StreamWriter sw = new StreamWriter(command);
                sw.WriteLine();
                s.Close();
            }
        }        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}这里ContextMenuStrip菜单的鼠标右击事件没有动作.谢谢!

解决方案 »

  1.   

     niInfo
    是什么打断点查
      

  2.   

    初步怀疑是Main_Load事件里出的问题,但是不太确定到了什么问题,请高手指点.谢谢
      

  3.   

    你是不是要添加托盘图标的右键菜单?如果是的话,NotifyIcon本身有个属性:contextMenuStrip,填入你要弹出的菜单就OK了.你现在的问题可能的原因: 你要弹出的menu没有声明吧?
      

  4.   

    要把Designer的代码贴出来才知道。。
      

  5.   

    Designer代码如下:namespace RemoteControl
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
                this.niInfo = new System.Windows.Forms.NotifyIcon(this.components);
                this.Stop = new System.Windows.Forms.ToolStripMenuItem();
                this.Restart = new System.Windows.Forms.ToolStripMenuItem();
                this.Exit = new System.Windows.Forms.ToolStripMenuItem();
                this.Menu = new System.Windows.Forms.ContextMenuStrip(this.components);
                this.Menu.SuspendLayout();
                this.SuspendLayout();
                // 
                // niInfo
                // 
                this.niInfo.ContextMenuStrip = this.Menu;
                this.niInfo.Icon = ((System.Drawing.Icon)(resources.GetObject("niInfo.Icon")));
                this.niInfo.Text = "notifyIcon1";
                this.niInfo.Visible = true;
                // 
                // Stop
                // 
                this.Stop.Name = "Stop";
                this.Stop.Size = new System.Drawing.Size(152, 22);
                this.Stop.Text = "停止";
                // 
                // Restart
                // 
                this.Restart.Name = "Restart";
                this.Restart.Size = new System.Drawing.Size(152, 22);
                this.Restart.Text = "重启";
                // 
                // Exit
                // 
                this.Exit.Name = "Exit";
                this.Exit.Size = new System.Drawing.Size(152, 22);
                this.Exit.Text = "退出";
                this.Exit.Click += new System.EventHandler(this.Exit_Click);
                // 
                // Menu
                // 
                this.Menu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.Stop,
                this.Restart,
                this.Exit});
                this.Menu.Name = "Menu";
                this.Menu.Size = new System.Drawing.Size(153, 92);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(305, 254);
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form1";
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "Main";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.VisibleChanged += new System.EventHandler(this.Form1_VisibleChanged);
                this.Menu.ResumeLayout(false);
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.NotifyIcon niInfo;
            private System.Windows.Forms.ContextMenuStrip Menu;
            private System.Windows.Forms.ToolStripMenuItem Stop;
            private System.Windows.Forms.ToolStripMenuItem Restart;
            private System.Windows.Forms.ToolStripMenuItem Exit;
        }
    }我试过了注释掉Main_Load事件里的StartListener()方法就没有问题.
      

  6.   

            private void Main_Load(object sender, EventArgs e) 
            { 
                StartListener(); 
            } 
            private void StartListener() 
            { 
                TcpListener listener; 
                try 
                { 
                    listener = new TcpListener(60000); 
                } 
                catch 
                { 
                    Console.WriteLine("Falt to Creat Listener Port"); 
                    return; 
                } 
                listener.Start(); 
                bool loop = true; 
                while (loop) 
                { 
                    Socket s = listener.AcceptSocket(); 
                    NetworkStream ns = new NetworkStream(s); 
                    StreamReader r = new StreamReader(ns); 
                    string command = r.ReadLine(); 
                    StreamWriter sw = new StreamWriter(command); 
                    sw.WriteLine(); 
                    s.Close(); 
                } 
            } while (loop) 
    你的窗体一启动就进入了死循环。。窗体一直在做事~!!!你给这个StartListener()加一个线程吧。        private void Main_Load(object sender, EventArgs e) 
            { 
               System.Threading.Thread thread = new System.Threading.Thread(StartListener);
               thread.IsBackground = true;
               thread.Start();
            }