TileItem Click Event无法正常工作?

时间:2015-04-21 21:55:06

标签: c# winforms event-handling devexpress mouseevent

我在tilecontrol(DevExpress)中有一个TileItem。点击此方法时会触发

private void tileItem1_ItemClick_1(object sender, DevExpress.XtraEditors.TileItemEventArgs e)
        {
            if (GetForegroundWindow() == Process.GetCurrentProcess().MainWindowHandle)
            {
                MessageBox.Show("It is focused!");
                //do some magic here
            }
            else
            {
                MessageBox.Show("It is not focused!");
                this.Activate();
            }
        }

我的问题是,即使我的表单没有集中注意力,它也会认为它是专注的,并且总是将if语句的第一个条件变为三元组。 如果窗口没有聚焦,我想在点击tileitem时激活窗口。这不是正确的方法吗? 我的想法是,在释放鼠标后触发该方法,因此窗口已经在那个时候处于焦点。如果这是问题那么哪个解决方案可以实现我想做的事情呢?

谢谢你的到来。

1 个答案:

答案 0 :(得分:0)

试试这个:

public class Form5 : Form {

    protected override void OnMouseDown(MouseEventArgs e) {
        base.OnMouseDown(e);
        if (wasMouseActivated) {
            wasMouseActivated = false;
            // window was activated by a mouse click
        }
        else {
            // window already had focus
        }
    }

    private const int WM_MOUSEACTIVATE = 0x21;
    private bool wasMouseActivated = false;
    protected override void WndProc(ref Message m) {
        if (m.Msg == WM_MOUSEACTIVATE) {
            wasMouseActivated = true;
        }

        base.WndProc(ref m);
    }
}