C++ Builder 2009 - How to Determine if Control'

2019-09-08 15:48发布

I have a TWinControl and am trying to determine if the parent window is visible.

I see TWinControl has a property of ParentWindow. The return type of ParentWindow is void *. So I'm curious if I must cast to a particular type, which would then give me access to check if the window is visible or not.

Does anyone know the type I need to cast to, or another way to accomplish this?


Additional Troubleshooting Notes, Part 1:

I tried to get the ParentWindows class by:

String parentWindowClassName = ((TObject *)(Control->ParentWindow))->ClassName();

But this gave an access violation. I also tried casting to TForm, which also gave an access violation, which makes me believe the parent window may be controlled by windows. If so, does anyone know of any trick to check if it is visible? E.g. Any COM tricks or anything?

Additional Troubleshooting Notes, Part 2:

The answer to this question may help solve my other question: C++ Builder 2009 - Cannot focus a disabled or invisible window

However the other question may be solved without this approach, which is why I posted a different question.

Additional Troubleshooting Notes, Part 3:

Thanks for the extra info Ken. I got my info off code assist: enter image description here

However I see your HWND return type from: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Controls_TWinControl_ParentWindow.html

That might be the extra info I need... will post a solution if I get it working. Thx.

1条回答
Deceive 欺骗
2楼-- · 2019-09-08 16:36

@KenWhite, your suggestions gave me what I needed, thanks!

The following is the code that solved my problem:

#include "winuser.h"

...

void SafeSetFocus(TWinControl *Control)
{
    HWND hWnd = Control->ParentWindow;
    bool parentIsVisible = IsWindowVisible(hWnd);

    if(Control->Enabled && Control->Visible && parentIsVisible)
    {
        Control->SetFocus();
    }
}
查看更多
登录 后发表回答