- [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 truyền dữ liệu giữa hai ứng dụng
Bài viết hôm nay, mình sẽ hướng dẫn các cách truyền dữ liệu giữa hai ứng dụng khác nhau trong lập trình C#, sử dụng thư viện Win32 (User32.dll).
Trong các bài viết trước, mình đã có bài viết hướng dẫn cách truyền dữ liệu giữa hai form, các bạn có thể tham khảo ở link bên dưới.
[C#] Hướng dẫn sử dụng delegate để truyền dữ liệu giữa 2 form
Giao diện demo ứng dụng truyền dữ liệu:
- Đầu tiên, các bạn tạo một class với tên MessageHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class MessageHelper
{
[DllImport("User32.dll")]
private static extern int RegisterWindowMessage(string lpString);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
//For use with WM_COPYDATA and COPYDATASTRUCT
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(int hWnd);
public const int WM_USER = 0x400;
public const int WM_COPYDATA = 0x4A;
//Used for WM_COPYDATA for string messages
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
public bool bringAppToFront(int hWnd)
{
return SetForegroundWindow(hWnd);
}
public int sendWindowsStringMessage(int hWnd, int wParam, string msg)
{
int result = 0;
if (hWnd > 0)
{
byte[] sarr = Encoding.UTF8.GetBytes(msg);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData = (IntPtr)100;
cds.lpData = msg;
cds.cbData = len + 1;
result = SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
}
return result;
}
public int sendWindowsMessage(int hWnd, int Msg, int wParam, int lParam)
{
int result = 0;
if (hWnd > 0)
{
result = SendMessage(hWnd, Msg, wParam, lParam);
}
return result;
}
public int getWindowId(string className, string windowName)
{
return FindWindow(className, windowName);
}
}
- Viết code cho App 1:
private void txtMessage_TextChanged(object sender, EventArgs e)
{
MessageHelper msg = new MessageHelper();
int result = 0;
int hWnd = msg.getWindowId(null, "WinApp2 - https://laptrinhvb.net");
result = msg.sendWindowsStringMessage(hWnd, 0, txtMessage.Text);
}
- Viết code cho App 2:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_USER:
break;
case WM_COPYDATA:
COPYDATASTRUCT mystr = new COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (COPYDATASTRUCT)m.GetLParam(mytype);
byte[] bytes = Encoding.UTF8.GetBytes(mystr.lpData);
string myString = Encoding.UTF8.GetString(bytes);
label1.Text = myString;
break;
}
base.WndProc(ref m);
}
HAPPY CODING