C ++ Builder 2009 - 无法聚焦禁用或不可见窗口 [英] C++ Builder 2009 - Cannot focus a disabled or invisible window

查看:369
本文介绍了C ++ Builder 2009 - 无法聚焦禁用或不可见窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经重构一个旧项目。我发现当旧程序员试图使用 SetFocus() TWinControls catch块。因此,吞下异常。



程序的默认行为是在控件启用时设置焦点。为了做到这一点,我创建了一个函数,我可以将 TWinControl 传递给:

  void SafeSetFocus(TWinControl * Control)
{
if(Control-> Enabled&& Control-> Visible)
{
Control-> SetFocus();
}
}

此代码适用于大多数程序,发现在一个区域,我仍然得到一个调试器异常无法聚焦一个禁用或不可见的窗口



认为该问题可能与父级相关,因此我尝试了以下调整:

  void SafeSetFocus(TWinControl * Control)
{
if(Control-> Enabled&& Control-> Visible&&
Control-> Parent-> Enabled&& Control-> Parent - > Visible)
{
Control-> SetFocus();
}
}

此更改未能解决问题。因为这个,我意识到窗口可能不一定是父。所以我的问题归结为:



有没有办法确定 TWinControl 的窗口是什么看看它是否可见?这是假设例外是准确的...否则如果你知道问题是什么,请分享你的知识:)






其他疑难解答备注,第1部分:



我尝试通过以下代码确定ParentWindow的类名:

  String parentWindowClassName =((TObject *)(Control-> ParentWindow)) - > ClassName 
MessageDlg(parentWindowClassName:+ parentWindowClassName,mtInformation,TMsgDlgButtons()<<< mbOK,0);

当我运行它时,第一行代码给出了访问违例...任何不同的想法

p> CanFocus()只是控件不工作。 CanFocus()对于控件和父级无效,请参阅屏幕截图。



解决方案

感谢@KenWhite提出了他的建议( C ++ Builder 2009 - 如何确定控件的窗口是否可见



他在这个问题的建议导致我的答案。下面是其他人可能感兴趣的代码:

  #includewinuser.h

。 ..

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

if(Control-> Enabled&& Control-> Visible&& amp; parentIsVisible)
{
Control-> SetFocus
}
}


I've been refactoring an old project. I found when the old programmers tried to use the SetFocus() of TWinControls they surrounded them in try/catches with empty catch blocks. Thus swallowing the exceptions.

The default behavior of the program is to set the focus if the control is enabled. In order to do that I created a function which I can pass the TWinControl to:

void SafeSetFocus(TWinControl *Control)
{
    if(Control->Enabled && Control->Visible)
    {
        Control->SetFocus();
    }
}

This code works for most of the program, however I found that in one area that I still get a Debugger Exception of Cannot focus a disabled or invisible window.

I thought that the issue might be related to the parent, so I tried the following adjustment:

void SafeSetFocus(TWinControl *Control)
{
    if(Control->Enabled && Control->Visible &&
            Control->Parent->Enabled && Control->Parent->Visible)
    {
        Control->SetFocus();
    }
}

This changed did not solve the issue. Because of this, I realized that the window may not necessarily be the parent. So my question boils down to:

Is there a way to determine what the window of the TWinControl is and check to see if it is visible? This assumes the exception is accurate... otherwise if you know what the issue is, please share your knowledge :)


Additional Troubleshooting Notes, Part 1:

I've tried to determine the class name of the ParentWindow by the following code:

String parentWindowClassName = ((TObject *)(Control->ParentWindow))->ClassName();
MessageDlg("parentWindowClassName: " + parentWindowClassName, mtInformation, TMsgDlgButtons() << mbOK, 0);

The first line of code gives an access violation when I run it... any thoughts on a different way to try to determine the info?

Additional Troubleshooting Notes, Part 2:

CanFocus() with just the control doesn't work. CanFocus() for the control and parent doesn't work, see screen shot.

解决方案

Thanks goes out to @KenWhite for his suggestions in question ( C++ Builder 2009 - How to Determine if Control's Window is Visible )

His suggestions in that question lead me to the answer. Below is the code others might be interested in:

#include "winuser.h"

...

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

    if(Control->Enabled && Control->Visible && parentIsVisible)
    {
        Control->SetFocus();
    }
}

这篇关于C ++ Builder 2009 - 无法聚焦禁用或不可见窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆