PDA

View Full Version : اجرا یکبار برنامه-در صورتی که برنامه اجرا بود اون فرم که از قبل اجرا بوده است را بزرگ کند!



Hosein.Zarei
سه شنبه 02 مهر 1387, 22:13 عصر
اگر عنوان بزرگی دارد به خاطر این است که قبلا تاپیک مشابهی توسط ... حذف شد.

اجرا یکبار برنامه-در صورتی که برنامه اجرا بود اون فرم که از قبل اجرا بوده است را بزرگ کند!
مشابه این برنامه های زیادی وجود دارد برای مثال وقتی ویندوز مدیا پلیر اجرا باشد و مینیمایز شده باشد اگر دوباره برنامه را اجرا کنید اون پنجره که مینیمایز شده را ماکسیمایز می کند.
این یک مونه ساده است.
من هم همین قابلیت را می خواهم به برنامه ام به دهم در این صورت که در مثل ویندوز مدیا پلیر فرم باز شده را بزرگ ( ماکسیمایز) کنم.

در قسمت 1001 نکته کدی که برای فقط یکبار اجرا شدن برنامه هست ولی من کدی را می خواهم که اگر برنامه اجر است اون را بزرگ کند.

امید وارم دوباره این تاپیک حذف نشد!

esmartiz_red
چهارشنبه 03 مهر 1387, 00:20 صبح
http://barnamenevis.org/forum/showpost.php?p=473313&postcount=125
http://barnamenevis.org/forum/showpost.php?p=470337&postcount=71
http://barnamenevis.org/forum/showpost.php?p=482919

Hosein.Zarei
چهارشنبه 03 مهر 1387, 22:43 عصر
از کمک تون خیلی ممنون
من قبلا به این تاپیک ها سرزده بودم.
لطفا دوباره بیشتر توجه کنید:
من می خواهم اگر برنامه قبلا اجرا شده, دوباره اجرا نشود و به جای آن فرمهای برنامه قبل را نشان دهد.
و مثال های شما فقط مورد اول را انجام می دهد و مانع اجرای دوباره برنامه می شود ولی فرم های قبلی از که از قبل اجرا شده را بزرگ نمی کند!

به طور ساده تر مثلا کاربر دفعه اول برنامه را باز می کند و در آن چیزهایی در فرم وارد می کند و آن را مینیمایز می کند, حالا اگر کاربر دوباره برنامه را اجرا کرد برنامه بارگذاری نشود و به جای آن فرمی که قبلا باز کرده بود را ماکسیمایز کند.

اگر مثال ویندوز مدیا پلیر را یکبار امتحان کنید حتما متوجه منظور من می شوید در غیر این صورت دوباره درخواست راهنمایی بیشتر در اینجا کنید تا توضیح دهم.

RED-C0DE
پنج شنبه 04 مهر 1387, 00:18 صبح
می تونی از این نمونه کد استفاده کنی :



static class Program
{

[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hwnd);

private const int WS_SHOWNORMAL = 1;


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Process instance = get_Running_Instance ();
if (instance == null) //okay , no other instance is running...run my instance :
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new Form1());
}
else
{ //There is another instance of this process.
MessageBox.Show("There's another instance running");
set_preInstace_Active(instance);
}
}

public static Process get_Running_Instance()
{
Process current = Process.GetCurrentProcess();
Process[] instances = Process.GetProcessesByName(current.ProcessName);

//Loop through the running processes in with the same name
foreach (Process process in instances )
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (System.Reflection.Assembly.GetExecutingAssembly() .Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}

//No other instance was found, return null.
return null;
}


public static void set_preInstace_Active(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);

//Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
}

}