- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
[C#] Hướng dẫn đọc dữ liệu Text từ chương trình Notepad sử dụng Win32
Xin chào các bạn, bài viết hôm nay mình xin tiếp tục hướng dẫn các bạn cách lấy dữ liệu text từ chương trình Notepad thông qua thư viện Win32 API C#.
Trong bài viết này, mình sẽ sử dụng thư viện Win32 và lấy dữ liệu text đang nhập trên chương trình notepad vào RichTextBox của mình.
Giao diện demo ứng dụng:

Như hình demo ở trên, các bạn thấy khi mình gõ dữ liệu vào bên Notepad xong, thì chúng ta qua ứng dụng bấm nút get Data, phần mềm sẽ lấy được dữ liệu trực tiếp của Notepad.
Source code GET_TEXT Win32 C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GetDataFormAnotherApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
List<WinText> windows = new List<WinText>();
IntPtr hWnd = FindWindow("notepad", null);
while (hWnd != IntPtr.Zero)
{
IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "edit", null);
System.Text.StringBuilder sb = new System.Text.StringBuilder(255);
int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);
windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() });
hWnd = FindWindowEx(IntPtr.Zero, hWnd, "notepad", null);
}
windows.OrderBy(x => x.Text).ToList().ForEach(y => richTextBox1.Text += string.Format("{0}", y.Text));
}
private struct WinText
{
public IntPtr hWnd;
public string Text;
}
const int WM_GETTEXT = 0x0D;
const int WM_GETTEXTLENGTH = 0x0E;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
}
}
Trong bài viết tới, mình sẽ hướng dẫn các bạn làm ngược lại, tức là lấy dữ liệu từ ứng dụng của mình gởi sang chương trình khác.
Ví dụ: Auto điền username và mật khẩu vào ứng dụng khác.
Mong các bạn đón đọc.
Have Fun :)