Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 1.44 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is it possible to determine if the text in a dbEdit is longer than what is visible?
  2. procedure Tgm12edLots.dbeLotNameMouseEnter(Sender: TObject);
  3. begin
  4.   with dbeLotName do begin
  5.     ShowHint := True;
  6.     Hint := Text;
  7.   end;
  8. end;
  9.        
  10. function CanShowAllText(Edit: TDBEdit):Boolean;
  11. var
  12.     TextWidth:Integer;
  13.     VisibleWidth: Integer;
  14.     Bitmap: TBitmap;
  15. const
  16. //This could be worked out but without delphi I can't remember all that goes into it.
  17.     BordersAndMarginsWidthEtc = 4;
  18. begin
  19.     Bitmap := TBitmap.Create;
  20.     try
  21.         Bitmap.Canvas.Font.Assign(Edit.Font);
  22.         TextWidth := Bitmap.Canvas.TextWidth(Edit.Text);
  23.         VisibleWidth := Edit.Width - BordersAndMarginsWidthEtc;
  24.         Result := TextWidth < VisibleWidth;
  25.     finally
  26.         Bitmap.Free;
  27.     end;
  28. end;
  29.        
  30. type
  31.   TCustomEditAccess = class(TCustomEdit);
  32.  
  33. function EditTextWidth(Edit: TCustomEdit): Integer;
  34. var
  35.   DC: HDC;
  36.   Size: TSize;
  37.   SaveFont: HFont;
  38. begin
  39.   DC := GetDC(0);
  40.   SaveFont := SelectObject(DC, TCustomEditAccess(Edit).Font.Handle);
  41.   GetTextExtentPoint32(DC, PChar(Edit.Text), Length(Edit.Text), Size);
  42.   SelectObject(DC, SaveFont);
  43.   ReleaseDC(0, DC);
  44.   Result := Size.cx;
  45. end;
  46.  
  47. function EditVisibleWidth(Edit: TCustomEdit): Integer;
  48. var
  49.   R: TRect;
  50. begin
  51.   SendMessage(Edit.Handle, EM_GETRECT, 0, LPARAM(@R));
  52.   Result := R.Right - R.Left;
  53. end;
  54.  
  55. function IsEditTextOverflow(Edit: TCustomEdit): Boolean;
  56. begin
  57.   Result := EditTextWidth(Edit) > EditVisibleWidth(Edit);
  58. end;