Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
[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



It works for LoginForm But doesnt work for MainForm

Please help me
Posted
Updated 4-Nov-14 23:07pm
v2

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:
C#
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());
        }
    }
 
Share this answer
 
v2
Try something like this

C#
[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());
    }
}
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900