Sample usage of notifications: detect control width change #96
Replies: 1 comment 3 replies
-
Basically what you want is to shift what you've just implemented, in your |
Beta Was this translation helpful? Give feedback.
-
Basically what you want is to shift what you've just implemented, in your |
Beta Was this translation helpful? Give feedback.
-
I have a dialog containing the control just like in the provided samples.
What I want is that when the size of the control changes (due to font change, capacity change or group view change) the parent dialog automatically resizes to the correct width so the control fits horizontally.
Because all those internal changes are notified using WM_NOTIFY and we can get the actual width of the control, this resizing can be implemented.
I had to find out how to do that. There seems no implementation for WM_NOTIFY in the sample.
Based on the documentation I had to find out how to handle WM_NOTIFY in MFC myself.
I would like to share my solution here for others. But also have a feature request to make it more elegant:
The 3 messages below are enough to implement the auto resize:
But it might be a good idea to introduce a new notification just to inform the width of the control changed due to any reason.
My resize feature is not interested in why the width changed. I only need a notification that it occurred.
For example:
HEXCTRL_MSG_WIDTHCHANGED
Ok, and now my solution. I used the Sample Dialog project provided with HexCtrl.
First you have to add the 3 messages ON_NOTIFY to the message map of the parent dialog.
In SampleDialogDlg.cpp:
BEGIN_MESSAGE_MAP(CSampleDialogDlg, CDialogEx) ON_WM_CLOSE() ON_BN_CLICKED(IDC_SETDATARND, &CSampleDialogDlg::OnBnSetRndData) ON_BN_CLICKED(IDC_CLEARDATA, &CSampleDialogDlg::OnBnClearData) ON_BN_CLICKED(IDC_FILEOPEN, &CSampleDialogDlg::OnBnFileOpen) ON_BN_CLICKED(IDC_HEXPOPUP, &CSampleDialogDlg::OnBnPopup) ON_BN_CLICKED(IDC_CHK_RW, &CSampleDialogDlg::OnChkRW) ON_WM_DROPFILES() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_SIZE() ON_NOTIFY(HEXCTRL_MSG_SETFONT, IDC_MY_HEX, &CSampleDialogDlg::OnWidthChanged) ON_NOTIFY(HEXCTRL_MSG_SETGROUPSIZE, IDC_MY_HEX, &CSampleDialogDlg::OnWidthChanged) ON_NOTIFY(HEXCTRL_MSG_SETCAPACITY, IDC_MY_HEX, &CSampleDialogDlg::OnWidthChanged) END_MESSAGE_MAP()
Then add the declaration of the OnWidthChanged method:
In SampleDialogDlg.h, class CSampleDialogDlg :
And lastly implement the action resizing code.
In SampleDialogDlg.cpp,
Now when using the scroll wheel on the control which changes the font size, the dialog automatically resizes to the width of the control so no horizontal scrollbar is shown. Any change to the width will trigger width resize.
When the dialog is created it automatically resizes the width to fit the control already as the font is set on the control which triggers the resize.
When this seems to be a nice addition to the sample maybe add this feature to the project.
Beta Was this translation helpful? Give feedback.
All reactions