![]() |
|
|
#1
|
|||
|
|||
|
Как-то видел один пример (скриншот) где по сути дела использовался StringGrid1 или DrawGrid1 в ячейки которого совершалась загрузка выбранного изображения. Вроде того: выбрал изображение, клацнул по ячейке и в нее загрузилось изображение.
Это вообще возможно? Если да, то помогите пожалуйста с кодом на примере. |
|
#2
|
|||
|
|||
|
Цитата:
|
|
#3
|
|||
|
|||
|
Цитата:
|
|
#4
|
||||
|
||||
|
Можно в оба компонента загрузить, но в DrawGrid проще, он под это заточен. Используй событие OnDawCell.
|
|
#5
|
|||
|
|||
|
У Grid'а убираешь DefaultDrawing (т.е. False) и рисуешь на канве в обработчике OnDrawCell.
|
|
#6
|
|||
|
|||
|
Случайно нашел подобный код:
Код:
BitMap := TBitmap.Create;
BitMap.LoadFromFile('Bitmap.bmp');
DrawGrid1.Canvas.CopyRect(DrawGrid1.CellRect(1, 1), BitMap.Canvas, Rect(0, 0, 32, 32)); |
|
#7
|
||||
|
||||
|
Примерно так:
Код:
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
with Sender as TDrawGrid do
ImageList.Draw(Canvas, ACol, ARow, Index);
end;Последний раз редактировалось angvelem, 21.12.2011 в 23:27. |
|
#8
|
|||
|
|||
|
Цитата:
|
|
#9
|
||||
|
||||
|
Привычка писать на WinAPI. Up.
|
|
#10
|
|||
|
|||
|
Я не понял.
Я с ImageList еще не сталкивался и не работал. Пользуюсь функцией вырезания части изображения. |
|
#11
|
|||
|
|||
|
Вот пример отображения пиктограмм из книги "Delphi для чайников":
Код:
if StringGrid1.Objects[ACol, ARow] is TIcon then
StringGrid1.Canvas.Draw(
Rect.Left+56, Rect.Top +24,
TIcon(StringGrid1.Objects[ACol, ARow])); |
|
#12
|
||||
|
||||
|
Цитата:
|
|
#13
|
|||
|
|||
|
Вот целиком пример:
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids, ExtCtrls;
type
TForm1 = class(TForm)
Bevel1: TBevel;
StringGrid1: TStringGrid;
OpenDialog1: TOpenDialog;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ShellApi;
procedure TForm1.Button1Click(Sender: TObject);
var
CurItem: LongInt;
var
pName: array[0..255] of Char;
fName: string[13];
N: Word;
IcoH: hIcon;
begin
CurItem:= 0;
with OpenDialog1 do
begin
if not Execute then Exit;
fName:= ExtractFilename(Filename);
StrPCopy(pName, Filename);
end;
N:= 0;
with StringGrid1 do
repeat
IcoH:= ExtractIcon(hInstance, pName, N);
if IcoH <= 1 then Break;
Col:= CurItem mod ColCount;
if (CurItem div ColCount) >= RowCount then
RowCount:= RowCount + 1;
Row:= CurItem div ColCount;
Cells[Col, Row]:= fName + ' #' + IntToStr(N);
Objects[Col, Row]:= TIcon.Create;
with Objects[Col, Row] as TIcon do Handle:= IcoH;
CurItem:= CurItem + 1;
N:= N + 1;
until false;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if StringGrid1.Objects[ACol, ARow] is TIcon then
StringGrid1.Canvas.Draw(
Rect.Left + 56, Rect.Top + 24, TIcon(StringGrid1.Objects[ACol, ARow]));
end;
end.Код:
object Form1: TForm1
Left = 256
Top = 106
BorderStyle = bsSingle
Caption = 'Icon Collector'
ClientHeight = 251
ClientWidth = 603
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Bevel1: TBevel
Left = 0
Top = 0
Width = 603
Height = 50
Align = alTop
Shape = bsFrame
end
object StringGrid1: TStringGrid
Left = 0
Top = 50
Width = 603
Height = 201
Align = alClient
ColCount = 4
DefaultColWidth = 144
DefaultRowHeight = 64
FixedCols = 0
RowCount = 4
FixedRows = 0
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine]
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
end
object Button1: TButton
Left = 16
Top = 12
Width = 89
Height = 25
Caption = '&Gather Icons...'
Default = True
TabOrder = 1
OnClick = Button1Click
end
object OpenDialog1: TOpenDialog
DefaultExt = 'ico'
Filter = 'All icon files|*.ico;*.exe;*.dll|All files|*.*'
Options = [ofHideReadOnly, ofFileMustExist, ofEnableSizing]
Title = 'Open Icon File'
Left = 208
Top = 16
end
end |
|
#14
|
|||
|
|||
|
Цитата:
|
|
#15
|
|||
|
|||
|
По-моему, OnDrawCell вызывается для всех видимых ячеек (и в примере загружаются несколько ячеек, начиная с первой). Выделенная ячейка имеет координаты Col,Row, так что выполните обычное сравнение:
Код:
if (ACol = Col) and (ARow = Row) then рисуем нашу ячейку |