带标题的编辑框

unit ExEdit;

interface

uses
  System.Classes, Vcl.Controls, Winapi.Windows, Vcl.Graphics, Vcl.StdCtrls,
  System.SysUtils, Winapi.messages;

type

  TBorders = class(TPersistent)
  private
    FRight: Boolean;
    FBottom: Boolean;
    FTop: Boolean;
    FLeft: Boolean;
    FPen: TPen;
  public
    constructor Create;
    destructor Destroy; override;
  published
    property Pen: TPen read FPen write FPen;
    property Left: Boolean read FLeft write FLeft;
    property Right: Boolean read FRight write FRight;
    property Top: Boolean read FTop write FTop;
    property Bottom: Boolean read FBottom write FBottom;
  end;

  TAlterMode = (alterNone, alterFont, alterHeight);

  TExEdit = class(TWinControl)
  private
    FTitle: TCaption;
    FTitleLength: Integer;
    FLines: string;
    fAlterMode: TAlterMode;
    FBorders: TBorders;
    fMinHeight: Integer;
    fMaxFont: Integer;
    fOldText: string;
    fMinFont: Integer;
    fMaxHeight: Integer;
    procedure WMChar(var Msg: TWMChar); message WM_CHAR;
    procedure WMKeyDown(var Msg: TWMKeyDown); message WM_KEYDOWN;
    procedure WMPaint(var Msg: TWMPaint);message WM_PAINT;
    procedure setLines(const Value: string);
    procedure setTitle(const Value: TCaption);
    procedure Polyline(const Points: array of TPoint);
    function getSelection: TSelection;
    procedure checkMode(isRecursion: Boolean = False);
    procedure checkText;
    procedure setMaxHeight(const Value: Integer);
  protected
    { protected declarations }
    procedure CreateParams(var Params: TCreateParams); override;
    procedure Loaded();override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Font;
    property AlterMode: TAlterMode read fAlterMode write fAlterMode;
    property Borders: TBorders read FBorders write FBorders stored True;
    property Title: TCaption read FTitle write setTitle;
    property Lines: string read FLines write setLines;
    property MinFont: Integer read fMinFont write fMinFont default 12;
    property MaxHeight: Integer read fMaxHeight write setMaxHeight default 0;
  end;

implementation

{ TExEdit }

procedure TExEdit.checkMode(isRecursion: Boolean);
var
  vhdc: HDC;
  vidx,vpos,tmpH: Integer;
  vsize: TSize;
begin

  FLines := string(Text).Substring(FTitleLength);

  vhdc := GetDC(Self.Handle);
  vidx := Length(Text);
  vpos := Perform(EM_POSFROMCHAR,vidx - 1,0);
  SelectObject(vhdc, Font.Handle);
  Winapi.Windows.GetTextExtentPoint32(vhdc, ‘A‘, 1, vsize);
  tmpH := HiWord(vpos)+vsize.cy + 5;

  if fAlterMode = alterNone then
  begin
    if (vpos = -1) or (tmpH > Height) then
      Perform(WM_CHAR,VK_BACK,$E0001);
  end;

  if fAlterMode = alterFont then
  begin
    if (vpos = -1) or (tmpH > Height) then
    begin
      Font.Size := Font.Size - 1;
      if fMinFont > Font.Size then
      begin
        Font.Size := fMinFont;
        Perform(WM_CHAR,VK_BACK,$E0001);
      end else
        checkMode(True);
    end
    else
    begin
      if not isRecursion and (fMaxFont > Font.Size) then
      begin
        Font.Size := Font.Size + 1;
        checkMode;
      end;
    end;
  end;
  if fAlterMode = alterHeight then
  begin
    if (vpos = -1) or (tmpH > Height) then
    begin
      Height := tmpH;
      if (fMaxHeight > 0) and (fMaxHeight < height) then
      begin
        Height := fMaxHeight;
        Perform(WM_CHAR,VK_BACK,$E0001);
      end else
        checkMode;
    end
    else
    begin
      Height := tmpH;
      if fMinHeight > Height then
        Height := fMinHeight;
    end;
  end;
end;

procedure TExEdit.checkText;
begin
  if fOldText <> Text then
  begin
    fOldText := Text;
    checkMode;
  end;
end;

constructor TExEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBorders := TBorders.Create;
  FBorders.Left := True;
  FBorders.Right := True;
  FBorders.Top := True;
  FBorders.Bottom := True;
  fMinFont := 12;
  fMaxHeight := 0;
end;

procedure TExEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  CreateSubClass(Params, ‘EDIT‘);
  with Params do
  begin
    Style := Style or ES_MULTILINE;
    { 完全重画 }
    Style := Style and not WS_CLIPCHILDREN;
    Style := Style and not WS_CLIPSIBLINGS;
    { 增加透明 }
    ExStyle := ExStyle or WS_EX_TRANSPARENT;
  end;
end;

destructor TExEdit.Destroy;
begin
  FBorders.Free;
  inherited Destroy;
end;

function TExEdit.getSelection: TSelection;
begin
  SendMessage(Handle, EM_GETSEL, NativeInt(@Result.StartPos),
  NativeInt(@Result.EndPos));
end;

procedure TExEdit.Loaded;
begin
  inherited;
  fMinHeight := Height;
  fMaxFont := Font.Size;
end;

type
  PPoints = ^TPoints;
  TPoints = array[0..0] of TPoint;

procedure TExEdit.Polyline(const Points: array of TPoint);
var
  vhdc: HDC;
begin
  vhdc := GetDC(Self.Handle);
  SelectObject(vhdc,Borders.Pen.Handle);
  SetROP2(vhdc, R2_COPYPEN);
  Winapi.Windows.Polyline(vhdc, PPoints(@Points)^, High(Points) + 1);
end;

procedure TExEdit.setLines(const Value: string);
begin
  FLines := Value;
  Text := Title + Lines;
end;

procedure TExEdit.setMaxHeight(const Value: Integer);
begin
  fMaxHeight := Value;
  if (fMaxHeight > 0) and (fMaxHeight < height) then
    fMaxHeight := Height;
end;

procedure TExEdit.setTitle(const Value: TCaption);
begin
  FTitle := Value;
  FTitleLength := Length(FTitle);
  Text := Title + Lines;
end;

procedure TExEdit.WMChar(var Msg: TWMChar);
var
  canInherited: Boolean;
begin
  canInherited := False;
  case Msg.CharCode of
    VK_BACK:
      canInherited :=
        (getSelection.StartPos >= FTitleLength)
          and (getSelection.EndPos > FTitleLength)
          and (Msg.KeyData <> 0);
  else
    canInherited := getSelection.StartPos >= FTitleLength;
  end;
  if canInherited then
  begin
    inherited;
    checkText;
  end;
end;

procedure TExEdit.WMKeyDown(var Msg: TWMKeyDown);
var
  canInherited: Boolean;
begin
  canInherited := False;
  case Msg.CharCode of
    VK_DELETE:
      canInherited := getSelection.StartPos >= FTitleLength;
  else
    canInherited := True;
  end;
  if canInherited then
  begin
    inherited;
    checkText;
  end;
end;

procedure TExEdit.WMPaint(var Msg: TWMPaint);
begin
  inherited;
  if Borders.Bottom then
    Polyline([Point(0, Height-1), Point(Width - 1, Height-1)]);
  if Borders.Left then
    Polyline([Point(0, 0), Point(0, Height - 1)]);
  if Borders.Right then
    Polyline([Point(Width - 1, 0), Point(Width - 1, Height - 1)]);
  if Borders.Top then
    Polyline([Point(0, 0), Point(Width - 1, 0)]);
end;

{ TBorders }

constructor TBorders.Create;
begin
  FPen := TPen.Create;
end;

destructor TBorders.Destroy;
begin
  FPen.Free;
  inherited Destroy;
end;

end.

参考:http://www.cnblogs.com/key-ok/p/3380846.html

时间: 2024-10-09 14:38:50

带标题的编辑框的相关文章

Android一个简单的警告框,带标题、图标、按钮的代码

工作之余,将内容过程比较常用的内容做个珍藏,下面内容是关于Android一个简单的警告框,带标题.图标.按钮的内容,应该是对大伙有些用. AlertDialog alertDialog = new AlertDialog.Builder(this).create();alertDialog.setTitle("Title");alertDialog.setMessage("Message");alertDialog.setButton("OK",

取得窗口句柄和标题,跨类调用并实时显示在编辑框中(VS2010)

接上篇日志. 在WM_LBUTTONUP消息响应函数中写入代码: POINT pnt; WCHAR  wc_TargtWndTittle[200]; ::GetCursorPos(&pnt);      HWND h_TargetWnd = ::WindowFromPoint(pnt) ; //取得鼠标指针处窗口句柄;      ::GetWindowText(h_TargetWnd,wc_TargtWndTittle,200); 将wc_TargtWndTittle显示在对话框程序的编辑框控件

一个简单的记事本编辑框的实现以及搜集的一些窗口风格的预定义

这是一个简单的记事本的窗口过程 1 WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM 2 3 LOCAL winRect:RECT 4 LOCAL editWidth:DWORD 5 LOCAL editHeight:DWORD 6 7 .IF uMsg==WM_DESTROY 8 invoke PostQuitMessage,NULL 9 .ELSEIF uMsg==WM_CREATE 10 ;创建一个编辑框 11

java报表工具FineReport的公式编辑框的语法简介

FINEREPORT用到公式的地方非常多,单元格(以=开头的便被解析为公式),条件显示,数据字典,报表填报属性值定义,图表标题,轴定义,页眉页脚,甚至单元格的其他属性中的鼠标悬浮提示内容都可以写公式,虽然那个编辑框非常不像. 简单的说下自己感觉的公式要注意的几个地方: 1.if语句语法刚接触感觉比较奇怪,if(条件式子,值1,值2),if可以嵌套,if(条件式子1,值1,if(条件式子2,值2,值3)). 2.switch语句也是,具体函数我不说了. 3.对于判断用单等号和双等号都行. 4.对于

web报表工具FineReport的公式编辑框的语法简介

FINEREPORT用到公式的地方非常多,单元格(以=开头的便被解析为公式),条件显示,数据字典,报表填报属性值定义,图表标题,轴定义,页眉页脚,甚至单元格的其他属性中的鼠标悬浮提示内容都可以写公式,虽然那个编辑框非常不像. 简单的说下自己感觉的公式要注意的几个地方: 1.if语句语法刚接触感觉比较奇怪,if(条件式子,值1,值2),if可以嵌套,if(条件式子1,值1,if(条件式子2,值2,值3)). 2.switch语句也是,具体函数我不说了. 3.对于判断用单等号和双等号都行. 4.对于

读取UEditor编辑框内容到数据库和上传图片的配置

主要内容: 如何从数据库读取之前编辑器文本框内容为纯文本 Ueditor上传图片的配置 1. 如何从数据库读取之前编辑器文本框内容为纯文本. 在写下标题问题解决方案之前,我先阐述一个前台显示中遇到的一个问题: 当显示视频时,从数据库读取出来的是html代码,经过razor解析返回到前台页面就是成这样了: <p><span style="font-family: 隶书, SimLi; font-size: 24px; color: rgb(255, 0, 0);">

锐动IOS带UI视频编辑SDK

1 编写目的 预期读者: 有视频编辑开发经验或者无经验的,打算或者正在使用"锐动iOS带UI视频编辑SDK"的相关工程师. iOS软件工程师. 产品经理. QA 2 名词解释 分辨率:用于计算机视频处理的图像,以水平和垂直方向上所能显示的像素数来表示分辨率.常见视频分辨率的有1080P即1920x1080,720P即1080x720,640x480等. 宽高比:视频分辨率的宽高比,常见的有16:9,4:3,1:1.锐动视频编辑SDK对各宽高比的视频都支持编辑,导出的默认分辨率是1280

Android 编辑框(EditText)属性学习

EditText的属性很多,这里介绍几个:android:hint="请输入数字!"//设置显示在空间上的提示信息android:numeric="integer"//设置只能输入整数,如果是小数则是:decimalandroid:singleLine="true"//设置单行输入,一旦设置为true,则文字不会自动换行.android:password="true"//设置只能输入密码android:textColor =

积累的VC编程小技巧之编辑框

1.如何让对话框中的编辑框接收对话框的消息 ////////////////////////////////////////////////// 如何让对话框中的CEdit控件类接收对话框的消息////////////////////////////////////////////////1.在对话框中增加一个ID 为IDC_EDIT1的CEdit1控件2.通过ClassWizard 生成一个基于CEdit的新类CMyEdit,CMyEdit  m_wndEdit;3.在对话框OnInitDia