- くだすれDelphi(超初心者用)その56
38 : ◆rW3tmvmDLc [sage]:2014/08/29(金) 16:36:09.72 ID:EE6o1/jo - Windows標準のコントロールの挙動で苦しめられるよりは自分で作ってしまった方が楽なことがあります。
全部作ると大変ですがグリッドコンポを使ってそれらしいものを作ってみました。イカソース(1/5)。 type { フォームにDrawGridを置き、AlignをalClientに、DefaultDrawingをFalseに、FixedColsとFixedRowsを0に。} TForm1 = class(TForm) DrawGrid1: TDrawGrid; (※中略) private { Private 宣言 } FPushed: boolean; // ボタン凹み中 FPushedCol, FPushedRow: integer; // どのセルのボタンが凹んでいるか procedure PushDown(ACol, ARow: integer); procedure PushUp; procedure InvalidateCell(ACol, ARow: Integer); procedure DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure ButtonClicked(ACol, ARow: Integer); procedure ResizeCells; end; (※中略) // 偽ボタンクリック時の処理。 procedure TForm1.ButtonClicked(ACol, ARow: Integer); var msg: string; begin msg := format('[%d,%d]のボタンが押されたよ!', [ACol, ARow]); Application.MessageBox(PChar(msg), 'ボタンが押されたよ!'); end;
| - くだすれDelphi(超初心者用)その56
39 :(2/5) ◆rW3tmvmDLc [sage]:2014/08/29(金) 16:37:03.78 ID:EE6o1/jo - // 指定されたセルを描画します。
procedure TForm1.DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); const cPushedOffset = 2; var IsThisCellPushed: boolean; uState: uint; str: string; begin str := format('[%d, %d]', [ACol, ARow]); // このボタンは押されている? IsThisCellPushed := (FPushed and (ACol = FPushedCol) and (ARow = FPushedRow)); // ボタンそっくりの絵を描く。 uState := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT; if IsThisCellPushed then uState := uState or DFCS_PUSHED; Windows.DrawFrameControl(DrawGrid1.Canvas.Handle, Rect, DFC_BUTTON, uState); // ボタン文字列の描画。 DrawGrid1.Canvas.Brush.Style := bsClear; if IsThisCellPushed then Windows.OffsetRect(Rect, cPushedOffset, cPushedOffset); Windows.DrawText(DrawGrid1.Canvas.Handle, PChar(str), Length(Str), Rect, DT_NOPREFIX or DT_CENTER or DT_SINGLELINE or DT_VCENTER); if IsThisCellPushed then Windows.OffsetRect(Rect, -cPushedOffset, -cPushedOffset); // 現在位置のボタンならフォーカスの点線枠を描く。 if gdFocused in State then begin DrawGrid1.Canvas.Brush.Style := bsSolid; Windows.DrawFocusRect(DrawGrid1.Canvas.Handle, Rect); end; end;
| - くだすれDelphi(超初心者用)その56
40 :(3/5) ◆rW3tmvmDLc [sage]:2014/08/29(金) 16:37:47.29 ID:EE6o1/jo - // グリッドのOnDrawCellイベントハンドラ
procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin DrawCell(ACol, ARow, Rect, State); end; // グリッドのOnKeyDownイベントハンドラ procedure TForm1.DrawGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_SPACE then PushDown(DrawGrid1.Col, DrawGrid1.Row); end; // グリッドのOnKeyUpイベントハンドラ procedure TForm1.DrawGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_SPACE then PushUp; end; // グリッドのOnMouseDownイベントハンドラ procedure TForm1.DrawGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var MouseCol, MouseRow: integer; begin if Button = mbLeft then begin DrawGrid1.MouseToCell(X, Y, MouseCol, MouseRow); PushDown(MouseCol, MouseRow); end; end;
| - くだすれDelphi(超初心者用)その56
41 :(4/5) ◆rW3tmvmDLc [sage]:2014/08/29(金) 16:38:41.67 ID:EE6o1/jo - // グリッドのOnMouseUpイベントハンドラ
procedure TForm1.DrawGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbLeft then PushUp; end; // フォームのOnResizeイベントハンドラ procedure TForm1.FormResize(Sender: TObject); begin ResizeCells; end; // 指定されたセルの再描画をWindowsにお願いする。 procedure TForm1.InvalidateCell(ACol, ARow: Integer); var aCellRect: TRect; begin aCellRect := DrawGrid1.CellRect(FPushedCol, FPushedRow); InvalidateRect(DrawGrid1.Handle, @aCellRect, True); end; // ボタンを凹ます。 procedure TForm1.PushDown(ACol, ARow: integer); begin FPushed := True; FPushedCol := ACol; FPushedRow := ARow; InvalidateCell(ACol, ARow); end;
| - くだすれDelphi(超初心者用)その56
42 :(5/5) ◆rW3tmvmDLc [sage]:2014/08/29(金) 16:43:51.55 ID:EE6o1/jo - // 凹ませたボタンを戻す。
procedure TForm1.PushUp; begin FPushed := False; InvalidateCell(FPushedCol, FPushedRow); ButtonClicked(FPushedCol, FPushedRow); end; // ボタン群のサイズ調整。 procedure TForm1.ResizeCells; begin with DrawGrid1 do begin DefaultColWidth := (ClientWidth - GridLineWidth * (ColCount - 1)) div ColCount; DefaultRowHeight := (ClientHeight - GridLineWidth * (RowCount - 1)) div RowCount; end; end; 以上です。Turbo Delphi Explorer / Windows 7 (64bit)で作りましたが古いバージョン、古いOSでも動くと思います。 このサンプルではDrawGridを使っていますが StringGridを使ってボタンのテキストをCellsプロパティに持たせてもよいでしょう。
|
|