NEWS

[C#] Hướng dẫn đọc dữ liệu Text từ chương trình Notepad sử dụng Win32

[C#] Hướng dẫn đọc dữ liệu Text từ chương trình Notepad sử dụng Win32
Đăng bởi: Thảo Meo - Lượt xem: 4711 00:58:22, 17/10/2018C#   In bài viết

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:

get text win32 c#
Giao diện chương trình lấy text từ chương trình khác "Notepad"

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 :)

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn đọc dữ liệu Text từ chương trình Notepad sử dụng Win32
Đăng bởi: Thảo Meo - Lượt xem: 4711 00:58:22, 17/10/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.