- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Hướng dẫn thiết kế ứng dụng Winform Single Instance
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách thiết kế ứng dụng Single Instance (duy nhất một thể hiện) trong lập trình C# Winform.
[C#] Single Instance Application Winform
Mặc định khi các bạn viết ứng dụng trên Winform, build ứng dụng xong, các mở file ứng dụng chạy, khi click vào ứng dụng nhiều lần thì sẽ mở lên mỗi ứng dụng một instance.
Các bạn xem ứng dụng Multi Instance mặc định winform như hình bên dưới đây.
Các bạn nhìn hình trên, khi mình mở file exe thì cứ mỗi lần nhấp vào thì nó sẽ mở một Instance ứng dụng mới.
Và trong bài viết này, mình muốn là setup ứng dụng mỗi lần chạy chỉ duy nhất một Instance.
Và nếu ứng dụng đó đang bị thu nhỏ dưới thanh Taskbar (minimize) thì khi chúng ta click vào ứng dụng nó sẽ show ứng dụng đó lên trước Desktop, chứ không phải là chạy một instance của ứng dụng mới.
Để thực hiện các bạn tạo cho mình 3 files c#:
1. ProgramInfo.cs
2. SingleInstance.cs
3. Win32API.cs
Source code file ProgramInfo.cs
c#:
namespace SingleInstanceApp
{
class ProgramInfo
{
static public string AssemblyGuid
{
get
{
object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
if (attributes.Length == 0)
{
return String.Empty;
}
return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value;
}
}
}
}
Source code file SingleInstance.cs
Tiếp đến là source code cho file Win32API.cs
namespace SingleInstanceApp
{
static public class Win32Api
{
[DllImport("user32")]
public static extern int RegisterWindowMessage(string message);
public static int RegisterWindowMessage(string format, params object[] args)
{
string message = String.Format(format, args);
return RegisterWindowMessage(message);
}
public const int HWND_BROADCAST = 0xffff;
public const int SW_SHOWNORMAL = 1;
[DllImport("user32")]
public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(IntPtr window)
{
ShowWindow(window, SW_SHOWNORMAL);
SetForegroundWindow(window);
}
}
}
Trong Form Main, các bạn thêm 2 dòng lệnh sau để cho phép ứng dụng tự động show Desktop khi ứng dụng bạn đang thu nhỏ dưới cửa sổ:
protected override void WndProc(ref Message message)
{
if (message.Msg == SingleInstance.WM_SHOWFIRSTINSTANCE)
{
ShowWindow();
}
base.WndProc(ref message);
}
public void ShowWindow()
{
Win32Api.ShowToFront(this.Handle);
}
Và các bạn chỉnh lại file Program.cs
để setup cho ứng dụng chỉ chạy duy nhất một thể hiện Single Instance.
namespace SingleInstanceApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!SingleInstance.Start())
{
SingleInstance.ShowFirstInstance();
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
SingleInstance.Stop();
}
}
}
Thanks for watching!