PDA

View Full Version : پیاده سازی قابلیت هایی که بابیلون داشت



بیتا حکمت
شنبه 27 تیر 1394, 05:57 صبح
سلام ، عیدکم مبارک و ایامکم سعیدة کل عام و انتم بخیر :لبخند:
تصمیم گرفتم به مناسبت این عید فرخنده یک سوال های کلاس و یک سوال خیلی های کلاس مطرح کنم
_
سوال اول : تو نرم افزار بابلیون وقتی ماوس روی یک متن نرم افزارهای دیگه می بردیم ، با کلیک روی متن اون نرم افزار ، متن در تکست باکس بابیلون کپی می شد ( به زبان ساده بگم ، بابلیون می تونست متن نرم افزارهایی که با ماوس اشاره کنیم تشخیص بده و بدست بیاره ) ، لینک ، Samle ، تجربه ، توضیحات یا حداقل ایده ای برای پیاده سازی این قابلیت دارین ؟!


سوال دوم : میخوام یه برنامه بنویسم که : روی هر نقطه از صفحه نمایش که کلیک کردم (حتی خارج از محدوده برنامه سی شارپ ) ، برنامه مختصات X و Y اون نقطه رو بهم بده !
و اینکه با دادن X و y به برنامه مذکور در اون مختصات کلیک کنه ..

shahryari
شنبه 27 تیر 1394, 10:55 صبح
سلام طاعات قبول
جواب اول در لینک زیر
https://social.msdn.microsoft.com/Forums/windows/en-US/1dc356e6-9441-44de-9eda-247003fa6ef5/copy-selected-text-from-any-window?forum=winformsapplications
(https://social.msdn.microsoft.com/Forums/windows/en-US/1dc356e6-9441-44de-9eda-247003fa6ef5/copy-selected-text-from-any-window?forum=winformsapplications)
private System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();

private void Form2_Load(object sender, EventArgs e)
{
//Initialize a timer to update the control text.
_timer.Interval = 1000;
_timer.Tick += new EventHandler(_timer_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
try
{
textBox1.Text = GetTextFromFocusedControl();
}
catch (Exception exp)
{
textBox1.Text += exp.Message;
}
}

//Start to monitor and show the text of the related control.
private void button1_Click(object sender, EventArgs e)
{
_timer.Start();
}

//Get the text of the focused control
private string GetTextFromFocusedControl()
{
try
{
int activeWinPtr = GetForegroundWindow().ToInt32();
int activeThreadId = 0, processId;
activeThreadId = GetWindowThreadProcessId(activeWinPtr, out processId);
int currentThreadId = GetCurrentThreadId();
if (activeThreadId != currentThreadId)
AttachThreadInput(activeThreadId, currentThreadId, true);
IntPtr activeCtrlId = GetFocus();

return GetText(activeCtrlId);
}
catch (Exception exp)
{
return exp.Message;
}
}




جواب سوال دوم:

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;

public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}

/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
//bool success = User32.GetCursorPos(out lpPoint);
// if (!success)

return lpPoint;
}

behzadkhan
شنبه 27 تیر 1394, 12:41 عصر
با سلام

دوست عزیز

یک برنامه با عنوان "
خواندن متن از برنامه دیگر توسط API" در لینک زیر هست:

http://barnamenevis.org/showthread.php?64210-%D9%86%D9%85%D9%88%D9%86%D9%87-%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87-%D9%87%D8%A7%DB%8C-%DA%A9%D9%88%DA%86%DA%A9-%D9%88-%D9%85%D9%81%D9%8A%D8%AF-%D8%AF%D8%B1-%D8%B3%D9%8A-%D8%B4%D8%A7%D8%B1%D9%BE/page26

بررسی کنید.

ممکنه بدردتون بخورده.

با تشکر

بیتا حکمت
شنبه 27 تیر 1394, 13:04 عصر
با سلام

دوست عزیز

یک برنامه با عنوان "
خواندن متن از برنامه دیگر توسط API
" در لینک زیر هست:

http://barnamenevis.org/showthread.php?64210-%D9%86%D9%85%D9%88%D9%86%D9%87-%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87-%D9%87%D8%A7%DB%8C-%DA%A9%D9%88%DA%86%DA%A9-%D9%88-%D9%85%D9%81%D9%8A%D8%AF-%D8%AF%D8%B1-%D8%B3%D9%8A-%D8%B4%D8%A7%D8%B1%D9%BE/page26

بررسی کنید.

ممکنه بدردتون بخورده.

با تشکر

ممنون که گفتین ، ولی متاسفانه با Vs2010 باز نمیشه . شما به چه نسخه ای بازش کردین ؟

behzadkhan
شنبه 27 تیر 1394, 13:50 عصر
با سلام

دوست عزیز

کافی شما یک پروژه کنسول ایجاد کنید.

و کد زیر را در فایل Program.cs جایگزین نمایید.

البته توجه کنید که این آن چیزی که شما می خواهید نیست ولی شاید کمکتون بکنه.

فقط توجه کنید که برنامه پویا نیست و باید در Main برنامه باید نام Title پنجره مورد نظر خود را باید وارد کنید.

در صورتی که Title پنجره را درست وارد کرده باشید. تمامی متون آن پنجره در کنسول نمایش داده خواهد شد.

این کد برنامه هست :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


//www.barnamenevis.org - danialafshari
namespace GetWindowTextExample
{
public class Program
{
public static void Main()
{
// توجه کنید که عنوان برنامه ای را که می خواهید به تمامی متن های آن دسترسی پیدا کنید را در خط زیر باید وارد کنید
var allText = GetAllTextFromWindowByTitle("Microsoft SQL Server Management Studio");
Console.WriteLine(allText);
Console.ReadLine();
}
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);


[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);


[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);


[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, [Out] StringBuilder lParam);


// Callback method used to collect a list of child windows we need to capture text from.
private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
// Creates a managed GCHandle object from the pointer representing a handle to the list created in GetChildWindows.
var gcHandle = GCHandle.FromIntPtr(pointer);


// Casts the handle back back to a List<IntPtr>
var list = gcHandle.Target as List<IntPtr>;


if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}


// Adds the handle to the list.
list.Add(handle);


return true;
}


// Returns an IEnumerable<IntPtr> containing the handles of all child windows of the parent window.
private static IEnumerable<IntPtr> GetChildWindows(IntPtr parent)
{
// Create list to store child window handles.
var result = new List<IntPtr>();


// Allocate list handle to pass to EnumChildWindows.
var listHandle = GCHandle.Alloc(result);


try
{
// Enumerates though all the child windows of the parent represented by IntPtr parent, executing EnumChildWindowsCallback for each.
EnumChildWindows(parent, EnumChildWindowsCallback, GCHandle.ToIntPtr(listHandle));
}
finally
{
// Free the list handle.
if (listHandle.IsAllocated)
listHandle.Free();
}


// Return the list of child window handles.
return result;
}


// Gets text text from a control by it's handle.
private static string GetText(IntPtr handle)
{
const uint WM_GETTEXTLENGTH = 0x000E;
const uint WM_GETTEXT = 0x000D;


// Gets the text length.
var length = (int)SendMessage(handle, WM_GETTEXTLENGTH, IntPtr.Zero, null);


// Init the string builder to hold the text.
var sb = new StringBuilder(length + 1);


// Writes the text from the handle into the StringBuilder
SendMessage(handle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);


// Return the text as a string.
return sb.ToString();
}


// Wraps everything together. Will accept a window title and return all text in the window that matches that window title.
private static string GetAllTextFromWindowByTitle(string windowTitle)
{
var sb = new StringBuilder();


try
{
// Find the main window's handle by the title.
var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);


// Loop though the child windows, and execute the EnumChildWindowsCallback method
var childWindows = GetChildWindows(windowHWnd);


// For each child handle, run GetText
foreach (var childWindowText in childWindows.Select(GetText))
{
// Append the text to the string builder.
sb.Append(childWindowText);
}


// Return the windows full text.
return sb.ToString();
}
catch (Exception e)
{
Console.Write(e.Message);
}


return string.Empty;
}
}
}





===================================

با تشکر