- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[C#] Hướng dẫn sử dụng thư viện Win32 giao tiếp với ứng dụng khác
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách giao tiếp điêu khiển ứng dụng khác qua thư viện Win32 lập trình C#.
Ví dụ: Các bạn muốn điền thông tin vào phần mềm khác bên ngoài và click để thực hiện.
Trong ví dụ bài viết này, mình sẽ demo hai chức năng.
- Send Text từ ứng dụng C# sang chương trình Notepad.
- Gởi text sang phần mềm máy tính Calc.exe, để tính toán
Giao diện ứng dụng:
Gởi qua phần mềm máy tính là 20 * 50 chẳng hạn...
Để điều khiển được ứng dụng, các bạn cần phải có thông tin của Title và Class của ứng dụng đó.
Trong bài viết này, mình sẽ hướng dẫn các bạn cách lấy Tilte và Class thông qua phần mềm AutoIT.
Các bạn có thể download phần mềm AutoIT tại trang chủ nó nhé các bạn. Chi tiết lấy title và class, các bạn có thể xem video ở dưới đây.
Source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spy_App
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
const int BM_CLICK = 0x00F5;
private const int BN_CLICKED = 245;
private void btn_send_Click(object sender, EventArgs e)
{
IntPtr WindowHandle = FindWindow(textBox2.Text, textBox1.Text);
if(WindowHandle == IntPtr.Zero)
{
//MessageBox.Show(textBox1.Text + " Ứng dụng không tồn tại");
//return;
var process = Process.Start("calc.exe");
Thread.Sleep(1000);
WindowHandle = FindWindow(textBox2.Text, textBox1.Text);
}
SetForegroundWindow(WindowHandle);
SendKeys.SendWait("50");
SendKeys.SendWait("*");
SendKeys.SendWait("10");
SendKeys.SendWait("=");
SendKeys.SendWait("{ENTER}");
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr WindowHandle = FindWindow(textBox2.Text, textBox1.Text);
if (WindowHandle == IntPtr.Zero)
{
MessageBox.Show(textBox1.Text + " Ứng dụng không tồn tại");
return;
}
SetForegroundWindow(WindowHandle);
SendKeys.SendWait("Spy App https://laptrinhvb.net
");
SendKeys.SendWait("{ENTER}");
}
}
}
HAVE FUN :)