Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Points for Epsilon -- clipboard use for raw data and Delphi 4

Posted on 1999-07-30
2
Medium Priority
?
231 Views
Last Modified: 2010-04-04
//Epsilon: post an answer so I can give you the points.
//Others: This problem was solved via email.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Clipbrd;

const
  MYFORMAT = 'MyClipboardFormat';

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var hmem, cf, sd: Cardinal;
    lock: Pointer;
begin
  // Allocate memory in the global heap
  // Do not free it in this app. It will be freed when the clipboard is cleared
  hmem := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE, 100);

  // Lock the memory to get access to it
  lock := GlobalLock(hmem);
  CopyMemory(lock, PChar('This is a test'), 16); // just copy some data
  GlobalUnlock(hmem);

  //Register own clipboard format if it not already exists
  cf := RegisterClipboardFormat(MYFORMAT);
  if cf = 0 then
    ShowMessage('Failed to obtain the clipboard format')
  else begin
    if OpenClipboard(Handle) then
    begin
      sd := SetClipboardData(cf, hmem);
      CloseClipboard;
      if sd <> 0 then
        ShowMessage('Data succesfully copied to the clipboard');
    end
    else
      ShowMessage('Could not open the clipboard!');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var hmem, cf: Cardinal;
    lock: Pointer;
    text: array[0..16] of Char;
begin
  // find the registered clipboard format
  cf := RegisterClipboardFormat(MYFORMAT);
  if cf = 0 then
    ShowMessage('Failed to obtain the clipboard format')
  else begin
    if OpenClipboard(Handle) then
    begin
      hmem := GetClipboardData(cf);
      CloseClipboard;
      if hmem = 0 then
        ShowMessage('No usable data on the clipboard')
      else begin
        lock := GlobalLock(hmem);
        CopyMemory(@text[0], lock, 16);
        GlobalUnlock(hmem);
        ShowMessage(text);
      end;
    end
    else
      ShowMessage('Could not open the clipboard!');
  end;
end;

end.
0
Comment
Question by:arcrotty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:arcrotty
ID: 1392073
Adjusted points to 250
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 1000 total points
ID: 1392074
Thank you very much   :o)


Cheers,

Epsylon.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question