Created
February 3, 2012 03:10
-
-
Save xingfuqiu/1727388 to your computer and use it in GitHub Desktop.
Delphi:FWatermarkHint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyEdit.pas | |
// 版权所有(C) 邱兴福 | |
// Homepage: http://xingfuqiu.com | |
// Email: [email protected] | |
// purpose: | |
// 您可以以任何方式使用本代码,如果您对本代码不满, | |
// 您可以将其粉碎。您也可以删除版权信息和作者联系方式。 | |
// 如果您给我一个进步的机会,我将万分感谢。 | |
///////////////////////////////////////////////////////////////////////////////// | |
unit MyEdit; | |
interface | |
uses Messages, Classes, StdCtrls, Controls, Graphics; | |
type | |
TMyEdit = class(TEdit) | |
private | |
FCanvas: TControlCanvas; | |
FWatermarkHint: string; | |
protected | |
procedure WMPaint(var Message: TWMPaint); message WM_PAINT; | |
procedure CMExit(var Message: TCMExit); message CM_EXIT; | |
public | |
constructor Create(AOwner: TComponent); override; | |
destructor Destroy; override; | |
published | |
property WatermarkHint: string read FWatermarkHint write FWatermarkHint; | |
end; | |
implementation | |
{ TMyEdit } | |
procedure TMyEdit.CMExit(var Message: TCMExit); | |
begin | |
inherited; | |
Perform(WM_PAINT, 0, 0); | |
end; | |
constructor TMyEdit.Create(AOwner: TComponent); | |
begin | |
inherited Create(AOwner); | |
FCanvas := TControlCanvas.Create; | |
FCanvas.Control := Self; | |
end; | |
destructor TMyEdit.Destroy; | |
begin | |
FCanvas.Free; | |
inherited; | |
end; | |
procedure TMyEdit.WMPaint(var Message: TWMPaint); | |
begin | |
inherited; | |
if (not Focused) and (Self.Text = '') and (FWatermarkHint <> '') then | |
begin | |
FCanvas.Font := Self.Font; | |
FCanvas.Font.Color := clGrayText; | |
FCanvas.TextRect(Self.ClientRect, 1, 1, FWatermarkHint); | |
end; | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment