Reputation: 21
I am creating a font dialog box which consists of font selection/styling/sizing
in C++ using the Windows API.
I added text and a combobox in my resource script, but I am confused about how to add items to the combobox and handle its click events. I searched the Internet but am unable to find the answer.
My code is
#include "Resource.h"
#include <windows.h>
#define IDD_DLGFIRST 101
#define IDC_SCROLLBARV 1010
#define COMBOX1 1
#define COMBOX2 2
#define COMBOX3 3
IDD_DLGFIRST DIALOG 200, 200, 300, 200
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Font Color"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Font",10,10,15,80,20
COMBOBOX COMBOX1,10,30,80,100, CBS_SIMPLE | WS_VSCROLL | WS_TABSTOP
LTEXT "Font Style",11,110,15,80,20
COMBOBOX COMBOX2,110,30,80,100, CBS_SIMPLE | WS_VSCROLL | WS_TABSTOP
LTEXT "Font Size",11,210,15,80,20
COMBOBOX COMBOX3,210,30,80,100, CBS_SIMPLE | WS_VSCROLL | WS_TABSTOP
END
Upvotes: 2
Views: 1549
Reputation: 596266
You can't add items to a ComboBox via a resource script. The script can only define the ComboBox itself and its characteristics. You need to write code to handle the ComboBox items at runtime.
For instance, in your dialog's WndProc procedure, handle the WM_INITDIALOG
message to send CB_ADDSTRING
messages to the ComboBox's HWND
, which you can get from the Win32 GetDlgItem()
function.
To handle clicks and other events from the ComboBox, they are sent to your dialog's WndProc in the form of WM_COMMAND
notifications, such as CBN_SELCHANGE
.
And FYI, Windows has its own built-in Font Dialog Box, which you can invoke using the Win32 ChooseFont()
function, or the MFC CFontDialog
class, etc.
Upvotes: 2
Reputation: 15162
The API does not know how to take items from the resource script, you could however do like MFC and use the data element part of the CONTROL resource statement. You would then have to read the saved information during the WM_INITDIALOG handler and add the items to the combo box.
I'm not sure if the resource dialog designer is able to add such items if you aren't dealing with an MFC project.
Upvotes: 1