I need to draw a component that looks like a tcombobox but has all the events of a tedit component.

I need to draw a component that looks like a tcombobox but has all the events of a tedit component. It only needs to look like a tcombobox and does not require the function. I specifically need the mousedown and mousemove events as I need to be able to allow the user to place the component on a form and move it where they would like it placed. I have all the other components working except for the tcombo as it does not have these mouse events. Any help would be greatly appreciated.
shardtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

developmentguruPresidentCommented:
 It depends on what type of combo box you are trying to emulate.  The Windows 2000 version is drawn differently from the windows XP version.  I would pick a style and stick with it unless you want to be maintaining two versions of the paint routine (doable, but do you really need it?).  I would descend from TCustomControl and override the Paint Method to do your drawing.  Once in the Paint method you can use all of the Canvas methods to draw what you want.  A personal favorite of mine for this kind of work is using the Frame3D procedure in the ExtCtrls unit.  You pass it the canvas to draw to, the rectangle, TopColor, BottomColor, and width.  It adjusts the rect for you so you can use it to draw a 3D looking border, then fill in the inside of it.  I don't have time to do a full version at the moment, if you decide on a style I may have more time later.
0
shardtAuthor Commented:
Thanks for the info. unfortunately I do not have allot of experience in using the draw routines. If you could when you have time send me a quick example and I should be able to figure it out then.
0
developmentguruPresidentCommented:
You can tell a combo box to paint to any other controls canvas handle using the following line.

ComboBox1.PaintTo(PaintBox1.Canvas.Handle, 0, 0);

I tested that with a PaintBox and canvas on the form.  That line was muy one line in the OnPaintEvent of the paint box.

  Attached I have some code that paints to a paint box's canvas to emulate an empty combo box.  You would need to add code to show text, react to button preses, etc to make it a functional combo box.  Mu form I used was Form2, change as needed.  The only reason you need the DFM is because I included an image of the button.  If you would like to provide your own, adjust the code as needed and you can simply place a PaintBox, a combo box, and an image list on the form.  I set the Paint Box to the same size as the combo box.

--DFM--
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 286
  ClientWidth = 426
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 8
    Top = 9
    Width = 145
    Height = 21
    OnPaint = PaintBox1Paint
  end
  object ComboBox1: TComboBox
    Left = 8
    Top = 36
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 0
    Text = 'ComboBox1'
  end
  object ImageList1: TImageList
    Height = 17
    Width = 15
    Left = 184
    Top = 88
    Bitmap = {
      494C01010100040004000F001100FFFFFFFFFF00FFFFFFFFFFFFFFFF424D3600
      00000000000036000000280000003C000000110000000100200000000000F00F
      000000000000000000000000000000000000F8E3DBFFF1C6B7FFF2C9B8FFF3C9
      B9FFF3C9B9FFF3C9B9FFF3C9B9FFF2C9B8FFF5C8B3FFF5C8B3FFF5C8B3FFF4C8
      B2FFF3C6B1FFF2C5B0FFF6E3D9FF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F4C1ABFFF9CBB7FFFBCEB9FFFACEBCFFFACEBCFFFACEBCFFFACEBCFFFBCF
      B9FFFBCDB6FFFBCDB2FFFBCDB2FFFACBB1FFF9C9ADFFF7C8AEFFF2C5B0FF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F6C3ADFFFCCDBAFFFCD0BEFFFBD2
      C1FFFCD0BEFFFCD0BEFFFCD0BEFFFBD3BDFFFCD3B7FFFBD1B6FFFBD1B6FFFBCF
      B2FFFBCDAFFFF9C9ADFFF3C6B1FF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F6C3ADFFFCD0BEFFFCD3C2FFFCD3C2FFFCD5C2FFFCD5C2FFFCD5C2FFFCD5
      C2FFFCD4BAFFFCD3B7FFFCD3B7FFFCD1B3FFFBCDAFFFFACBAFFFF4C8B2FF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F6C3ADFFFCD0BEFFFCD3C2FFFCD3
      C2FFFCD5C2FFFCD5C2FFFCD5C2FFFCD5C2FFFCD4BAFFFCD3B7FFFCD3B7FFFCD1
      B3FFFBCDAFFFFACBAFFFF4C8B2FF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F6C7B1FFFCD3C2FFFCD6C5FFFCD6C5FFFCD6C5FFFCD6C5FFFDD5C3FF8561
      4DFFFCD4BAFFFBCFB2FFFBCFB2FFFBCFB2FFFBCFB2FFFACBB1FFF5C8B3FF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F6C8B4FFFCD5C5FFFCD5C5FFFCD5
      C5FFFCD5C5FFFCD5C5FF85614DFF85614DFF85614DFFFBD3BDFFFBD3BDFFFBCE
      B6FFFBCEB6FFFACDB5FFF3C9B5FF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F5CAB7FFFBD6C8FFFBD6C8FFFBD6C8FFFBD3C1FF85614DFF85614DFF8561
      4DFF85614DFF85614DFFFBCEB6FFFBCEB6FFFBCEB6FFFBCDB6FFF3CBB9FF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F6CBB8FFFCD8C9FFFDD8CAFFFDD8
      CAFF85614DFF85614DFF85614DFFFBD3C1FF85614DFF85614DFF85614DFFFBCE
      B6FFFCCDB7FFFBCDB9FFF4CCBAFF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F6CBB8FFFCD8C9FFFDD8CAFF85614DFF85614DFF85614DFFFBD3C1FFFBD3
      C1FFFBCEB6FF85614DFF85614DFF85614DFFFCCDB7FFFBCDB9FFF4CCBAFF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F7CEBDFFFCDBCDFFFCDBCDFFFCD8
      C9FF85614DFFFCD8C9FFFCD8C9FFFDCEBBFFFBCDB9FFFBCDB9FF85614DFFFCCD
      B7FFFBCDB9FFFBCDB9FFF3CCBCFF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F8D0BFFFFCDDD0FFFDDDCEFFFDD9CAFFFBD6C8FFFBD6C8FFFBD6C8FFFBD6
      C8FFFDCEBBFFFCCDBAFFFCCDBAFFFBCDB9FFFCCDBAFFFCCDBAFFF3CCBCFF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F7D3C4FFFCE1D4FFFDE0D1FFFCDB
      CDFFFCD8C9FFFCD8C9FFFCD8C9FFFBD5C8FFFCD0BEFFFDCEBBFFFDCEBBFFFDCE
      BBFFFDCEBBFFFACEBCFFF3CCBCFF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000F7D3C4FFFCE1D4FFFDE0D1FFFCDBCDFFFCD8C9FFFCD8C9FFFCD8C9FFFBD5
      C8FFFCD0BEFFFDCEBBFFFDCEBBFFFDCEBBFFFDCEBBFFFACEBCFFF3CCBCFF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000F9D8CAFFFEE6DAFFFCE3D8FFFDDE
      D1FFFDDBCEFFFDDBCEFFFDD9CAFFFCD8C9FFFCD5C5FFFCD3C5FFFCD3C5FFFCD3
      C2FFFCD0BEFFFACEBCFFF4CCBAFF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000FDE0D1FFFEEAE1FFFEE6DAFFFCE1D4FFFDE0D1FFFDE0D1FFFCDDD0FFFDDB
      CEFFFDD9CAFFFBD8C8FFFBD8C8FFFCD6C5FFFCD3C2FFFACEBCFFF3C9B9FF0000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000FCEEE6FFFDE0D1FFF9D8CAFFF7D3
      C4FFF8D0BFFFF8D0BFFFF7CEBDFFF4CCBAFFF6CBB8FFF5CAB7FFF5CAB7FFF7C8
      B5FFF5C7B3FFF4C5AFFFF9E6DCFF000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000424D3E000000000000003E000000280000003C0000001100000001000100
      00000000880000000000000000000000000000000000000000000000FFFFFF00
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000}
  end
end
 
--PAS--
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ImgList;
 
type
  TNewCombo = class(TWinControl)
  private
  protected
  public
    constructor Create(Owner : TComponent); override;
  published
  end;
 
  TForm2 = class(TForm)
    PaintBox1: TPaintBox;
    ComboBox1: TComboBox;
    ImageList1: TImageList;
    procedure PaintBox1Paint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
 
{ TNewCombo }
 
 
{ TNewCombo }
 
constructor TNewCombo.Create(Owner: TComponent);
begin
  inherited;
end;
 
procedure TForm2.PaintBox1Paint(Sender: TObject);
const
  ButtonWidth = 15;
 
var
  R, B : TRect;
  BorderColor : TColor;
  Button : TBitmap;
 
begin
  with PaintBox1, Canvas do
    begin
      //setup initial values
      R := Rect(0, 0, Width, Height);
      BorderColor := RGB(127, 157, 185);
 
      //outside frame
      Frame3D(Canvas, R, clNavy, clNavy, 1);
      Frame3D(Canvas, R, clWhite, clWhite, 1);
 
      B := R;
 
      //Paint the inside of the text area
      R.Right := R.Right - ButtonWidth;
      Brush.Color := clWhite;
      FillRect(R);
 
      B.Left := Width - ButtonWidth - 2;
      Button := TBitmap.Create;
      try
        ImageList1.GetBitmap(0, Button);
        CopyRect(B, button.Canvas, Rect(0, 0, Button.Width, Button.Height));
      finally
        Button.Free;
      end;
    end;
end;
 
end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shardtAuthor Commented:
Thanks this got me going in the right direction. I ened up having to use the TCustomControl as I needed a TWinControl object.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.