Click here to Skip to main content
15,031,495 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an mfc dialog based application that creates static controls dynamically whenever a particular button is clicked. Now I need to add an event handler OnClick to those controls. Please help me to solve out this problem.

I am using Visual Studio 2008

What I have tried:

I tried using ON_CONTROL_RANGE, but nothing happens on clicking those controls
Updated 27-Aug-18 23:13pm
Richard MacCutchan 28-Aug-18 4:37am
You need to provide more details. It is anyone's guess what happens in your code.
Member 13964032 28-Aug-18 5:22am
In the header file I have included the following

afx_msg void OnClickControl(UINT nID);

In the message map I included


1 solution

You have to use the SS_NOTIFY style when creating the static controls to enable notifications. Then the parent window will get a STN\_CLICKED notification code | Microsoft Docs[^].

In the message map you can use STN_CLICKED or BN_CLICKED because they have the same value.
Member 13964032 28-Aug-18 5:23am
I tried that too
Jochen Arndt 28-Aug-18 5:37am
Then you made something wrong. But without seeing the relevant code (including the creation of the control) it is impossible to help.

The code of the CP article Using the CStatic control shows how it can be done with dialog template defined controls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900