程序已打开,并且在托盘中,重新点击程序时,不用重新运行,仅最大化托盘中的程序!
使用了ShowWindowAsync只能实现最小化的时候可以激活窗口,但是托盘状态不行,哪位高手知道,请多多指教...
使用了ShowWindowAsync只能实现最小化的时候可以激活窗口,但是托盘状态不行,哪位高手知道,请多多指教
展开
2个回答
展开全部
几处要修改:
一修改Program.cs:
原来为:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
添加命名空间:
using System.Diagnostics;
using System.Runtime.InteropServices;
然后修改代码为:
public const int WM_PROCESSISRUNNING = 0x0400 + 101;
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process pCurrent = Process.GetCurrentProcess();
Process[] pList = Process.GetProcessesByName(pCurrent.ProcessName);
if (pList.Length < 2)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
SendMessage(pList[0].MainWindowHandle, WM_PROCESSISRUNNING, 0, 0);
}
}
然后到主窗体里面加入代码:
protected override void WndProc(ref Message m)
{
if (m.Msg == Program.WM_PROCESSISRUNNING)
{
this.WindowState = FormWindowState.Normal;
}
base.WndProc(ref m);
}
一修改Program.cs:
原来为:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
添加命名空间:
using System.Diagnostics;
using System.Runtime.InteropServices;
然后修改代码为:
public const int WM_PROCESSISRUNNING = 0x0400 + 101;
[DllImport("user32")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process pCurrent = Process.GetCurrentProcess();
Process[] pList = Process.GetProcessesByName(pCurrent.ProcessName);
if (pList.Length < 2)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
SendMessage(pList[0].MainWindowHandle, WM_PROCESSISRUNNING, 0, 0);
}
}
然后到主窗体里面加入代码:
protected override void WndProc(ref Message m)
{
if (m.Msg == Program.WM_PROCESSISRUNNING)
{
this.WindowState = FormWindowState.Normal;
}
base.WndProc(ref m);
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
#region dll import
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string classname, string windowname);
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
public static extern long SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern long GetWindowText(IntPtr hwnd, string lpString, long cch);
public IntPtr formhwnd;
#endregion
public Form1()
{
//只运行一个实例
formhwnd = FindWindow(null, "From1");
if (formhwnd.ToInt32() > 0)
{
MessageBox.Show("系统已运行!查看是否最小化!", "安全", MessageBoxButtons.OK, MessageBoxIcon.Information);
SetForegroundWindow(formhwnd);
this.Close();
}
InitializeComponent();
}
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(string classname, string windowname);
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("User32.dll")]
public static extern long SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern long GetWindowText(IntPtr hwnd, string lpString, long cch);
public IntPtr formhwnd;
#endregion
public Form1()
{
//只运行一个实例
formhwnd = FindWindow(null, "From1");
if (formhwnd.ToInt32() > 0)
{
MessageBox.Show("系统已运行!查看是否最小化!", "安全", MessageBoxButtons.OK, MessageBoxIcon.Information);
SetForegroundWindow(formhwnd);
this.Close();
}
InitializeComponent();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询