unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, clipbrd, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
Memo1: TMemo;
procedure FormActivate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure CopyStreamToClipboard(S: TStream; fmt: Word);
var
hMem: THandle;
pMem: Pointer;
begin
{streamÀÇ À§Ä¡¸¦ ¸Ç ¾ÕÀ¸·Î À̵¿½ÃŲ´Ù}
S.Position := 0;
{streamÀÇ Å©±â¸¸Å Àü¿ª Èü(heap)¿¡ ¸Þ¸ð¸® ºí·ÏÀ» ÇÒ´çÇÑÈÄ À©µµ¿ì ÇÚµéÀ» ¾ò´Â´Ù}
hMem := GlobalAlloc(GHND or GMEM_DDESHARE, S.Size);
if hMem <> 0 then
begin
{Àü¿ª ÈüÀÇ ÇÒ´çµÈ ¸Þ¸ð¸® ºí·ÏÀ» °íÁ¤(lock)½ÃŲ´Ù}
pMem := GlobalLock(hMem);
if pMem <> nil then
begin
{ÇÒ´ç¹ÞÀº ¸Þ¸ð¸® ºí·°¿¡ streamÀÇ ³»¿ëÀ» º¹»çÇÑ´Ù}
S.Read(pMem^, S.Size);
{À§ÀÇ S.Read()¿¡ ÀÇÇØ Áõ°¡µÈ Æ÷ÀÎÅ͸¦ ´Ù½Ã ¸Ç ¾ÕÀ¸·Î À̵¿½ÃŲ´Ù}
S.Position := 0;
{Àü¿ª Èü¿¡¼ ¸Þ¸ð¸® ºí·ÏÀÇ Àá±Ý ÇØÁ¦}
GlobalUnlock(hMem);
{Ŭ¸³º¸µå¸¦ ¿·¯¼ ÁÖ¾îÁø Æ÷¸Ë(¿©±â¼´Â CF_TEXT)°ú À©µµ¿ì ÇÚµé·Î ÁöÁ¤µÈ
µ¥ÀÌŸ¸¦ Ŭ¸³º¸µå¿¡ ÁØ´Ù}
Clipboard.Open;
try
Clipboard.SetAsHandle(fmt, hMem);
finally
Clipboard.Close;
end;
end
else
begin
{¸Þ¸ð¸® lockÀÇ ½ÇÆÐ·Î memory block ¿¹¿Ü¸¦ ¹ß»ý½ÃŲ´Ù}
GlobalFree(hMem);
OutOfMemoryError;
end;
end
else
begin
{¸Þ¸ð¸® ÇÒ´çÀÇ ½ÇÆÐ·Î memory block ¿¹¿Ü¸¦ ¹ß»ý½ÃŲ´Ù}
OutOfMemoryError;
end;
end;
procedure CopyGridToClipboard(theGrid: TStringGrid);
var
m: TMemoryStream;
i, j: Integer;
S: String;
begin
m := TMemoryStream.Create;
try
with theGrid do
for i := 0 to Pred(RowCount) {RowCount-1} do
for j := 0 to Pred(ColCount) {ColCount-1} do
begin
S := Cells[j, i];
if j = Pred(ColCount) then // ¸Ç ¸¶Áö¸· ColumnÀ̸é CR/LF ¸¦ Ãß°¡ÇÏ¿© Ç౸ºÐ
AppendStr(S, #13#10)
else
AppendStr(S, #9); // °¢ ColumnÀ» Tab ¹®ÀÚ·Î ±¸ºÐ
m.WriteBuffer(S[1], Length(S));
end;
S[1] := #0; // ¹®ÀÚ¿ streamÀÇ ³¡ Ç¥½Ã
m.WriteBuffer(S[1], 1);
CopyStreamToClipboard(m, CF_TEXT); // streamÀÇ ³»¿ëÀ» Ŭ¸³º¸µå·Î º¹»ç
finally
m.Free;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
var
i, j: Integer;
begin
// ColumnÀÇ titleÀ» ¸¸µç´Ù
for i := 1 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[i, 0] := Char(Ord('A')+i-1);
// RowÀÇ titleÀ» ¸¸µç´Ù
for i := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[0, i] := IntToStr(i);;
// ÀÓÀÇÀÇ ÀڷḦ ¸¸µé¾î¼ °¢ cell¿¡ ÀÔ·ÂÇÕ´Ï´Ù
for i := 1 to StringGrid1.ColCount - 1 do
for j := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[i, j] := Format('%.0n', [i * j * 10000.0]);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
// StringGridÀÇ ³»¿ëÀ» Ŭ¸³º¸µå·Î ºÏ»ç
CopyGridToClipboard(StringGrid1);
// Ŭ¸³º¸µåÀÇ ³»¿ëÀ» ¸Þ¸ð·Î ºÙ¿©³Ö±â(Å×½ºÆ®¿ë)
Memo1.Clear;
Memo1.PasteFromClipboard;
end;
end. |
|