Question

I have an mode that uses TComboBox.SelStart to indicate progress along the edit text string. In this mode I would like to make some kind of change to the edit caret, for example to widen it to 2 pixels or 'bold' it in some way to indicate this mode and to have it grab more attention. Is this possible?

Was it helpful?

Solution

Yes, as Alex mentioned in his comment, this can be done using API calls. Example:

procedure SetComboCaretWidth(ComboBox: TComboBox; Multiplier: Integer);
var
  EditWnd: HWND;
  EditRect: TRect;
begin
  ComboBox.SetFocus;
  ComboBox.SelStart := -1;
  Assert(ComboBox.Style = csDropDown);

  EditWnd := GetWindow(ComboBox.Handle, GW_CHILD);
  SendMessage(EditWnd, EM_GETRECT, 0, LPARAM(@EditRect));
  CreateCaret(EditWnd, 0,
              GetSystemMetrics(SM_CXBORDER) * Multiplier, EditRect.Height);
  ShowCaret(EditWnd);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetComboCaretWidth(ComboBox1, 4); // bold caret
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetComboCaretWidth(ComboBox1, 1); // default caret
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top