2016-07-25 2 views
0

테두리가없는 최대화 된 창으로 시작하는 Windows 응용 프로그램 (C#으로 작성)이 있습니다.SetWindowPos는 매번 다른 위치로 창을 이동합니다.

사용자가 응용 프로그램에서 버튼을 클릭하면 창을 복원 (즉, 최대화 된 상태 제거)하고 크기를 특정 크기로 조정 한 다음 화면의 오른쪽 하단으로 이동하려고합니다.

제 문제는 SetWindowPos()을 호출 할 때 창 크기를 올바르게 조정해도 항상 화면의 오른쪽 하단에 배치되지 않는다는 것입니다. 가끔은 그렇지만, 다른 시간에 화면이 화면의 다른 곳에 배치됩니다 (거의 무시하는 이유 때문에 마치 "뛰어 다니는 것처럼").

내가 뭘 잘못하고 있니?

여기 내 코드입니다. 내 윈도우가 모든 다른 윈도우의 위에 있어야하기 때문에 -1을 두 번째 매개 변수로 SetWindowPos에 전달합니다.

NativePoint point = new NativePoint { x = x, y = y }; 

ClientToScreen(handler, ref point); 

를하고 전화를 변경 :

public void MoveAppWindowToBottomRight() 
{ 
    Process process = Process.GetCurrentProcess(); 

    IntPtr handler = process.MainWindowHandle; 

    ShowWindow(handler, 9); // SW_RESTORE = 9 
    int x = (int)(System.Windows.SystemParameters.PrimaryScreenWidth - 380); 
    int y = (int)(System.Windows.SystemParameters.PrimaryScreenHeight - 250); 

    NativePoint point = new NativePoint { x = x, y = y }; 

    ClientToScreen(handler, ref point); 
    SetWindowPos(handler, -1, point.x, point.y, 380, 250, 0);    
} 

[StructLayout(LayoutKind.Sequential)] 
public struct NativePoint 
{ 
    public int x; 
    public int y; 
} 
+0

[SystemParameters.PrimaryScreenWidth] (https://msdn.microsoft.com : 당신이 계정에 작업 표시 줄의 크기를 가지고 싶다면


또한, 대신 SystemParameters.PrimaryScreen***Screen.WorkingArea property을 활용한다 /en-us/library/system.windows.systemparameters.primaryscreenwidth.aspx) 및 [SystemParameters.PrimaryScreenHeight] (https://msdn.microsoft.com/en-us/library/system.windows.systemparameters.primaryscreenheight.aspx) 작업 표시 줄이 차지하는 공간을 고려하지 마십시오. 이것은이 질문의 문제와 관련이 없습니다. 당신이 해결해야 할 또 다른 버그입니다. – IInspectable

답변

6

이러한 라인을 제거해야 호출 ClientToScreen()

SetWindowPos(handler, -1, x, y, 380, 250, 0); 

당신이 이미 화면 좌표가 좌표로 이해되지 않는다.

ClientToScreen()을 호출하면 창 위치의 현재 위치가 인 이라는 새 좌표가 만들어 지므로 매번 위치가 달라집니다. 즉, 함수가 호출 될 때마다 좌표가 달라집니다.

int x = (int)(Screen.PrimaryScreen.WorkingArea.Width - 380); 
int y = (int)(Screen.PrimaryScreen.WorkingArea.Height - 250); 
+0

작업 표시 줄에 대해 지적하는 것에 대해 IInspectable에게 제공됩니다. –

+0

@ visual-vincent에 감사드립니다. SetWindowPos가 상대 위치를 기반으로 새 좌표를 생성하는지 몰랐습니다. 필요한 호출이 'MoveWindow'인 것을 알았습니다.이 윈도우는 최상위 창에 대해 화면의 왼쪽 위 모서리를 기준으로 새 좌표를 계산합니다. –

+0

@AlbertoVenturini : Nonononono, 현재 위치와 관련된 새로운 좌표를 생성하는'ClientToScreen()'입니다. 내가 말했듯이, 그것은 당신이 제거해야 할 전화입니다, 불분명하게해서 유감스럽게 생각합니다. 여전히'SetWindowPos()'를 사용할 수 있습니다. –