我想在FMX字符串网格的单个单元格(而不是checkcolunn)上放置一个复选框。
我认为我需要使用'StringGrid1.AddObject‘,但是我不知道如何从那里开始。
发布于 2022-07-20 15:56:46
我不明白你不能(但你改变了我的代码)。首先,我想说我不是Android上的Stringgrid粉丝,但这是另外一点。第二点,我专注于绘图,我还编写了一些代码来管理包含Tpath而不是TStyleObject的其他样式
procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var sb : TFMXObject;
aRectF : TRectF;
img : tfmxobject;
begin
if Assigned(Stylebook) then sb:=Stylebook.Style else sb:=TStyleManager.ActiveStyle(Self);
if Test.find(Column.Index,Row)>=0 then
begin
if Test[Test.Find(Column.Index,Row)].ischecked
then img := sb.FindStyleResource('checkcellstyle.checkboxchecked')
else img := sb.FindStyleResource('checkcellstyle.checkboxunchecked');
if assigned(img) then
begin
aRectf:=Bounds;
arectf.Left:=arectf.Right-20; // to adapt
TStyleObject(img).DrawToCanvas(Canvas,arectf,1);
end
else begin
img:= sb.FindStyleResource('checkcellstyle');
if assigned(img) then img:=img.FindStyleResource('checkmark');
if assigned(img) and (img is Tpath) then
begin
if Test[Test.Find(Column.Index,Row)].ischecked
then Tpath(img).Fill.Color:=TColorAnimation(Img.Children[0]).StopValue
else Tpath(img).Fill.Color:=TColorAnimation(Img.Children[0]).StartValue;
var bmp:=Tpath(Img).MakeScreenshot;
aRectf:=Bounds;
arectf.Left:=arectf.Right-20; // to adapt
Canvas.DrawBitmap(bmp,Trectf.Create(0,0,bmp.Width,bmp.Height),aRectf,1);
end;
end;
end;
end;
但这不是问题。
这里有两个截图,证明了我的代码是有效的。第一,我使用transparent.style作为默认平台。
然后我添加C:\Users\Public\Documents\Embarcadero\Studio\22.0\Styles\Android\AndroidWearDarkBlue.fsf,得到这个
对我来说,在Android上还不清楚的是如何检查/检查Android上的价值。有时CellDblclick事件起作用,有时不起作用!
在我的“脏”版本中进行编辑,我解释说,您必须管理所选单元格的列表。测试变量是TSelectedCells列表。
这是我的单元格列表的版本。
TCell = class
col : integer;
raw : integer;
private
Fischecked: boolean;
procedure Setischecked(const Value: boolean);
public
property ischecked : boolean read Fischecked write Setischecked;
constructor Create(acol,araw : Integer; ischecked : boolean = false);
end;
TSelectedCells = class(TList<TCell>)
public
function Find(const aCol,araw : integer): Integer;
end;
{ TSelectedCells }
function TSelectedCells.Find(const aCol, araw: integer): Integer;
begin
for Result := 0 to Count-1 do
if (Self[Result].col=aCol) AND (Self[Result].raw=araw) then
exit;
Result := -1;
end;
发布于 2022-07-18 16:25:51
您不能这样做,因为Cellscol是一个字符串,并且不支持单元格选择。但是,如果您在某个地方管理所选单元格的列表并使用OnDrawCellEvent,则有可能。
这里是一个“脏”示例,但您可以看到我使用的样式:
我是如何处理这件事的
procedure TForm1.FormCreate(Sender: TObject);
begin
SelectedCells:=TList<String>.Create(['0,2']);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SelectedCells.Free;
end;
procedure TForm1.StringGrid1CellDblClick(const Column: TColumn;
const Row: Integer);
begin
if SelectedCells.IndexOf(Format('%d,%d',[Column.Index,Row]))>=0
then SelectedCells.Remove(Format('%d,%d',[Column.Index,Row]))
else SelectedCells.Add(Format('%d,%d',[Column.Index,Row]));
end;
procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var sb : TFMXObject;
aRectF : TRectF;
begin
if Assigned(Stylebook) then sb:=Stylebook.Style else sb:=TStyleManager.ActiveStyle(Self);
if SelectedCells.IndexOf(Format('%d,%d',[Column.Index,Row]))>=0 then
begin
var img := sb.FindStyleResource('checkcellstyle.checkboxchecked');
if assigned(img) then
begin
aRectf:=Bounds;
arectf.Left:=arectf.Right-20; // to adapt
TStyleObject(img).DrawToCanvas(Canvas,arectf,1);
end;
end;
end;
注意:我认为并不是所有的风格都可以使用。
发布于 2022-07-20 12:05:29
我重新研究了SergeGirard的答案
unit Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Rtti,
FMX.Grid.Style, FMX.Grid, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Edit,
System.Generics.Collections, FMX.Styles, FMX.Styles.Objects, FMX.StdCtrls,
FMX.Objects;
Const
CHECKBOX_COLUMN = 0;
CHECKBOX_ROW = 2;
SYSTEM_ON = $101;
SYSTEM_OFF = $100;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
CurrencyColumn1: TCurrencyColumn;
StyleBook1: TStyleBook;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
const Column: TColumn; const Bounds: TRectF; const Row: Integer;
const Value: TValue; const State: TGridDrawStates);
procedure Grid1GetValue(Sender: TObject; const ACol, ARow: Integer;
var Value: TValue);
procedure FormDestroy(Sender: TObject);
procedure StringGrid1CellClick(const Column: TColumn; const Row: Integer);
procedure StringGrid1SelectCell(Sender: TObject; const ACol, ARow: Integer;
var CanSelect: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
SelectedCells: TList<string>;
implementation
{$R *.fmx}
{$R *.LgXhdpiTb.fmx ANDROID}
{$R *.LgXhdpiPh.fmx ANDROID}
{$R *.Windows.fmx MSWINDOWS}
{$R *.NmXhdpiPh.fmx ANDROID}
{$R *.SmXhdpiPh.fmx ANDROID}
procedure TForm1.FormCreate(Sender: TObject);
Var
iRow: Integer;
MyStyle: TFmxObject;
begin
for iRow := 0 to 10 do
StringGrid1.Cells[0, iRow] := iRow.ToString;
StringGrid1.Cells[CHECKBOX_COLUMN, CHECKBOX_ROW] := SYSTEM_OFF.ToString;
SelectedCells := TList<String>.Create
([CHECKBOX_COLUMN.ToString + ',' + CHECKBOX_ROW.ToString]);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SelectedCells.Free;
end;
procedure TForm1.Grid1GetValue(Sender: TObject; const ACol, ARow: Integer;
var Value: TValue);
Var
i: Integer;
begin
//Fill column with some numbers
for i := 1 to 10 do
if (ACol = 0) AND (ARow = i - 1) then
Value := i;
end;
procedure TForm1.StringGrid1CellClick(const Column: TColumn;
const Row: Integer);
begin
if (Column.Index = CHECKBOX_COLUMN) AND (Row = CHECKBOX_ROW) then
Begin
If StringGrid1.Cells[CHECKBOX_COLUMN, CHECKBOX_ROW] =
SYSTEM_OFF.ToString
then
StringGrid1.Cells[CHECKBOX_COLUMN, CHECKBOX_ROW] :=
SYSTEM_ON.ToString
else
StringGrid1.Cells[CHECKBOX_COLUMN, CHECKBOX_ROW] :=
SYSTEM_OFF.ToString;
End;
StringGrid1.Repaint;
end;
procedure TForm1.StringGrid1DrawColumnCell(Sender: TObject;
const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
sb: TFmxObject;
aRectF: TRectF;
img: TFmxObject;
begin
if Assigned(Stylebook) then
sb := Stylebook.Style
else
sb := TStyleManager.ActiveStyle(Self);
if SelectedCells.IndexOf(Format('%d,%d', [Column.Index, Row])) >= 0 then
begin
if StringGrid1.Cells[CHECKBOX_COLUMN, CHECKBOX_ROW].ToInteger = SYSTEM_ON
then
Begin
img := sb.FindStyleResource('checkcellstyle.checkboxchecked');
End
else
Begin
img := sb.FindStyleResource('checkcellstyle.checkboxunchecked');
End;
if Assigned(img) then
begin
aRectF := Bounds;
aRectF.Left := aRectF.Right - 90; // to adapt
TStyleObject(img).DrawToCanvas(Canvas, aRectF, 1);
StringGrid1.Repaint;
end;
end;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject;
const ACol, ARow: Integer; var CanSelect: Boolean);
begin
if (ACol = CHECKBOX_COLUMN) AND (ARow = CHECKBOX_ROW) then
CanSelect := False
else
CanSelect := true;
end;
end.`
如图片所示,这在windows 32上工作得很好。然而,在安卓系统上却没有。实际上,它与样式有关(从未找到checkcellstyle.checkboxchecked)。我找不到Android的工作风格,有什么建议吗?
https://stackoverflow.com/questions/73023973
复制相似问题