看了觉得好一定要给我加分啊!<br>//////////////////////////////////////////////////////////////////////<br>//兩個控件對齊需要的偏移量=GetSystemMetrics(SM_CYSIZEFRAME)-1<br>//x/y方向相同,即FillPanel1.x = DragPanel1.x + GetSystemMetrics(SM_CYSIZEFRAME)-1<br><br>unit cDragPanel;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> ExtCtrls;<br><br>type<br> TDragPanel = class(TPanel)<br> private<br> xCount,yCount : Integer;<br> FFontSize : Integer;<br> procedure CreateParams(var Params: TCreateParams); override;<br> procedure CalcFrameSize;<br> protected<br> procedure WMSize(var Message: TWMSize); message WM_Size;<br> Procedure Paint; override; {Drawing the panel}<br> public<br> constructor Create(AOwner: TComponent); override;<br> procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;<br> procedure SetFontSize(FontSize : Integer);<br> published<br> { Published declarations }<br> end;<br><br> TFillPanel = class(TPanel)<br> private<br> xCount,yCount : Integer;<br> FFontSize : Integer;<br> procedure CalcFrameSize;<br> protected<br> procedure WMSize(var Message: TWMSize); message WM_Size;<br> Procedure Paint; override; {Drawing the panel}<br> public<br> constructor Create(AOwner: TComponent); override;<br> procedure SetFontSize(FontSize : Integer);<br> published<br> { Published declarations }<br> end;<br><br>procedure Register;<br><br>implementation<br><br><br>procedure Register;<br>begin<br> RegisterComponents('ABMCTRL', [TDragPanel]);<br> RegisterComponents('ABMCTRL', [TFillPanel]);<br>end;<br><br>procedure TDragPanel.CalcFrameSize;<br>begin<br> xCount := (ClientWidth-2) div FFontSize;<br> yCount := (ClientHeight-2) div FFontSize;<br> ClientWidth := ClientWidth - (ClientWidth-2) mod FFontSize;<br> ClientHeight := ClientHeight - (ClientHeight-2) mod FFontSize;<br>end;<br><br>constructor TDragPanel.Create(AOwner: TComponent);<br>begin<br> inherited;<br> xCount := 0;<br> yCount := 0;<br> Font.Size := 9;<br> Canvas.Font.Size := 9;<br> FFontSize := -Canvas.Font.Height;<br>end;<br><br>procedure TDragPanel.CreateParams;<br>begin<br> inherited CreateParams(Params);<br> with Params do Style := Style or WS_SIZEBOX ; { enable resize }<br>end;<br><br>procedure TDragPanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);<br>begin<br> inherited;<br> ReleaseCapture;<br> perform(WM_SysCommand, SC_MOVE or 2, 0); { enable move }<br>end;<br><br>procedure TDragPanel.Paint;<br>var<br> R : TRect;<br> i,j : Integer;<br>begin<br> inherited;<br><br> R := GetClientRect;<br> with Canvas do<br> begin<br> { fill background }<br> Brush.Color := clWhite;<br> FillRect(R);<br><br> For i:= 0 to yCount-1 do<br> For j:=0 to xCount-1 do<br> TextOut(1+j*FFontSize,1+i*FFontSize,'汉');<br><br> Brush.Color := clBlack;<br> FrameRect(R);<br> end;<br>end;<br><br>procedure TDragPanel.SetFontSize(FontSize: Integer);<br>begin<br> FFontSize := FontSize;<br> CalcFrameSize;<br> Invalidate;<br>end;<br><br>procedure TDragPanel.WMSize(var Message: TWMSize);<br>begin<br> inherited;<br> CalcFrameSize;<br>end;<br><br>{ TFillPanel }<br><br>procedure TFillPanel.CalcFrameSize;<br>begin<br> xCount := (ClientWidth-2) div FFontSize;<br> yCount := (ClientHeight-2) div FFontSize;<br> ClientWidth := ClientWidth - (ClientWidth-2) mod FFontSize;<br> ClientHeight := ClientHeight - (ClientHeight-2) mod FFontSize;<br>end;<br><br>constructor TFillPanel.Create(AOwner: TComponent);<br>begin<br> inherited;<br> xCount := 0;<br> yCount := 0;<br> Font.Size := 9;<br> Canvas.Font.Size := 9;<br> FFontSize := -Canvas.Font.Height;<br>end;<br><br>procedure TFillPanel.Paint;<br>var<br> R : TRect;<br> i,j : Integer;<br>begin<br> inherited;<br><br> R := GetClientRect;<br> with Canvas do<br> begin<br> { fill background }<br> Brush.Color := clWhite;<br> FillRect(R);<br><br> For i:= 0 to yCount-1 do<br> For j:=0 to xCount-1 do<br> TextOut(1+j*FFontSize,1+i*FFontSize,'汉');<br><br> Brush.Color := clBlack;<br> FrameRect(R);<br> end;<br>end;<br><br>procedure TFillPanel.SetFontSize(FontSize: Integer);<br>begin<br> FFontSize := FontSize;<br> CalcFrameSize;<br> Invalidate;<br>end;<br><br>procedure TFillPanel.WMSize(var Message: TWMSize);<br>begin<br> inherited;<br> CalcFrameSize;<br>end;<br><br>end.