コード例 #1
0
ファイル: FlashRecovery.cs プロジェクト: hhhhhaleyLi/winform-
        private void IsDeviceExist(string status)
        {
            //检测硬件设备是否连接成功
            ManagementObjectCollection collection = null;
            bool isExist = false;

            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.DoWork += new DoWorkEventHandler((sender1, e1) =>
                {
                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
                    {
                        collection = searcher.Get();
                        searcher.Dispose();
                    }
                });
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((sender2, e2) =>
                {
                    isExist = false;
                    foreach (ManagementBaseObject device in collection)
                    {
                        //获取包含PID和VID的字符串
                        string deviceId = (string)device.GetPropertyValue("DeviceID");
                        //这里的XXX填写自己产品硬件设备的PID和VID,连接硬件后在设备管理器中可以查到
                        if ("XXX".Equals(deviceId))
                        {
                            isExist = true;
                            break;
                        }
                    }
                    //未连接硬件设备
                    if (!isExist)
                    {
                        this.deviceIdLabel.Text    = @"00000000";
                        this.connStatus.Text       = @"未连接设备";
                        this.connStatusImage.Image = global::FlashRecovery.Properties.Resources.未连接设备;
                        if ("out".Equals(status))
                        {
                            MessageBox.Show("检测到硬件设备断开连接,请重新插入硬件设备恢复软件使用");
                        }
                        //设置窗体不可操作
                        CurGlobals.EnableWindow(this.Handle, 0);
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(_deviceId))
                        {
                            //获取设备ID
                            //...
                        }
                        this.deviceIdLabel.Text    = _deviceId;
                        this.connStatus.Text       = @"成功连接设备";
                        this.connStatusImage.Image = global::FlashRecovery.Properties.Resources.成功连接设备;
                        //设置窗体可操作
                        CurGlobals.EnableWindow(this.Handle, 1);
                    }
                });
                bw.RunWorkerAsync();
            }
        }
コード例 #2
0
ファイル: FlashRecovery.cs プロジェクト: hhhhhaleyLi/winform-
        private static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong)
        {
            if (IntPtr.Size == 4)
            {
                return(CurGlobals.SetWindowLongPtr32(hWnd, nIndex, dwNewLong));
            }

            return(CurGlobals.SetWindowLongPtr64(hWnd, nIndex, dwNewLong));
        }
コード例 #3
0
ファイル: FlashRecovery.cs プロジェクト: hhhhhaleyLi/winform-
        private void FlashRecovery_Load(object sender, EventArgs e)
        {
            //记录窗体初始宽度
            _formWidth = this.Width;
            //记录窗体初始高度
            _formHeight = this.Height;

            #region 检测是否连接硬件设备
            ManagementObjectCollection collection;
            //获取所有连接的USB设备
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
            {
                collection = searcher.Get();
                searcher.Dispose();
            }

            string deviceID = "";
            //获取硬件设备ID  deviceID
            //...
            _deviceId = deviceID;

            bool isExist = false;
            //判断产品硬件设备是否连接
            foreach (ManagementBaseObject device in collection)
            {
                string deviceId = (string)device.GetPropertyValue("DeviceID");
                //连接硬件设备
                if ("XXX".Equals(deviceId))
                {
                    isExist = true;
                    this.connStatus.Text       = @"成功连接设备";
                    this.connStatusImage.Image = global::FlashRecovery.Properties.Resources.成功连接设备;
                    this.deviceIdLabel.Text    = deviceID.ToString();
                    break;
                }
            }
            //未连接硬件设备
            if (!isExist)
            {
                MessageBox.Show("未检测到硬件设备,请连接硬件设备", "警告", MessageBoxButtons.OK);
                //设置窗体不可操作
                CurGlobals.EnableWindow(this.Handle, 0);
            }
            #endregion
        }
コード例 #4
0
ファイル: FlashRecovery.cs プロジェクト: hhhhhaleyLi/winform-
        private void FlashRecovery_Resize(object sender, EventArgs e)
        {
            Refresh();

            //通知业务进程改变窗体大小
            CurGlobals.SENDMESSAGETRANSFERSTRUCT temp = new CurGlobals.SENDMESSAGETRANSFERSTRUCT();
            temp.dwData = this.Handle;
            //设置业务进程的窗体大小
            string tempSize = $"{this.exeTabControl.Width - 5},{this.exeTabControl.Height - 5}".PadLeft(20, '0');

            byte[] arr = System.Text.Encoding.Default.GetBytes(tempSize);
            //参数的长度
            temp.cbData = arr.Length + 1;
            //业务进程窗体大小参数
            temp.lpData = tempSize;
            //向每个进程发消息
            foreach (Process p in m_AppProcess)
            {
                CurGlobals.SendMessage(p.MainWindowHandle, WM_COPYDATA, IntPtr.Zero, ref temp);
            }
        }
コード例 #5
0
ファイル: FlashRecovery.cs プロジェクト: hhhhhaleyLi/winform-
        //将指定的程序嵌入指定的控件
        private void EmbedProcess(Process app, Control con, int flag)
        {
            if (app == null || app.MainWindowHandle == IntPtr.Zero || con == null)
            {
                return;
            }
            //设置窗体展示方式
            CurGlobals.ShowWindow(app.MainWindowHandle, (short)5);
            //设置父窗体
            CurGlobals.SetParent(app.MainWindowHandle, con.Handle);
            SetWindowLong(new HandleRef(this, app.MainWindowHandle), GWL_STYLE, WS_VISIBLE);
            CurGlobals.SendMessage(app.MainWindowHandle, WM_SETTEXT, IntPtr.Zero, strGUID);
            CurGlobals.MoveWindow(app.MainWindowHandle, 0, 0, con.Width, con.Height, true);

            if (flag != -1)
            {
                CurGlobals.SENDMESSAGETRANSFERSTRUCT temp = new CurGlobals.SENDMESSAGETRANSFERSTRUCT();
                temp.cbData = _argument;
                temp.dwData = Handle;
                CurGlobals.SendMessage(app.MainWindowHandle, WM_COPYDATA, IntPtr.Zero, ref temp);
            }
        }
コード例 #6
0
 public static void HandleRunningInstance(Process instance)
 {
     CurGlobals.ShowWindowAsync(instance.MainWindowHandle, 1);
     CurGlobals.SetForegroundWindow(instance.MainWindowHandle);
 }