Skip to content

Instantly share code, notes, and snippets.

@yanlusu
Last active October 26, 2020 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanlusu/d8560991f13436cb6567efd698acfc28 to your computer and use it in GitHub Desktop.
Save yanlusu/d8560991f13436cb6567efd698acfc28 to your computer and use it in GitHub Desktop.

Adding combo boxes to a list view

teps to achieve this:

  • Create a class derived from CListCtrl or CListView.

  • Subclass your view or control window using new class.

  • Create combo box with desired style (not visible - hence, do not use WS_VISIBLE).

  • Get list view control font and assign it to the created combo.

  • Handle NM_CLICK notification.

  • Use GetItemIndexRect to get cell’s rectangle using item and subitem retrieved from NMITEMACTIVATE (cast NMHDR pointer).

  • Set combo position over a cell and make combo visible.

    • Handle CBN_SELCHANGE notification from a combo box mapping WM_COMMAND using ON_CBN_SELCHANGE macro. You can also use OnCmdMsg or OnCommand virtual overrides.

    • In CBN_SELCHANGE notification handler, retrieve selected item’s string.

    • Get combo’s window rectangle and convert to client coordinates. Get center point from rectangle and call Hittest to determine cell a combo is over.

    • Using item and subitem returned from a Hittest set string and/or item data for the appropriate item of the list view control.

    • Hide combo box.

You can change a font size to fit combo box exactly into a cell height, or adjust a cells height to achieve the same.

You can populate combo each time with data appropriate for the cell combo will cover.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment