非窗口程序如何监视系统剪贴板? [英] How do non-window program monitor system clipboard?

查看:55
本文介绍了非窗口程序如何监视系统剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个使用C#监视Windows剪贴板的程序.我找到了一些有关该主题的文章.根据线程如何在C#中监视剪贴板内容的更改?找到WPF窗口的句柄,我使用WPF编写了一个演示.在我发现的所有示例代码中,它们都是WinForm或WPF应用程序,它们与win32 api互操作,并需要窗口句柄作为参数.例如api函数 SetClipboardViewer(HWNDhWndNewViewer)

但是在我的情况下,我需要程序运行后台作为服务来监视和收集剪贴板内容.如何在没有窗口UI的情况下监视剪贴板?

能给我一些建议吗?预先感谢.

I want to write a program to monitor windows clipboard using C#. I found some post about this topic. According thread How to monitor clipboard content changes in C#? and Finding the handle to a WPF window, I write a demo using WPF. In all samples code I found, all of them are WinForm or WPF apps, and win32 api they interop with need window handle as parameters. Such as api function SetClipboardViewer(HWND hWndNewViewer)

But in my scenario, I need my program run background as a service to monitor and collect clipboard content. How to monitor clipboard without window UI?

Could you give me some suggestions? Thanks in advance.

根据user1795804的建议,我编写了以下测试代码

According user1795804's suggestion, I write following test code

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public static class User32
    {


  [DllImport("User32.dll")]
    public static extern IntPtr OpenClipboard(IntPtr hWndNewOwner);

    [DllImport("User32.dll")]
    public static extern IntPtr GetClipboardData(uint uFormat);
}
class Program
{
    static void Main(string[] args)
    {
        int result = (int)User32.OpenClipboard(new IntPtr(0));
        if (result == 0)
        {
            Console.WriteLine("error");
        }
        else
        {
            Console.WriteLine("success");
        }

        int returnHandle = (int)User32.GetClipboardData(1); //CF_TEXT 1
        if (returnHandle == 0)
        {
            Console.WriteLine("can't get text data");
        }

        Console.ReadKey();
    }
    }
}

结果是我可以打开剪贴板,并且似乎可以获取到日期对象的句柄.但是现在我有两个问题.

1.尽管我在剪贴板中有一个数据对象的句柄,但如何使用句柄获取此数据?我找不到相关功能.

2.我需要传递一个proc函数作为回调,以便它在系统事件引发时可以接收消息.但是我在非窗口应用程序中找不到对应的内容.

The result is I can open clipboard and seem to get a handle to date object. But now I have two issue.

1. Although I have a handle to data object in clipboard, how can I get this data using handle? I can't find related function.

2. I need pass a proc function as callback so that it can receive message when system event raise. But I can't find counterpart in non-window app.

推荐答案

Microsoft表示,有三种方法可以监视剪贴板的更改.最古老的方法是创建剪贴板查看器窗口.Windows2000新增了以下功能:查询剪贴板序列号,Windows Vista增加了对剪贴板格式侦听器的支持.支持剪贴板查看器窗口以与早期版本的Windows向后兼容.新程序应使用剪贴板格式侦听器或剪贴板序列号."

According to Microsoft, "There are three ways of monitoring changes to the clipboard. The oldest method is to create a clipboard viewer window. Windows 2000 added the ability to query the clipboard sequence number, and Windows Vista added support for clipboard format listeners. Clipboard viewer windows are supported for backward compatibility with earlier versions of Windows. New programs should use clipboard format listeners or the clipboard sequence number."

此GetClipboardSequenceNumber不接受任何参数,根据Microsoft的说法,系统为每个窗口站保留一个剪贴板的序列号.每当剪贴板的内容更改或清空剪贴板时,此序列号就会递增.您可以跟踪此值以确定剪贴板的内容是否已更改并优化创建DataObjects.如果剪贴板渲染延迟,则在渲染更改之前,序列号不会增加."

This GetClipboardSequenceNumber does not take any arguments and according to Microsoft, "The system keeps a serial number for the clipboard for each window station. This number is incremented whenever the contents of the clipboard change or the clipboard is emptied. You can track this value to determine whether the clipboard contents have changed and optimize creating DataObjects. If clipboard rendering is delayed, the sequence number is not incremented until the changes are rendered."

这将满足您我想编写一个使用C#监视Windows剪贴板的程序"的要求.

This would fulfill your requirement of "I want to write a program to monitor windows clipboard using C#".

这篇关于非窗口程序如何监视系统剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆