// <»ç¿ë¹æ¹ý> ¾Æ·¡ ÄÚµùÀ» ÇϽÅÈÄ¿¡ StringGridÀÇ ¾î¶² ÇÁ·ÎÆÛƼµµ º¯°æÇÒ Çʿ䰡 ¾ø½À´Ï´Ù
// Button1 À» Ŭ¸¯ÇϽøé StringGridÀÇ Æ¯Á¤¿µ¿ªÀÌ ¼±ÅÃµÇ°í ±× ³»¿ëÀ» Ŭ¸³º¸µå·Î
// º¹»çÇÑ ´ÙÀ½ ´Ù½Ã Memo ¿¡ ºÙ¿©³Ö±â°¡ µË´Ï´Ù
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Grids, clipbrd;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Memo1: TMemo;
Button1: TButton;
procedure FormActivate(Sender: TObject);
procedure Button1Click(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 CopySelectedGridToClipboard(theGrid: TStringGrid);
var
m: TMemoryStream;
i, j: Integer;
S: String;
begin
m := TMemoryStream.Create;
try
with theGrid do
for i := theGrid.Selection.Top to theGrid.Selection.Bottom do
for j := theGrid.Selection.Left to theGrid.Selection.Right do
begin
S := Cells[j, i];
// ¸Ç ¸¶Áö¸· ¼±ÅÃµÈ ColumnÀ̸é CR/LF ¸¦ Ãß°¡ÇÏ¿© Ç౸ºÐ
if j = theGrid.Selection.Right then
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.Button1Click(Sender: TObject);
var
i: integer;
SelectedRectangle: TGridRect;
CoordTopLeft, CoordBottomRight: TGridCoord;
begin
// ¼±ÅÃÇϰíÀÚ ÇÏ´Â Á÷»ç°¢Çü ¿µ¿ªÀ» ¼³Á¤ÇÑ´Ù
CoordTopLeft.X:= 2; // µÎ¹øÂ° Ä÷³ºÎÅÍ ¼±ÅÃ
CoordTopLeft.Y:= 1; // ù¹øÂ° ÇàºÎÅÍ ¼±ÅÃ
CoordBottomRight.X:= StringGrid1.RowCount - 2; // ¸¶Áö¸· Ä÷³ÀÇ ÀüÄ÷³
CoordBottomRight.Y:= StringGrid1.ColCount - 2; // ¸¶Áö¸· ÇàÀÇ ÀüÇà
// Á÷»ç°¢Çü ¿µ¿ªÀ» TGridRect ¿¡ ÇÒ´çÇÑ´Ù
with StringGrid1 do
begin
SelectedRectangle.TopLeft := CoordTopLeft;
SelectedRectangle.BottomRight := CoordBottomRight;
end;
StringGrid1.Selection:= SelectedRectangle;
// StringGridÀÇ ¼±ÅÃµÈ ¿µ¿ªÀ» Ŭ¸³º¸µå·Î ºÏ»ç
CopySelectedGridToClipboard(StringGrid1);
// Ŭ¸³º¸µåÀÇ ³»¿ëÀ» ¸Þ¸ð·Î ºÙ¿©³Ö±â(Å×½ºÆ®¿ë)
Memo1.Clear;
Memo1.PasteFromClipboard;
end;
end. |
|