- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [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 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 :)