WPF window does not exceed the screen

Asked

Viewed 422 times

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 mouse position, but this window can exceed the limits of the monitor, how can I make it not to exceed the limits?

To illustrate better follows a picture:

inserir a descrição da imagem aqui

When opening the window exceeds the edge, screen limit (monitor), preventing the user to see the full window, forcing to drag.

1 answer

3


One hypothesis is, in this case, the mouse coordinates do not set the upper 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, the lower right corner shall be considered.

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 window dimensions are less than half the screen dimensions and the origin of the coordinates is in the upper left corner of the screen.

  • I work, at least, because there are times that open, and sometimes the window is behind, the main one, all white...

  • This is caused by something else. This code only defines the coordinates of the upper left corner of the window.

  • 1

    All right, I figured out the mistake instead of using JanelaAbrir.Left, was using this.Left, corrected and gave no more error. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.