Files
tj-tzhg/JJMediSys/ErrMsgShow.cs
2025-11-26 17:21:18 +08:00

285 lines
9.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JJMediSys
{
public partial class ErrMsgShow : Form
{
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
private bool m_aeroEnabled; // variables for box shadow
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCPAINT = 0x0085;
private const int WM_ACTIVATEAPP = 0x001C;
public struct MARGINS // struct for box shadow
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
private const int WM_NCHITTEST = 0x84; // variables for dragging the form
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
protected override CreateParams CreateParams
{
get
{
m_aeroEnabled = CheckAeroEnabled();
CreateParams cp = base.CreateParams;
if (!m_aeroEnabled)
cp.ClassStyle |= CS_DROPSHADOW;
return cp;
}
}
private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT: // box shadow
if (m_aeroEnabled)
{
var v = 2;
DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 1,
rightWidth = 1,
topHeight = 1
};
DwmExtendFrameIntoClientArea(this.Handle, ref margins);
}
break;
default:
break;
}
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form
m.Result = (IntPtr)HTCAPTION;
}
public Dispensing fordispensing;
public ErrMsgShow(Dispensing dispensing)
{
m_aeroEnabled = false;
InitializeComponent();
fordispensing = dispensing;
}
public ErrMsgShow()
{
m_aeroEnabled = false;
InitializeComponent();
}
private void ErrMsgShow_Load(object sender, EventArgs e)
{
//int x = fordispensing.Location.X + (fordispensing.Width - this.Width) / 2;
//int y = fordispensing.Location.Y + (fordispensing.Height - this.Height) / 2;
//this.Location = new Point(x, y);
//Msg.Text = fordispensing.ErrorInfoStr;
Msg.Text = "蓝牙连接失败";
if (Msg.Text.Contains("蓝牙11"))
{
BRetry.Location = new System.Drawing.Point(16, 171);
BCancel.Location = new System.Drawing.Point(362, 171);
ResetBLE.Visible = true;
ResetBLE.Location = new System.Drawing.Point(190, 171);
}
else
{
BRetry.Location = new System.Drawing.Point(77, 171);
BCancel.Location = new System.Drawing.Point(305, 171);
ResetBLE.Visible = false;
}
}
private void BRetry_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
this.Close();
}
private void BCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
//Form1.CancelDispensing = true; //手动点击终止后 TubeLabelTool.cs中的循环将退出 并返回手动取消状态
//if (!fordispensing.isStop)//处于可手动终止状态
// fordispensing.DispenseCancel = true; //
//fordispensing.Closewin();
this.Close();
}
private async void ResetBLE_Click(object sender, EventArgs e)
{
try
{
// 停止蓝牙服务
await StopBluetoothServiceAsync();
// 等待几秒钟以确保服务完全停止
// await Task.Delay(5000);
// 启动蓝牙服务
// await StartBluetoothServiceAsync();
Console.WriteLine("蓝牙服务已成功重启。");
}
catch (Exception ex)
{
Console.WriteLine($"发生错误: {ex.Message}");
}
DialogResult = DialogResult.Retry;
this.Close();
}
static async Task StopBluetoothServiceAsync()
{
string scriptPath = @"D:\\BuleToothOff.ps1";
// 创建 ProcessStartInfo 对象
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe", // 指定 cmd.exe 可执行文件
Arguments = $"powershell -ExecutionPolicy Bypass -File {scriptPath}", // 使用 cmd.exe 启动 PowerShell 并运行脚本
//RedirectStandardOutput = true, // 重定向标准输出
// RedirectStandardError = true, // 重定向标准错误
// UseShellExecute = false , // 不使用外壳程序
Verb = "runas"
//CreateNoWindow = true // 不创建新窗口
};
// 创建 Process 对象
using (Process process = new Process { StartInfo = startInfo })
{
// 启动进程
process.Start();
// 读取标准输出
string output = process.StandardOutput.ReadToEnd();
// 读取标准错误
string error = process.StandardError.ReadToEnd();
// 等待进程退出
process.WaitForExit();
// 输出结果
Console.WriteLine("Output:");
Console.WriteLine(output);
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error:");
Console.WriteLine(error);
}
}
Console.WriteLine("脚本执行完毕。");
}
static async Task StartBluetoothServiceAsync()
{
string scriptPath = @".\BuleToothOn.ps1";
// 创建 ProcessStartInfo 对象
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "powershell.exe", // 指定 PowerShell 可执行文件
Arguments = $"-ExecutionPolicy Bypass -File \"{scriptPath}\"", // 指定脚本文件路径
RedirectStandardOutput = true, // 重定向标准输出
RedirectStandardError = true, // 重定向标准错误
UseShellExecute = false, // 不使用外壳程序
CreateNoWindow = true // 不创建新窗口
};
// 创建 Process 对象
using (Process process = new Process { StartInfo = startInfo })
{
// 启动进程
process.Start();
// 读取标准输出
string output = process.StandardOutput.ReadToEnd();
// 读取标准错误
string error = process.StandardError.ReadToEnd();
// 等待进程退出
process.WaitForExit();
// 输出结果
Console.WriteLine("Output:");
Console.WriteLine(output);
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error:");
Console.WriteLine(error);
}
}
Console.WriteLine("脚本执行完毕。");
}
static async Task<string> ReadOutputAsync(System.IO.StreamReader reader)
{
return await reader.ReadToEndAsync();
}
}
}