Article View: borland.public.cppbuilder.nativeapi
Article #4390Problem with monitorng the clipboard (other viewers does not receive notifications)
From: Vladimir Stefanovic
Date: Wed, 01 Feb 2006 15:13
Date: Wed, 01 Feb 2006 15:13
148 lines
4290 bytes
4290 bytes
Hi,
I've made a sample code to monitor the clipboard changes, and
write the changed clipboard contents (if text) to TMemo. I also
have one TCheckBox to say whether the monitoring should currently
be activated or not.
Unfortunately, *my* viewer works fine but *other* clipboard
viewers does not receive clipoard changes any more, untill I
close my app.
I tested that problematic issue with M$ ClipBook Viewer as a
secondary viewer:
C:\WINDOWS\system32\clipbrd.exe
Here's my code (I obviously missed something):
--- H ---
HWND hNextViewer;
MESSAGE void __fastcall ChangeChain(TMessage &Message);
MESSAGE void __fastcall ClpBrdNotify(TMessage &Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_CHANGECBCHAIN,TMessage,ChangeChain)
MESSAGE_HANDLER(WM_DRAWCLIPBOARD,TMessage,ClpBrdNotify)
END_MESSAGE_MAP(TForm)
--- CPP ---
// ...
#include <ClipBrd.hpp>
// ...
void __fastcall TForm1::FormShow(TObject *Sender)
{
static bool PassedOnceOnShow = false;
if ( !PassedOnceOnShow )
{
if ( CheckBox1->Checked )
{
hNextViewer = SetClipboardViewer( Handle );
}
}
}
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ChangeClipboardChain( Handle, hNextViewer );
}
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
if ( ! CheckBox1->Checked )
{
ChangeClipboardChain( Handle, hNextViewer );
}
else
{
hNextViewer = SetClipboardViewer( Handle );
}
}
void __fastcall TForm1::ChangeChain( TMessage& Message )
{
if ( (HWND)Message.WParam == hNextViewer )
hNextViewer = (HWND)Message.LParam;
PostMessage( hNextViewer, WM_CHANGECBCHAIN, (WPARAM)Message.WParam,
(LPARAM)Message.LParam );
}
void __fastcall TForm1::ClpBrdNotify( TMessage& Message )
{
if ( ::Clipboard()->HasFormat( CF_TEXT ) )
{
AnsiString cText = ::Clipboard()->AsText;
Memo1->Lines->Add( cText );
}
}
P.S.
Some additional questions:
1) How should I check the correctness of SetClipboardViewer()
regarding this:
BCB/Win32 Help:
<quote>
SetClipboardViewer()
Return Values
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.
</quote>
Can I use this:
::SetLastError( 0 );
hNextViewer = SetClipboardViewer( Handle );
if ( ( !hNextViewer ) && ( ::GetLastError() ) )
{
CheckBox1->Checked = false;
}
2) Is there any signifficant differences between these two approachs
to read the clipboard:
--- code 1 ---
if ( ::Clipboard()->HasFormat( CF_TEXT ) )
{
AnsiString cText = ::Clipboard()->AsText;
Memo1->Lines->Add( cText );
}
--- code 2 ---
if ( IsClipboardFormatAvailable( CF_TEXT ) )
{
if ( OpenClipboard( Handle ) )
{
HANDLE cbh = GetClipboardData( CF_TEXT );
if ( cbh != NULL )
{
AnsiString cText = AnsiString( (char*)GlobalLock( cbh ) );
GlobalUnlock( cbh );
Memo1->Lines->Add( cText );
}
}
CloseClipboard();
}
--
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<43e0cf69$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6fad66537802daaf,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Problem with monitorng the clipboard (other viewers does not receive notifications) Date: Wed, 1 Feb 2006 16:13:52 +0100 Lines: 150 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 82.117.212.65 Message-ID: <43e0cf69$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1138806633 82.117.212.65 (1 Feb 2006 07:10:33 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail