警报/警告窗口不会弹出控制位置

时间:2015-08-11 23:37:48

标签: c# winforms devexpress

我正在尝试获取一个自动关闭的警报窗口,以便在用户使用错误的特定控件下弹出。

我目前正在使用DevExpress AlertControl机制。需要使用BeforeFormShow事件设置其位置,因此我使用以下管理器类来设置其位置并使用特定消息显示警报:

class AlertPopper
{
    private AlertControl _alert;
    private Form _form;
    private int _x, _y;

    /// Pass in an AlertControl to be managed by the AlertPopper.
    public AlertPopper(Form form, AlertControl alert)
    {
        _form = form;
        _alert = alert;
        _x = _y = 0;

        _alert.BeforeFormShow += SetAlertLocation;
    }

    private void SetAlertLocation(object sender, AlertFormEventArgs e)
    {
        e.Location = new System.Drawing.Point(_x, _y);
    }

    public void DisplayAlert(int x, int y, string message)
    {
        _x = x;
        _y = y;

        var alertInfo = new AlertInfo("Warning", message);
        _alert.Show(_form, alertInfo);
    }

    public void DisplayAlert(Control control, string message)
    {
        DisplayAlert(control.Location.X, control.Location.Y, message);
    }
}

目的是捕获异常并在相关控件下显示警告,如下所示:

// In form constructor, start the alert manager:
public MyForm()
{
    _alertPopper = new AlertPopper(this, this.AlertWarning);
}


// ... then in some event handler method, display the alert:
private void SomeControl_Click(Control sender, EventArgs e)
{
    // ... in some catch block for a recoverable exception
    _alertPopper.DisplayAlert(sender, "Bad thing happened.");
}

但是,而不是显示在控件下方的警报窗口,它显示得很远(通常在我的两个显示器设置的错误显示屏上,但不在此处):

Alert shows up relative to screen corner, not window corner

看来坐标正在相对于我的屏幕角处理,而不是我的窗角。如何才能获得相对于后者显示的警报?

1 个答案:

答案 0 :(得分:2)

您需要使用Control.PointToScreen方法 以下是DisplayAlert方法的示例:

public void DisplayAlert(Control control, string message)
{
    var point = new Point(control.Width / 2, control.Height);
    var screenPoint = control.PointToScreen(point);

    DisplayAlert(screenPoint.X, screenPoint.Y, message);
}

这是您的事件处理程序方法:

private void SomeControl_Click(Control sender, EventArgs e)
{    
    _alertPopper.DisplayAlert(sender, "Bad thing happened.");
}