WPF window does not exceed screen

3

I have the following code:


    System.Windows.Point ponto = PointToScreen(Mouse.GetPosition(this));

    JanelaAbrir.Left = ponto.X;
    JanelaAbrir.Top = ponto.Y;

    JanelaAbrir.ShowDialog();

It opens the window in the mouse position, but this window can go beyond the limits of the monitor, how can I make it not to exceed the limits?

To illustrate, an image follows:

When opening the window it goes beyond the border, limit the screen (monitor), preventing the user to see the full window, forcing it to drag.

    
asked by anonymous 01.12.2014 / 13:35

1 answer

3

One hypothesis is, in this case, the coordinates of the mouse do not define the top left corner of the window but the upper right corner. The same can be done if the window does not fit vertically, instead of setting the upper left corner define the lower left corner. If both situations occur, it will be considered the lower right corner.

int x = coordenada x do rato;
int y = coordenada y do rato;
int maxX = largura da tela;
int maxY = altura da tela;
int largura = largura da janela;
int altura = altura da janela;

if(x + largura > maxX) JanelaAbrir.Left = x - Largura;
else JanelaAbrir.Left = x;
if(y + altura > maxY) JanelaAbrir.Top = y - altura;
else JanelaAbrir.Top = y;

This is valid if the dimensions of the window are less than half the dimensions of the screen and the origin of the coordinates is in the upper left corner of the screen.

    
01.12.2014 / 14:41