3

I am using C++ MFC, and have created a simple dialog with CButtons, each of them mapped with its .bitmap files and resource ids (ID_BUTTON*) in a .rc script file.

Similar lines are present in my .rc file, in DIALOG description:

CONTROL         "TEST|Button7",ID_BUTTON2,"Button",BS_OWNERDRAW | WS_TABSTOP,234,29,30,71

In my project I am trying to get the resource id of each CButton object. I did it with this:

int getID = this->GetDlgCtrlID();

But how can I use my resource ID further to get the CButton control text value? Meaning this:

"TEST|Button7"

If not, is there a specific way to get it?

genpfault
  • 51,148
  • 11
  • 85
  • 139
neaAlex
  • 206
  • 3
  • 15
  • Your question is confusing because you refer to "resource name" which would surely be `ID_BUTTON2`. Perhaps you should re-word it because the answer you accepted shows you how to get the text value associated with the control which is not the same as the resource name. – Andrew Truckle Sep 24 '19 at 15:37
  • 1
    Control name instead then. Sorry about that ! Still new to MFC and I am prone to typo errors. – neaAlex Sep 24 '19 at 15:49

1 Answers1

5

It's actually very simple. Where you use int getID = this->GetDlgCtrlID(); to get the resource ID, you can use this code to get the control's name:

CString buttonName;
this->GetWindowText(buttonName);

PS: Assuming the calls are made inside a class member function, then you don't actually need the this-> pointer; just call the GetWindowText() or GetDlgCtrlID() functions. (But using this-> does no harm, and can make code a bit clearer to read.)

If you want to get the text for a button from outside the button's own class functions - say, from the parent dialog box handler, you can use this:

CString buttonName;
GetDlgItem(idValue)->GetWindowText(buttonName);

Where idValue is the resource ID of the button (or any other control) concerned.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83