Sergiowaitforit
New member
- Joined
- Oct 26, 2024
- Messages
- 2
- Programming Experience
- Beginner
I am currently working on a C# Windows Forms application, and I'm trying to implement functionality to copy large amounts of text from a browser and process it in my application. However, I have encountered a challenge regarding the use of the clipboard. I've noticed that when the text exceeds a certain character limit (about 65,000 characters), I can’t access it properly from the clipboard. Is there a known limit, and how can I handle large text data effectively?
I would appreciate any guidance or examples that could help me implement this functionality in my application.
Thank you!
My code is:
I would appreciate any guidance or examples that could help me implement this functionality in my application.
Thank you!
My code is:
AutomaticEntryForm:
using Test.Parsers;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Test_Win
{
public partial class AutomaticEntryForm : Form
{
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const byte VK_F12 = 0x7B;
private const int KEYEVENTF_KEYDOWN = 0x0000;
private const int KEYEVENTF_KEYUP = 0x0002;
public AutomaticEntryForm()
{
InitializeComponent();
ConfigurarPantalla();
}
private void ConfigurarPantalla()
{
this.Text = "Asistente de importación de datos";
this.Width = 400;
this.Height = 200;
this.TopMost = true;
var btnCapturar = new Button
{
Text = "CAPTURAR",
Width = 100,
Height = 50,
Top = 50,
Left = 150
};
btnCapturar.Click += BtnCapturar_Click;
this.Controls.Add(btnCapturar);
}
private void BtnCapturar_Click(object sender, EventArgs e)
{
ProgressForm progressForm = new ProgressForm();
progressForm.TopMost = true;
progressForm.Show();
Thread captureThread = new Thread(() => CapturarYProcesarContenido(progressForm));
captureThread.Start();
this.Close(); // Cerrar el formulario de captura al iniciar el hilo
}
private void CapturarYProcesarContenido(ProgressForm progressForm)
{
try
{
UpdateProgress(progressForm, 10, "Buscando navegador...");
var browserHandle = GetBrowserHandle();
if (browserHandle != IntPtr.Zero)
{
SetForegroundWindow(browserHandle);
Thread.Sleep(500);
}
else
{
MessageBox.Show("No se encontró el navegador.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
UpdateProgress(progressForm, 30, "Abriendo consola del navegador...");
Thread.Sleep(1000);
SimulateKeyPress(VK_F12);
Thread.Sleep(2000);
UpdateProgress(progressForm, 50, "Seleccionando contenido...");
SendKeys.SendWait("^a");
Thread.Sleep(1000);
UpdateProgress(progressForm, 70, "Copiando contenido...");
SendKeys.SendWait("^c");
Thread.Sleep(1000);
UpdateProgress(progressForm, 90, "Cerrando consola del navegador...");
SimulateKeyPress(VK_F12);
Thread.Sleep(1000);
UpdateProgress(progressForm, 100, "Captura completada.");
Thread.Sleep(500);
// Guardar el contenido en un archivo temporal
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string tempFilePath = Path.Combine(desktopPath, "ClipboardContent.txt");
// Obtener el contenido del portapapeles directamente
string contenido = Clipboard.GetText();
// Crear el archivo y escribir el contenido
try
{
File.WriteAllText(tempFilePath, contenido); // Guardar el contenido en el archivo
MessageBox.Show("Contenido guardado en " + tempFilePath, "Éxito", MessageBoxButtons.OK); // Confirmar que se guardó
}
catch (Exception ex)
{
MessageBox.Show($"Error al guardar el archivo: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// Muestra el botón de aceptar en el progressForm al finalizar
progressForm.Invoke(new Action(() => progressForm.ShowCompletionMessage()));
}
catch (Exception ex)
{
MessageBox.Show($"Error al capturar y procesar contenido: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateProgress(ProgressForm progressForm, int progress, string task)
{
progressForm.Invoke(new Action(() =>
{
progressForm.UpdateProgress(progress, task);
}));
}
private IntPtr GetBrowserHandle()
{
foreach (var proc in System.Diagnostics.Process.GetProcessesByName("chrome"))
{
if (!string.IsNullOrEmpty(proc.MainWindowTitle))
{
return proc.MainWindowHandle;
}
}
return IntPtr.Zero;
}
private void SimulateKeyPress(byte keyCode)
{
keybd_event(keyCode, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(keyCode, 0, KEYEVENTF_KEYUP, 0);
}
}
}