Creating a ComboBox with one or more separator elements?

I use Delphi7 and I would like to have a ComboBox with separator elements (as in popup menus).

I saw this perfectly implemented in Mozilla Sunbird (I know this is not Delphi ...) as follows:

  • The separator element is the simple gray line in the center of the element.

  • If you hover over the separator, selections do not appear

  • If the user presses the delimiter, he is not selected. And the combobox does not close.

No. 1 can be implemented using DrawItem. I could live without No. 2 because I have no idea about it.

For number 3, I ask you for help. I realized that immediately after closing the message CBN_CLOSEUP is sent to the combo box.

I was thinking about connecting the proc window, and if CBN_CLOSEUP is sent to a specific combobox, then against it. But I'm not sure if this is the best solution, or maybe there are other, more elegant ways?

Whatever the solution, I would like to have a standard ComboBox that correctly supports WinXP / Vista / 7.

Thank!


Edit: For a working component, see this thread:

Can you help translate this very small C ++ component into Delphi?

+3
source share
3 answers

( ) . , , :

  • , .
  • Enter , .
  • Escape , ( ) .
  • , , , .
  • , - , , .
  • , F4 , .

, - ( , , , ?), .

, , , :

  • .
  • 3 , .
  • , .

++ Builder ; Delphi .

void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control,
    int Index, TRect &Rect, TOwnerDrawState State)
{
  bool draw_separator = NeedsSeparator(Index) && 
      !State.Contains(odComboBoxEdit);

  TCanvas *canvas = dynamic_cast<TCustomCombo*>(Control)->Canvas;
  canvas->FillRect(Rect);

  TRect text_rect = Rect;
  // Add space for separator if needed.
  if (draw_separator) {
    text_rect.Top += 3;
  }

  canvas->TextOut(text_rect.Left + 3,
      (text_rect.Top + text_rect.Bottom) / 2 - 
        canvas->TextHeight(ComboBox1->Items->Strings[Index]) / 2), 
      ComboBox1->Items->Strings[Index]);

  // Draw a separator line above the item if needed.
  if (draw_separator) {
    canvas->Pen->Color = canvas->Font->Color;
    canvas->MoveTo(Rect.Left, Rect.Top + 1);
    canvas->LineTo(Rect.Right, Rect.Top + 1);
  }
}

void __fastcall TForm1::ComboBox1MeasureItem(
    TWinControl * /* Control */, int Index, int &Height)
{
  Height = ComboBox1->ItemHeight;

  // Add space for the separator if needed.
  if (Index != -1 && NeedsSeparator(Index)) {
    Height += 3;
  }
}
+1

, , SpTBXLib. -, .

0
source

Source: https://habr.com/ru/post/1778505/


All Articles