C# 能不能获取文本框光标当前所在的位置,及所在的行

C# 能不能获取文本框光标当前所在的位置,及所在的行,第1张

[DllImport("user32dll", EntryPoint = "SendMessageA")]

private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);

[DllImport("user32 ")]

private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

private void button2_Click(object sender, EventArgs e)

{

int EM_LINEINDEX = 0xBB;

int EM_LINEFROMCHAR = 0xC9;

int EM_GETSEL = 0xB0;

int EM_GETLINE = 0xC4;

//通过API完成很简单的

//通过向文本框传递EM_LINEFROMCHAR消息可以得到光标所在的行数

int Row = (int)SendMessage(textBox1Handle, EM_LINEFROMCHAR, -1, 0);

////通过向文本框传递EM_GETSEL消息可以得到编辑控件中一段选定内容的起点与终点字符的个数

//int s = (int)SendMessage(textBox1Handle, EM_GETSEL, 0, 0);

////通过向文本框传递EM_GETSEL消息可以得到每一行的第一个字符在全文中的字符序号

//int li = (int)SendMessage(textBox1Handle, EM_LINEINDEX, -1, 0);

//int Col = (s & 0xffff) - li + 1;

StringBuilder currentLine = new StringBuilder(256);

SendMessage(textBox1Handle, EM_GETLINE, Row, currentLine);

MessageBoxShow(currentLineToString());

}

你想要什么反馈??鼠标慢慢移过去?那自己先getcursorpos然后再算出dx和dy,用sleep或timer做移动轨迹呗。哪有这么多为什么。你鼠标物理移动还不是产生了N次硬件中断才有的移动视觉效果。

通过win32定义的POINT结构(typedef struct tagPOINT { LONG x;

LONG y;} POINT),来获得当前鼠标光标位置的窗口HWND,这是最直观的武器!常规 *** 作如下:先得到Cursor的POINT(BOOL GetCursorPos(LPPOINT)函数),再用WindowFromPoint。这样,我们几乎可以获得任何打开的有窗口的函数的HWND了!然后通过获取类名的win32 api函数(int GetClassName( HWND hWnd, LPTSTR lpClassName, int nMaxCount ))得到类名这里的lpClassName最好用字符数组地址,nMaxCount就是数组的size了。

[DllImport("user32dll", EntryPoint = "SendMessageA")]

private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);

[DllImport("user32 ")]

private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

private void button2_Click(object sender, EventArgs e)

{

int EM_LINEINDEX = 0xBB;

int EM_LINEFROMCHAR = 0xC9;

int EM_GETSEL = 0xB0;

int EM_GETLINE = 0xC4;

//通过API完成很简单的

//通过向文本框传递EM_LINEFROMCHAR消息可以得到光标所在的行数

int Row = (int)SendMessage(textBox1Handle, EM_LINEFROMCHAR, -1, 0);

////通过向文本框传递EM_GETSEL消息可以得到编辑控件中一段选定内容的起点与终点字符的个数

//int s = (int)SendMessage(textBox1Handle, EM_GETSEL, 0, 0);

////通过向文本框传递EM_GETSEL消息可以得到每一行的第一个字符在全文中的字符序号

//int li = (int)SendMessage(textBox1Handle, EM_LINEINDEX, -1, 0);

//int Col = (s & 0xffff) - li + 1;

StringBuilder currentLine = new StringBuilder(256);

SendMessage(textBox1Handle, EM_GETLINE, Row, currentLine);

MessageBoxShow(currentLineToString());

}

可以试一下

       #region 得到光标在屏幕上的位置

        [DllImport("user32")]

         public static extern bool GetCaretPos(out Point lpPoint);

         [DllImport("user32dll")]

         private static extern IntPtr GetForegroundWindow();

         [DllImport("user32dll")]

         private static extern IntPtr GetFocus();

         [DllImport("user32dll")]

         private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);

         [DllImport("user32dll")]

         private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

         [DllImport("kernel32dll")]

         private static extern IntPtr GetCurrentThreadId();

         [DllImport("user32dll")]

         private static extern void ClientToScreen(IntPtr hWnd, ref Point p);

         private Point CaretPos()

         {

             IntPtr ptr = GetForegroundWindow();

             Point p = new Point();

             //得到Caret在屏幕上的位置   

             if (ptrToInt32() != 0)

             {

                 IntPtr targetThreadID = GetWindowThreadProcessId(ptr, IntPtrZero);

                 IntPtr localThreadID = GetCurrentThreadId();

                 if (localThreadID != targetThreadID)

                 {

                     AttachThreadInput(localThreadID, targetThreadID, 1);

                     ptr = GetFocus();

                     if (ptrToInt32() != 0)

                     {

                         GetCaretPos(out   p);

                         ClientToScreen(ptr, ref   p);

                     }

                     AttachThreadInput(localThreadID, targetThreadID, 0);

                 }

             }

             return p;

         }

         #endregion

通过Windows API 函数“HWND WindowFromPoint(POINT Point)”可以获取光标所在处的窗口的句柄。需要有一个触发消息来调用这个函数:比如按下键盘上的某个按键来触发这个函数调用。这是个简单的示范,能够模拟鼠标点击其他窗口的关闭按钮行为;要获得更完善的方案,可以给我私信。

具体 *** 作方法:运行程序,让后将鼠标指针移动至其他窗口之上,最好是在标题栏上;然后按下键盘上的任意按键(推荐按字母键)。

在VC++60“文件”菜单中,新建一个空的“Win32 AppLication”工程。然后新建一个cpp源文件,在源文件中写入如下代码:

#include <windowsh>

LRESULT CALLBACK WindowProc(HWND hdl_wnd, UINT msg_code, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

{

 WNDCLASS wndcls;

 wndclscbClsExtra = 0;

 wndclscbWndExtra = 0;

 wndclshbrBackground = (HBRUSH)COLOR_BTNSHADOW;

 wndclshCursor = ::LoadCursor(NULL, IDC_ARROW);

 wndclshIcon = ::LoadIcon(NULL, IDI_APPLICATION);

 wndclshInstance = hInstance;

 wndclslpfnWndProc = WindowProc;

 wndclslpszClassName = "gethandle";

 wndclslpszMenuName = NULL;

 wndclsstyle = CS_VREDRAW | CS_HREDRAW;

 ::RegisterClass(&wndcls);

 ::CreateWindow("gethandle", "获取窗口句柄", WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE, 200, 120, 400, 300, NULL, NULL, hInstance, 0);

 MSG msg;

 while (::GetMessage(&msg, NULL, 0, 0))

 {

   ::TranslateMessage(&msg);

   ::DispatchMessage(&msg);

 }

 return 1;

}

LRESULT CALLBACK WindowProc(HWND hdl_wnd, UINT msg_code, WPARAM wParam, LPARAM lParam)

{

 LRESULT value = 0;

 switch (msg_code)

 {

   case WM_CLOSE:

     ::PostQuitMessage(WM_QUIT);

   break;

    //注意:必须在本窗口获得键盘输入焦点的时候,WM_KEYDOWN消息才能生效!

    //如果想在本窗口没有焦点的时候都能生效,则需要安装全局的键盘钩子。

   case WM_KEYDOWN:

   {

     POINT pt;

     HWND hdl_other;

     ::GetCursorPos(&pt);  //获取鼠标指针的坐标

     hdl_other = ::WindowFromPoint(pt); //将该坐标传递给获取句柄的函数

     ::PostMessage(hdl_other, WM_CLOSE, 0, 0);  //用获取到的句柄,向目标窗口发送一个WM_CLOSE消息。WM_CLOSE消息相当于用户点击了窗口右上角的关闭按钮。

   }

   break;

   default:

   return ::DefWindowProc(hdl_wnd, msg_code, wParam, lParam);

 }

 return value;

}

以上就是关于C# 能不能获取文本框光标当前所在的位置,及所在的行全部的内容,包括:C# 能不能获取文本框光标当前所在的位置,及所在的行、c语言调用Windows API的SetCursorPos设置鼠标光标位置函数为什么光标没在视觉上进行反馈、如何获得窗口句柄以及WIN32+API参考大全等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9507274.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存