c#如何获取鼠标选取的内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#如何获取鼠标选取的内容相关的知识,希望对你有一定的参考价值。

我像通过我的一个按钮事件把鼠标选取的内容复制到剪切板 请高手支招
C/S winform程序 无论鼠标在哪里选取 我只要获取鼠标选取的类容

获取鼠标选取的内容,可以通过截图的方式,以图片的形式进行保存。

截图方法可借助QQ截图功能,一般快捷按键“ctrl+ALT+A。

或通过浏览器的截图功能:

截图方法:

1、按截图快捷键、或点击浏览器“截图”按钮后,选取需要需要的内容,划定范围后,双击被选择的区域,选取的内容以图片的形式被保存在剪贴板。

2、运行图片处理程序,如系统自带的“画图”。开始----新建文件---(参数不用修改),在新建好的文档空白区域右键鼠标,然后选择“粘贴”,刚才截取的内容被显示出来。然后保存。关闭退出。

”如果对象是文本文件,在文本开头处,点击鼠标左键不松,拖动鼠标到文本结尾,选中需要的内容后,在选择区域点击鼠标右键,在弹出菜单中选择复制,然后打开系统中能够编辑文本的软件,如WORD,新建一个文档后,在新建好的文档空白区域点击鼠标右键,在菜单中选择“粘贴”。最后保存退出。内容即被保存。

参考技术A 要是在TextBox里面的数据,你button_Click里面就写
this.textBox1.SelectedText这就是你选中的数据
再给剪切板就是
Clipboard.SetDataObject(this.textBox1.SelectedText);

补充:你是WinForm,那看看控件里有没有Select..之类的属性,有的话一般就能取到选中内容。
你要是的想选软件界面之外的内容,那这种方法就没用了,不过好像也选不中本回答被提问者和网友采纳
参考技术B 要是在TextBox里面的数据,你button_Click里面就写
this.textBox1.SelectedText这就是你选中的数据
再给剪切板就是
Clipboard.SetDataObject(this.textBox1.SelectedText);

补充:你是WinForm,那看看控件里有没有Select..之类的属性,有的话一般就能取到选中内容。
你要是的想选软件界面之外的内容,那这种方法就没用了,不过好像也选不中
参考技术C .Net封装好的方法

int x = Control.MousePosition.X;
int y = Control.MousePosition.Y;

用API方法

using System.Runtime.InteropServices;
Point p;
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point pt);
private void timer1_Tick(object sender, EventArgs e)

GetCursorPos(out p);
label1.Text = p.X.ToString();//X坐标
label2.Text = p.Y.ToString();//Y坐标
参考技术D 好象 要有指定的控件,比如文本文件什么的,屏幕好象就要直接画了

如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?

【中文标题】如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?【英文标题】:How to get the "ctrl c" or mouse copy event in a Powerpoint 2013 Addin (developed in C#)? 【发布时间】:2017-01-11 16:24:39 【问题描述】:

下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?

下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?

下面的代码不是在 MS Powerpoint 中捕获字符类型,它只是在 Powerpoint 之外,我如何才能在此代码中捕获“控制副本”或“鼠标右键单击副本”?

    public partial class ThisAddIn
    

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;

        private static IntPtr hookId = IntPtr.Zero;
        private delegate IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam);
        private static HookProcedure procedure = HookCallback;

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);  


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        
            hookId = SetHook(procedure);
        

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        
            UnhookWindowsHookEx(hookId);
        


        private static IntPtr SetHook(HookProcedure procedure)
        
            using (Process process = Process.GetCurrentProcess())
            using (ProcessModule module = process.MainModule)
                return SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0);
        

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            



                int pointerCode = Marshal.ReadInt32(lParam);
                string pressedKey = ((Keys)pointerCode).ToString();

                //Do some sort of processing on key press
                var thread = new Thread(() =>  if (pressedKey.ToLower() == "LControlKey")  MessageBox.Show(pressedKey);  );
                thread.Start();
            
            return CallNextHookEx(hookId, nCode, wParam, lParam);
        


        protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
        
            return new Ribbon();
        

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        

        #endregion
    

【问题讨论】:

【参考方案1】:

使用自定义功能区。

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <commands>
    <command idMso="Cut" onAction="CopyIntercept" />
    <command idMso="Copy" onAction="CopyIntercept" />
  </commands>
</customUI>

public void CopyIntercept(IRibbonControl control, ref bool CancelDefault)


您也许可以使用功能区设计器来完成此操作,但上面的示例适用于 XML 功能区。

【讨论】:

以上是关于c#如何获取鼠标选取的内容的主要内容,如果未能解决你的问题,请参考以下文章

c# 动态获取当前屏幕中光标所在位置

如何获取monthCalendar中鼠标选定的日期 C#

如何在 Powerpoint 2013 插件(用 C# 开发)中获取“ctrl c”或鼠标复制事件?

matlab如何用鼠标选取fig图上的区域并获得坐标值

如何从鼠标右键单击获取文本?

如何在 Qt 中获取鼠标在屏幕上的位置?