0

Is there a way I can get a HWND by it's property "name"? I know that every IDE has its own properties for HWND elements but those properties are applied to the HWND.

I'm not working in Visual Studio, this is just a case. I want to get HWNDs by Name in C++ without VS Libraries.

For example:

HWND button = GetHwndByName("button1"); //Example

Property "name" is button1 enter image description here

Property "name" is: button1

ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • Have you tried [`FindWindow`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx)? – Michael Jul 22 '15 at 08:38
  • 5
    At the Win32 API level, there is no "name" associated with an HWND. The function suggested by @Michael gets an HWND based on window class and window text (such as title or button text). – user1610015 Jul 22 '15 at 08:44
  • That is true. `FindWindow` searchs by "Text" in my case "my button". But if there isn't "name" property associated with an HWND how can Visual Basic get elements by name? I mean, it must be something associated with that HWND to its properties. – ProtectedVoid Jul 22 '15 at 08:45
  • IIRC, `(name)` is not a property, it's really the name of the variable. Thus to get its HWND you just have to do `HWND button = button1->Handle;`. – ElderBug Jul 22 '15 at 08:52
  • In the case of WPF, the name is stored in the System.Windows.FrameworkElement class. It's the same thing you could achieve if you made a UI library in C++ (like MFC) and made a class that has a property that identifies the control by name. – user1610015 Jul 22 '15 at 08:54
  • In Win32 dialogs controls are identified by a numerical ID, not a name. – Jonathan Potter Jul 22 '15 at 09:15
  • Why do you need to find a window by name? usually you go by numerical ID. – Daniel Jul 22 '15 at 09:44
  • I need it because I need to assign events to elements which share a type, for example: All buttons with name "func-" are associated to a function, all of them will share an event. I can't make a difference between HWNDs using other property, suggestions? Thanks. – ProtectedVoid Jul 22 '15 at 09:49
  • `GetProp/SetProp` is the mechanic to bind arbitrary information to a HWND - I think if you more fully explain exactly what.why you need a better alternative will exists. – Alex K. Jul 22 '15 at 10:35
  • Wait, is this a raw Windows API or a WinForms/WPF program? How are you designing the GUI exactly? Implementing the code? – andlabs Jul 22 '15 at 13:06
  • I'm using CA Plex. It's a multi-platform IDE, but in my case I'm designing a Windows Form. GUI Designing is similar to Visual Studio but waaaay older than Visual Studio. @AlexK. Can explain me what you mean? Thanks. – ProtectedVoid Jul 22 '15 at 14:25
  • This could be the [UIA_NamePropertyId](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684017.aspx#UIA_NamePropertyId) property used for [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx). – IInspectable Jul 22 '15 at 20:49
  • @IInspectable if I'm reading the docs right wouldn't that just be the label to the side of the control? or am I missing something? – andlabs Jul 23 '15 at 04:56
  • @andlabs: A button control (or any control really) doesn't have a label. The *Name* property is an arbitrary, invisible value, that can be queried using UI Automation. You can use the [Inspect](https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521.aspx) tool to see this in action. – IInspectable Jul 23 '15 at 09:32

1 Answers1

0

I'm going to assume you're either trying to access your GUI controls in code or some other program's GUI controls.

As some people have mentioned, the (name) property in the properties editor is just the variable name used for that control. Your screenshot shows Visual Studio editing a .net program. In the case of .net, the (name) field is the name of the class member of the window class that represents the control. So if (name) is button1 then Visual Studio might generate code like

// pseudo-C++/C#-like
class Form1 : public System.Windows.Forms.Form {
private:
    System.Windows.Forms.Button *button1;
...
};

The idea here is that you would have event handlers as part of your Form1 class:

void Form1::onButton1Clicked(void)
{
    this->button1->SetText("You clicked me!");
}

As such, the (name) is not an intrinsic property of the window from Windows's point of view.

I don't know what CA Plex's GUI editor looks like, but I would assume, given you said you were using C++, that it either

a) produces a class like the one I pasted above, in which case you would just use the (name) directly as members, or

b) produces a header file with each of those control names as global HWND variables

Either way, you can just use them directly from within your code. Perhaps have something like

void doToAllButtons(void (*f)(HWND, LPARAM), LPARAM lParam)
{
    (*f)(button1, lParam);
    (*f)(button2, lParam);
    (*f)(button3, lParam);
}

and simply write an appropriate function to call via this one.

If you need to interface with another program and want to use its variable names, then you're out of luck. You'll need to find the windows you want some other way, such as with FindWindow().

andlabs
  • 11,290
  • 1
  • 31
  • 52