How do non-window program monitor system clipboard?

99封情书 提交于 2021-01-27 15:53:14

问题


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.


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();
    }
    }
}

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.


回答1:


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."

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."

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



来源:https://stackoverflow.com/questions/17461083/how-do-non-window-program-monitor-system-clipboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!