Google Search

Google Search Results

Tuesday, September 9, 2008

Clipboard Change Notification

This is my first post to my new blog. This blog will be a place for me to post programming information, a way for me to share my knowledge, most of which I learned on-line, so it feels right to give some back.

I've been working fulltime for the same company for the past 8 years, developing software mostly for Windows PCs. I am the only developer at the small company I work for, so I do everything (we do have a testing department of 4 to 6 people, so I only debug my code, then send it off to testing for the major testing). Most of my code is C++, and most uses the Borland VCL library, and I compile most of it with Borland C++ Builder version 6 (BCB6). I don't like .NET because you can't write one set of code that works across Windows 98 through Vista, so I haven't done anything professional using .NET other than web-based stuff (ASP.NET).

I don't like talking about myself, so let's get right to some free source code, do whatever the heck you want with it:

This is a VCL component that notifys you anytime the Windows clipboard changes, just drop on your form and set the OnClipboardChanged event. Then you can use VCL's Clipboard() object to work with the clipboard if you want.

---------- MyClipboard.cpp -----------

#include
#include
#include "MyClipboard.h"
#pragma package(smart_init)

__fastcall TMYClipboard::TMYClipboard(TComponent* Owner)
: inherited(Owner)
{
FWindowHandle = AllocateHWnd(WndProc);
NextViewerWindow = SetClipboardViewer(FWindowHandle);
}

__fastcall TMYClipboard::~TMYClipboard()
{
ChangeClipboardChain(FWindowHandle,NextViewerWindow);
DeallocateHWnd(FWindowHandle);
}

void __fastcall TMYClipboard::WndProc(TMessage &Msg)
{
if(Msg.Msg == WM_DRAWCLIPBOARD)
{
if(FOnClipboardChanged)
FOnClipboardChanged(this);
if(NextViewerWindow)
SendMessage(NextViewerWindow,Msg.Msg,Msg.WParam,Msg.LParam);
}
else if(Msg.Msg == WM_CHANGECBCHAIN)
{
if(NextViewerWindow == (HWND) Msg.WParam)
NextViewerWindow = (HWND) Msg.LParam;
else if(NextViewerWindow)
SendMessage(NextViewerWindow,Msg.Msg,Msg.WParam,Msg.LParam);
Msg.Result = 0;
}
else
Msg.Result = DefWindowProc(FWindowHandle,Msg.Msg,Msg.WParam,Msg.LParam);
}


namespace Myclipboard
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TMYClipboard)};
RegisterComponents("MYPAGE", classes, 0);
}
}

static inline void ValidCtrCheck(TMYClipboard *)
{
new TMYClipboard(NULL);
}

--------------- MyClipboard.h ----------------

#ifndef MYClipboardH
#define MYClipboardH

#include
#include

class PACKAGE TMYClipboard : public TComponent
{
typedef TComponent inherited;
private:
protected:
HWND FWindowHandle;
HWND NextViewerWindow;
void __fastcall WndProc(TMessage &Msg);
void __fastcall (__closure *FOnClipboardChanged)(TObject *Sender);
public:
__fastcall TMYClipboard(TComponent* Owner);
__fastcall ~TMYClipboard();
__published:
__property void __fastcall (__closure *OnClipboardChanged)(TObject *Sender)={read=FOnClipboardChanged,write=FOnClipboardChanged};
};

#endif

No comments: