| Topページ | お問い合わせ | 2008/01/17 更新 |
ここでは、CodeGear(旧Borland)のWin32アプリケーション開発ツール「Delphi」に関するTipsを紹介します。
| 分類 | 主な内容 |
|---|---|
| 位置やサイズ | TPoint、TSize |
| TEdit | 入力制限 |
| TMemo | タブサイズや余白など |
| TTreeView | 行の高さ、Drag、右クリックで選択ノードを変更 |
| TStringGrid | 右クリックで選択セルを変更 |
| ちらつき抑止 | コントロール更新中のちらつきを抑える。 |
TPoint
画面上の位置を定義します。(X: Longint; Y:Longint)
TSize
幅と高さを定義します。(cx: Longint;cy: Longint)
TRect
長方形を定義します。(Left, Top, Right, Bottom:Integer)
// キープレスイベント
procedure TForm1.Edit1KeyPress(Sender: TObject;var Key: Char);
begin
if (key in ['0'..'9']) or (Key = #8) then // 数値とBackSpaceのみOK
else
Key := #0;
end;
但し、クリップボードからの貼り付けは制限できませんので、最終的な数値チェックは必要となります。
TabSize := TabSize * 4; // integer 型 SendMessage(Memo1.Handle, EM_SETTABSTOPS, 1, LPARAM(@TabSize));余白を変更する。
MemoRect := Memo1.ClientRect; // TRect 型 // 第2パラメタに左右の余白、第3パラメタに上下の余白 InflateRect(MemoRect, 0-LRBlank, 0-TBBlank); Memo1.Perform(EM_SETRECT, 0, Longint(@MemoRect));行間を変更する。
Memo1.SelectAll;
ParaFmt.cbSize := SizeOf(TParaFormat2); // TParaFormat2 型
ParaFmt.dwMask := PFM_LINESPACING;
ParaFmt.dyLineSpacing := Round(LineSpaceValue /
Screen.PixelsPerInch * 1440);
ParaFmt.bLineSpacingRule := 4; // 4のとき行間はTwips値
SendMessage(Memo1.Handle, EM_SETPARAFORMAT, 0, LongInt(@ParaFmt));
TreeView_SetItemHeight(TreeView1.Handle,16); // 16ピクセルに変更行の高さを求める。
TreeView_GetItemHeight(TreeView1.Handle); // 戻り値はinteger型BeginDrag(False) でも、すぐに Drag が始まる。
if DragImageList <> nil thenを
if ( ActiveDrag <> dopNone ) and ( DragImageList <> nil ) thenに変更します。
TreeView1.Selected := TreeView1.Selected;とすると、変なコードですが選択されるようになります。
procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
w_Col,w_Row,w_TRow : integer;
begin
if Button = mbRight then
begin
(Sender as TStringGrid).MouseToCell(X,Y,w_Col,w_Row);
if (w_Row >= (Sender as TStringGrid).FixedCols) and
(w_Col >= (Sender as TStringGrid).FixedRows) then
begin
// 先頭行を退避しておく(スクロールさせない対応)
w_TRow := (Sender as TStringGrid).TopRow;
(Sender as TStringGrid).Col := w_Col;
// 以下は垂直スクロールさせない対応
SendMessage((Sender as TStringGrid).Handle,WM_SETREDRAW,0,0);
(Sender as TStringGrid).Row := w_Row;
(Sender as TStringGrid).TopRow := w_TRow;
SendMessage((Sender as TStringGrid).Handle,WM_SETREDRAW,1,0);
(Sender as TStringGrid).Refresh;
end
else;
end
else;
end;
Memo1.Lines.BeginUpdate;
TreeView1.Items.BeginUpdate;
StringGrid1.Rows[0].BeginUpdate; // 行番号は停止と開始で合わせておく
try
// 文字列の更新処理
finally
StringGrid1.Rows[0].EndUpdate;
TreeView1.Items.EndUpdate;
Memo1.Lines.EndUpdate;
end;
ListBox など、TWinControl 継承のコンポーネントの描画を抑止するSendMessage(ListBox1.Handle,WM_SETREDRAW,0,0); // 描画停止 // Itemの設定など SendMessage(ListBox1.Handle,WM_SETREDRAW,1,0); // 描画再開 // TStringGrid に適用する場合はStringGrid1.Refresh; が必要です。以下のような方法もあるようです(メモリ使用量の節約になるらしいです)。
LockWindowUpdate(ListBox1.Handle); // 描画停止 // Itemの設定など LockWindowUpdate(0); // 描画再開