0

我想要一个控制元素来显示正在运行的程序中有关连接的 USB 设备的更改。搜索后,我设法找到了 USB 设备并将它们打印出来。我还处理获取有关删除和连接的信息。

但是现在有一个问题,当我将两个部分结合起来时它不起作用。这是我的代码:

namespace usbPortAbfrage
{
    public partial class Form1 : Form
    {
        ArrayList result = new ArrayList();
        public Form1()
        {
            InitializeComponent();
            bla();
        }

        public ArrayList GetComFriendlyNames()
        {
            ArrayList names = new ArrayList();
            try
            {
                ManagementObjectSearcher searcher =
                  new ManagementObjectSearcher("root\\WMI",
                  "SELECT InstanceName, PortName FROM MSSerial_PortName");

                foreach (ManagementObject port in searcher.Get())
                {
                    names.Add(port["PortName"]);
                }
            }
            catch (ManagementException)
            {
            }
            return names;
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (m.WParam.ToInt32() == 0x8004)
            {
                MessageBox.Show("Gerät entfernt");
                richTextBox1.Clear();
                bla();
            }

            if (m.WParam.ToInt32() == 0x8000)
            {
                MessageBox.Show("Gerät angeschlossen");
                richTextBox1.Clear();
                bla();
            }
        }

        public void bla()
        {
            richTextBox1.Clear();

            ArrayList test = GetComFriendlyNames();

            foreach (string name in test)
            {
                richTextBox1.AppendText(name + "\n");
            }
        }     
    }
}
4

1 回答 1

0

这是我的测试中的调试代码,用于尝试复制我似乎也无法命中事件代码的问题。(IE 不,此代码不起作用,它不是答案/解决方案,但我无法将其放入评论中,请不要投反对票!)

public partial class Form1 : Form
{
    private const int WM_ACTIVATEAPP = 0x001C;
    private const int DBT_DEVICEARRIVAL = 0x8000;               // system detected a new device      
    private const int DBT_DEVICEREMOVEPENDING = 0x8003;         // about to remove, still available      
    private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;  
    private bool appActive = true;

    public Form1()
    {
        this.Size = new System.Drawing.Size(300, 300);
        this.Text = "Form1";
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        // Paint a string in different styles depending on whether the 
        // application is active. 
        if (appActive)
        {
            e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, 20, 20, 260, 50);
            e.Graphics.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
        }
        else
        {
            e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, 20, 20, 260, 50);
            e.Graphics.DrawString("Application is Inactive", this.Font, SystemBrushes.ActiveCaptionText, 20, 20);
        }
    }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    protected override void WndProc(ref Message m)
    {
        // Listen for operating system messages. 
        switch (m.Msg)
        {
            // The WM_ACTIVATEAPP message occurs when the application 
            // becomes the active application or becomes inactive. 
            case WM_ACTIVATEAPP:

                // The WParam value identifies what is occurring.
                appActive = (((int)m.WParam != 0));

                // Invalidate to get new text painted. 
                this.Invalidate();

                break;
            case DBT_DEVICEARRIVAL:
                MessageBox.Show("Connected!");
                break;
            case DBT_DEVICEREMOVECOMPLETE:
                MessageBox.Show("Disconnected!");
                break;
        }
        base.WndProc(ref m);
    }
}

链接到 MSDN 上的类似问题:http: //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ea183afd-d070-4abd-8e00-a1784fdfedc5/

于 2012-11-06T14:53:32.960 回答