3 out of 6 rated this helpful - Rate this topic

SetClipboardViewer function

Applies to: desktop apps only

Adds the specified window to the chain of clipboard viewers. Clipboard viewer windows receive a WM_DRAWCLIPBOARD message whenever the content of the clipboard changes.

Syntax

HWND WINAPI SetClipboardViewer(
  __in  HWND hWndNewViewer
);

Parameters

hWndNewViewer [in]

Type: HWND

A handle to the window to be added to the clipboard chain.

Return value

Type: HWND

If the function succeeds, the return value identifies the next window in the clipboard viewer chain. If an error occurs or there are no other windows in the clipboard viewer chain, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The windows that are part of the clipboard viewer chain, called clipboard viewer windows, must process the clipboard messages WM_CHANGECBCHAIN and WM_DRAWCLIPBOARD. Each clipboard viewer window calls the SendMessage function to pass these messages to the next window in the clipboard viewer chain.

A clipboard viewer window must eventually remove itself from the clipboard viewer chain by calling the ChangeClipboardChain function — for example, in response to the WM_DESTROY message.

Examples

For an example, see Adding a Window to the Clipboard Viewer Chain.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
ChangeClipboardChain
GetClipboardViewer
SendMessage
Conceptual
Clipboard

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
asdfadf
Starting with Windows Vista, applications should use the much more robust AddClipboardFormatListener and RemoveClipboardFormatListener functions.See "The clipboard viewer linked list is no longer the responsibility of applications to maintain, unless they want to" for rationale and sample usage.


Great....
This clipboard notification chain works great for your own applications... until some other application doesn't process WM_CHANGECBCHAIN properly or doesn't call ChangeClipboardChain() before it exits. I mean, when that happens it's not Microsoft's fault that some other application broke the chain on me, right? It's perfectly reasonable for an API to have a linked list of observers and to require that the observers themselves maintain the integrity of the list, right? $0$0 $0 $0-> See WM_CLIPBOARDUPDATE (http://msdn.microsoft.com/en-us/library/ms649021(VS.85).aspx). -Michael$0
VM_CLIPBOARDUPDATE
See VM_CLIPBOARDUPDATE (http://msdn.microsoft.com/en-us/library/ms649021(VS.85).aspx) for a better way of doing this on Vista and Win7.$0
Return value and GetLastError()

SetClipboardViewer() does not set the last error to 0 on success. Therefore it's impossible to tell whether a 0 return value is an error condition or the end of the clipboard chain. The suggested solution is to explicitly clear the last error before calling SetClipboardViewer, like this:

    

SetLastError(0);

HWND nextViewer = SetClipboardViewer(wnd);

if (nextViewer == NULL && GetLastError() != 0) {

// SetClipboardViewer failed...

}

SetClipboardViewer() also sends a WM_DRAWCLIPBOARD message to the newly added viewer before it returns. Therefore, the last error must be cleared at the end of the WM_DRAWCLIPBOARD handler as well:

case WM_DRAWCLIPBOARD: {
// handle msg
/* ... */
// forward msg to next viewer
if (nextViewer)
SendMessage(nextViewer, msg, wparam, lparam);
// clear error to avoid confusing SetClipboardViewer() return
SetLastError(0);
return 0;
}
Clipboard activity monitor with Compact Framework

Hi !

I would like to know if there is an equivalent of the SetClipboardViewer function on the .NET compact framework.

Background : I write an application for a WM6 pocket PC in C# that runs in the background and should check the contents of the clipboard whenever its contents has been changed by any other application running on the device.

Several posts explain how to do that for the "full framework" by using User32.dll SetClipboardViewer described in this article:

http://www.codeguru.com/csharp/.net/net_general/tipstricks/article.php/c7315/


http://www.developer.com/net/csharp/article.php/3359891

http://www.radsoftware.com.au/articles/clipboardmonitor.aspx

However, I can't find the equivalent of SetClipboardViewer in the CF.

Any suggestion is welcome. Thanks for your time !

Fabien

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.