下面的代码已能实现大部分功能,我现在想解决的是,在“月”和“日”的edit移动后,整体控件不会刷新,就会在屏幕上有原来位置的图存在,但通过“年”edit移动整个控件时,就能实现从新从画控件,怎么才能在移动后两个edit后,让整个控件从画。
-------------------------------------
unit MEdits;
interface
uses
SysUtils, Classes, Controls,Stdctrls,Windows,messages,
Graphics, Forms, Dialogs,
ExtCtrls;
type
TGlassStyle = (
gsBlackness, gsDstInvert, gsMergeCopy, gsMergePaint, gsNotSrcCopy,
gsNotSrcErase, gsPatCopy, gsPatInvert, gsPatPaint, gsSrcAnd,
gsSrcCopy, gsSrcErase, gsSrcInvert, gsSrcPaint, gsWhiteness);
TMEdits = class(TCustomControl)
private
FYearEdit:TEdit;
FMonthEdit:TEdit;
FDayEdit:TEdit;
FCanMove:Boolean; //是否可以移动距离的变量
FColor: TColor;
FStyle: TGlassStyle;
FOnPaint: TNotifyEvent;
procedure SetColor(Value: TColor);
procedure SetStyle(Value: TGlassStyle);
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
procedure WMEraseBkgnd(var Message: TMessage); message WM_ERASEBKGND;
procedure WMWindowPosChanging(var Message: TWMWindowPosChanging); message WM_WINDOWPOSCHANGING;
///数值检查函数
Function CheckYear(Year:integer):Boolean;
Function CheckMonth(Month:integer):Boolean;
Function CheckDay(Day:integer):Boolean;
/////录入数据的类型检查
procedure CheckNum(Sender: TObject; var Key: Char); //检查键入的字符是否是数字类型
Function GetCanMove:Boolean;
procedure SetCanMove(flg:Boolean);
///////使控件能移动的onmousemove过程
procedure MoveYear(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure MoveMonth(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure MoveDay(Sender: TObject; Shift: TShiftState; X,Y: Integer);
{ Private declarations }
protected
Buffer: TBitmap;
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure Resize; override;
procedure ExitCheckYear(Sender:TObject);
procedure ExitCheckMonth(Sender:TObject);
procedure ExitCheckDay(Sender:TObject);
public
{ Public declarations }
constructor Create(Aowner:TComponent);override;
destructor Destroy;override;
property Canvas;
property YearEdit: TEdit read FYearEdit;
property MonthEdit:TEdit read FMonthEdit;
property DayEdit:TEdit read FDayEdit;
published
{ Published declarations }
property CanMove:Boolean read GetCanMove write SetCanMove;
property Align;
property Anchors;
property AutoSize;
property BiDiMode;
property BorderWidth;
property Color: TColor read FColor write SetColor;
property Ctl3D;
property Enabled;
property Style: TGlassStyle read FStyle write SetStyle default gsSrcAnd;
property Visible;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDrag;
property OnResize;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMEdits]);
end;
function GlassStyleToInt(gs: TGlassStyle): LongInt;
begin
case gs of
gsBlackness : Result := cmBlackness;
gsDstInvert : Result := cmDstInvert;
gsMergeCopy : Result := cmMergeCopy;
gsMergePaint : Result := cmMergePaint;
gsNotSrcCopy : Result := cmNotSrcCopy;
gsNotSrcErase: Result := cmNotSrcErase;
gsPatCopy : Result := cmPatCopy;
gsPatInvert : Result := cmPatInvert;
gsPatPaint : Result := cmPatPaint;
gsSrcAnd : Result := cmSrcAnd;
gsSrcCopy : Result := cmSrcCopy;
gsSrcErase : Result := cmSrcErase;
gsSrcInvert : Result := cmSrcInvert;
gsSrcPaint : Result := cmSrcPaint;
gsWhiteness : Result := cmWhiteness;
else Assert(True, 'Error parameter in function GlassStyleToInt');
end;
end;
{ TMEdits }
function TMEdits.CheckDay(Day: integer): Boolean;
begin
if Day<1 then Result:=false;
if Day>31 then Result:=false;
Result:=true;
end;
function TMEdits.CheckMonth(Month: integer): Boolean;
begin
if Month<1 then Result:=false;
if Month>12 then Result:=false;
Result:=true;
end;
procedure TMEdits.CheckNum(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9']) then
begin
MessageBeep(0);
Key:=#0;
end;
if key=#13 then
begin
key:=#0;
postmessage(TEdit(Sender).Handle,wm_nextdlgctl,0,0);
end;
end;
function TMEdits.CheckYear(Year: integer): Boolean;
begin
if Year<1900 then Result:=false;
if Year>9999 then Result:=false;
Result:=true;
end;
procedure TMEdits.CMCtl3DChanged(var Message: TMessage);
begin
RecreateWnd;
end;
constructor TMEdits.Create(Aowner: TComponent);
begin
inherited;
FYearEdit:=TEdit.Create(self);
FYearEdit.Parent:=self;
FYearEdit.Width:=33;
FYearEdit.Height:=21;
FYearEdit.Top:=0;
FYearEdit.Left:=0;
FYearEdit.ShowHint:=true;
FYearEdit.Hint:='年';
FYearEdit.OnKeyPress:=CheckNum;
FYearEdit.OnExit:=ExitCheckYear;
FYearEdit.OnMouseMove:=MoveYear;
FMonthEdit:=TEdit.Create(self);
FMonthEdit.Parent:=self;
FMonthEdit.Width:=21;
FMonthEdit.Height:=21;
FMonthEdit.Top:=0;
FMonthEdit.ShowHint:=true;
FMonthEdit.Hint:='月';
FMonthEdit.OnKeyPress:=CheckNum;
FMonthEdit.Left:=FYearEdit.Width+10;
FMonthEdit.OnExit:=ExitCheckMonth;
FMonthEdit.OnMouseMove:=MoveMonth;
FDayEdit:=TEdit.Create(self);
FDayEdit.Parent:=self;
FDayEdit.Width:=21;
FDayEdit.Height:=21;
FDayEdit.Top:=0;
FDayEdit.ShowHint:=true;
FDayEdit.Hint:='日';
FDayEdit.OnKeyPress:=CheckNum;
FDayEdit.Left:=FMonthEdit.Left+FMonthEdit.Width+10;
FDayEdit.OnExit:=ExitCheckDay;
FDayEdit.OnMouseMove:=MoveDay;
Buffer := TBitmap.Create;
ControlStyle := [csAcceptsControls, csCaptureMouse, csClickEvents,
csDoubleClicks, csReplicatable];
FStyle := gsSrcAnd;
ParentCtl3d := False;
Ctl3D := False;
ParentColor := False;
FColor := clWhite;
self.Width:=400;
self.Height:=22;
//self.FDoubleBuffered:=true;
end;
procedure TMEdits.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle + WS_EX_TRANSPARENT;
end;
destructor TMEdits.Destroy;
begin
Buffer.Free;
FYearEdit:=nil;
FMonthEdit:=nil;
FDayEdit:=nil;
inherited;
end;
procedure TMEdits.ExitCheckDay(Sender: TObject);
var
TmpValue:integer; //得到edit中的输入字符转换成的整数
begin
TmpValue:=StrToInt(TEdit(Sender).Text);
if not CheckDay(TmpValue) then
begin
showmessage('日期中的天数输入错误,请从新输入');
TEdit(Sender).Text:='';
TEdit(Sender).SetFocus;
end;
end;
procedure TMEdits.ExitCheckMonth(Sender: TObject);
var
TmpValue:integer; //得到edit中的输入字符转换成的整数
begin
TmpValue:=StrToInt(TEdit(Sender).Text);
if not CheckMonth(TmpValue) then
begin
showmessage('日期中的月份输入错误,请从新输入');
TEdit(Sender).Text:='';
TEdit(Sender).SetFocus;
end;
end;
procedure TMEdits.ExitCheckYear(Sender: TObject);
var
TmpValue:integer; //得到edit中的输入字符转换成的整数
begin
TmpValue:=StrToInt(TEdit(Sender).Text);
if not CheckYear(TmpValue) then
begin
showmessage('日期中的年份输入错误,请从新输入');
TEdit(Sender).Text:='';
TEdit(Sender).SetFocus;
end;
end;
function TMEdits.GetCanMove: Boolean;
begin
Result:=FCanMove;
end;
procedure TMEdits.MoveDay(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not FCanMove then Exit;
ReleaseCapture;
TEdit(sender).Perform(WM_SYSCOMMAND,$F012,0);
FDayEdit.Top:=FYearEdit.Top;
FMonthEdit.Top:=FYearEdit.Top;
if FDayEdit.Left<FMonthEdit.Left+FMonthEdit.Width+1 then
begin
FDayEdit.Left:=FMonthEdit.Left+FMonthEdit.Width+10;
end;
if FMonthEdit.Left<FYearEdit.Left+FYearEdit.Width+1 then
begin
FMonthEdit.Left:=FYearEdit.Left+FYearEdit.Width+10;
end;
//self.RecreateWnd;
end;
procedure TMEdits.MoveMonth(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not FCanMove then Exit;
ReleaseCapture;
TEdit(Sender).Perform(WM_SYSCOMMAND,$F012,0);
FMonthEdit.Top:=FYearEdit.Top;
FDayEdit.Top:=FYearEdit.Top;
if FMonthEdit.Left<FYearEdit.Left+FYearEdit.Width+1 then
begin
FMonthEdit.Left:=FYearEdit.Left+FYearEdit.Width+10;
end;
if FDayEdit.Left<FMonthEdit.Left+FMonthEdit.Width+1 then
begin
FDayEdit.Left:=FMonthEdit.Left+FMonthEdit.Width+10;
end;
//self.RecreateWnd;
end;
procedure TMEdits.MoveYear(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not FCanMove then Exit;
ReleaseCapture;
self.Perform(WM_SYSCOMMAND,$F012,0);
FMonthEdit.Top:=FYearEdit.Top;
FDayEdit.Top:=FYearEdit.Top;
end;
procedure TMEdits.Paint;
var
R: TRect;
rop: LongInt;
begin
R := Rect(0, 0, Width, Height);
Buffer.Width := Width;
Buffer.Height := Height;
Buffer.Canvas.Brush.Style := bsSolid;
Buffer.Canvas.Brush.Color := FColor;
Buffer.Canvas.FillRect(Rect(0, 0, Width, Height));
rop := GlassStyleToInt(FStyle);
StretchBlt(Buffer.Canvas.Handle, 0, 0, Width, Height,
Canvas.Handle, 0, 0, Width, Height, rop);
if Ctl3D then DrawEdge(Buffer.Canvas.Handle, R, BDR_RAISEDINNER, BF_RECT);
Buffer.Canvas.Pen.Mode := pmCopy;
Buffer.Canvas.Pen.Style := psSolid;
Canvas.Draw(0, 0, Buffer);
if Assigned(FOnPaint) then FOnPaint(Self);
end;
procedure TMEdits.Resize;
begin
Invalidate;
inherited;
end;
procedure TMEdits.SetCanMove(flg: Boolean);
begin
if flg=FCanMove then Exit;
FCanMove:=flg;
end;
procedure TMEdits.SetColor(Value: TColor);
begin
if Value <> FColor then
begin
FColor := Value;
RecreateWnd;
end;
end;
procedure TMEdits.SetStyle(Value: TGlassStyle);
begin
if Value <> FStyle then
begin
FStyle := Value;
RecreateWnd;
end;
end;
procedure TMEdits.WMEraseBkgnd(var Message: TMessage);
begin
Message.Result := 0;
end;
procedure TMEdits.WMWindowPosChanging(var Message: TWMWindowPosChanging);
begin
Invalidate;
inherited;
end;
end.