如果应用程序已经在运行(最小化状态)并且用户单击exe [英] If application already running (Minimize state) and user click exe

查看:165
本文介绍了如果应用程序已经在运行(最小化状态)并且用户单击exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,
有两种形式
LoginForm:要登录
MainForm:有一些可用信息.

1)登录成功后,我的登录表单关闭,主表单打开
2)如果最小化主窗体,然后双击exe(快捷方式).
主要形式应该是开放的.

在program.cs文件中,我使用了以下代码

In my application,
two forms are available
LoginForm : To login
MainForm : where some information is available.

1) After login successfully, My login form close and Main form open
2) If minimize main form and double click on exe(shortcut).
main form should be open.

On program.cs file i have used below code

[STAThread]
static void Main()
{
	try
	{
		bool createdNew;
		m_Mutex = new Mutex(false, "XYZ.exe", out createdNew);  
		if (createdNew)
		{ 
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			DevExpress.Skins.SkinManager.EnableFormSkins();
			DevExpress.UserSkins.BonusSkins.Register();
			UserLookAndFeel.Default.SetSkinStyle("Office 2007 Blue");

			Application.Run(new LoginForm());
		}
		else
		{ 
			NativeMethods.PostMessage(
					(IntPtr)NativeMethods.HWND_BROADCAST,
					NativeMethods.WM_SHOWME,
					IntPtr.Zero,
					IntPtr.Zero);
		} 
	}
	catch (System.Exception ex)
	{
		ClsError.LogError(ex);
	}
}

public static void ShowDialogCentered(this Form frm, Form owner)
{
	Rectangle ownerRect = GetOwnerRect(frm, owner);
	frm.Location = new Point(ownerRect.Left + (ownerRect.Width - frm.Width) / 2,
							 ownerRect.Top + (ownerRect.Height - frm.Height) / 2);
	frm.ShowDialog(owner);
}

private static Rectangle GetOwnerRect(Form frm, Form owner)
{
	return owner != null ? owner.DesktopBounds : Screen.GetWorkingArea(frm);
}

/// On LoginForm Used below Code

protected override void WndProc(ref Message m)
{
    if (m.Msg == NativeMethods.WM_SHOWME)
    {
        ShowMe();
    }
    base.WndProc(ref m);
}
private void ShowMe()
{ 
    if (WindowState == FormWindowState.Minimized)            
    { 
        WindowState = FormWindowState.Normal;
    }
 
    // get our current "TopMost" value (ours will always be false though)
    bool top = TopMost;
    // make our form jump to the top of everything
    TopMost = true;
    // set it back to whatever it was
    TopMost = top; 
}

// Same code i used in MainForm




它适用于LoginForm,但不适用于MainForm

请帮助我




It works for LoginForm But doesnt work for MainForm

Please help me

推荐答案

最简单的方法是检查应用程序是否已打开,如果有则将焦点设置为它: ^ ]
从program.cs文件中的Main方法调用该函数:
Simplest way is to check if the app is already open and set the focus to it if so: A simple way to ensure only one instance is running.[^]
Call that from your Main method in your program.cs file:
static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
        {
        //TODO: Remove this line to allow multiple instances
        Process.GetCurrentProcess().SingleInstance();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMain());
        }
    }


尝试类似的事情

Try something like this

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

public struct WINDOWPLACEMENT
{
            public int length;
            public int flags;
            public int showCmd;
            public POINTAPI ptMinPosition;
            public POINTAPI ptMaxPosition;
            public RECT rcNormalPosition;
}

public struct POINTAPI
{
            public int x;
            public int y;
}

public struct RECT
{
            public int left;
            public int top;
            public int right;
            public int bottom;
}


[STAThread]
public static void Main()  
{
    Process[] procesos = Process.GetProcessesByName("YourMainFormProcessName");
    bool openProcess= false;

    if (procesos.Length > 1)
    {
        foreach (Process proceso in procesos)
        {
          IntPtr wHandle = proceso.MainWindowHandle;

            if (wHandle != IntPtr.Zero)
            {
              openProcess = true;

              WINDOWPLACEMENT wp = new WINDOWPLACEMENT();

              wp.showCmd = 3;

              SetForegroundWindow(wHandle);

              SetWindowPlacement(wHandle, ref wp);
            }
        }
    }

    if (openProcess == false)
    {
      Application.Run(new frmMain());
    }
}


谢谢,但我有两种形式的
1)frmLogin
2)frmMain

如果exe没有运行,我已经使用过
Application.Run(new frmLogin());

但是目前我的frmMain最小化,如果用户单击exe,我想打开frmMain.
Thanks but i have two form
1) frmLogin
2) frmMain

If exe is not running i have used
Application.Run(new frmLogin());

But currently my frmMain minimize and i want to open frmMain if user clicks on exe.


这篇关于如果应用程序已经在运行(最小化状态)并且用户单击exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆