2013-06-28 2 views
1

내가 현재이 코드를 사용하여 크기를 조정 화면 비율로 크기 조정에서 양식을 고정하고 그것을 완벽하게어떻게 더

내 질문은 내가 너비와 높이의 나의 선호하는 한계에 중지 WndProc를 수정합니까 어떻게 작동합니다. 그래, 난 MinimumSize 설정하여 해결했지만 양식의 종횡비가 Windows 바탕 화면의 한계에 도달하면 새로운 문제가 발생합니다. maximum right 크기가 엉망이되기 시작합니다. 종횡비가 잠금 대신 스트레칭을 시작합니다.

WndProcSystemInformation.VirtualScreen.Width으로 수정하여 한계 너비에 도달하면 두 가지 크기를 모두 늘리지 않아야합니다.

이 기능은 추가되었지만 여전히 내 해상도에만 해당됩니다. 어떻게 모든 해상도를 지원하는지 보편적으로 만들 수 있습니까? 코드의

 If r.right - r.left > SystemInformation.VirtualScreen.Width Then 
      r.bottom = 900 'quick fix (not good) how to calculate this value? 
     End If 

소스 : http://www.vb-helper.com/howto_net_form_fixed_aspect.html

Imports System.Runtime.InteropServices 
... 
Public Structure Rect 
    Public left As Integer 
    Public top As Integer 
    Public right As Integer 
    Public bottom As Integer 
End Structure 

Protected Overrides Sub WndProc(ByRef m As _ 
    System.Windows.Forms.Message) 
    Static first_time As Boolean = True 
    Static aspect_ratio As Double 
    Const WM_SIZING As Long = &H214 
    Const WMSZ_LEFT As Integer = 1 
    Const WMSZ_RIGHT As Integer = 2 
    Const WMSZ_TOP As Integer = 3 
    Const WMSZ_TOPLEFT As Integer = 4 
    Const WMSZ_TOPRIGHT As Integer = 5 
    Const WMSZ_BOTTOM As Integer = 6 
    Const WMSZ_BOTTOMLEFT As Integer = 7 
    Const WMSZ_BOTTOMRIGHT As Integer = 8 

    If m.Msg = WM_SIZING And m.HWnd.Equals(Me.Handle) Then 
     ' Turn the message's lParam into a Rect. 
     Dim r As Rect 
     r = DirectCast(_ 
      Marshal.PtrToStructure(m.LParam, _ 
       GetType(Rect)), _ 
      Rect) 

     ' The first time, save the form's aspect ratio. 
     If first_time Then 
      first_time = False 
      aspect_ratio = (r.bottom - r.top)/(r.right - _ 
       r.left) 
     End If 

     ' Get the current dimensions. 
     Dim wid As Double = r.right - r.left 
     Dim hgt As Double = r.bottom - r.top 

     ' Enlarge if necessary to preserve the aspect ratio. 
     If hgt/wid > aspect_ratio Then 
      ' It's too tall and thin. Make it wider. 
      wid = hgt/aspect_ratio 
     Else 
      ' It's too short and wide. Make it taller. 
      hgt = wid * aspect_ratio 
     End If 

     ' See if the user is dragging the top edge. 
     If m.WParam.ToInt32 = WMSZ_TOP Or _ 
      m.WParam.ToInt32 = WMSZ_TOPLEFT Or _ 
      m.WParam.ToInt32 = WMSZ_TOPRIGHT _ 
     Then 
      ' Reset the top. 
      r.top = r.bottom - CInt(hgt) 
     Else 
      ' Reset the height to the saved value. 
      r.bottom = r.top + CInt(hgt) 
     End If 

     ' See if the user is dragging the left edge. 
     If m.WParam.ToInt32 = WMSZ_LEFT Or _ 
      m.WParam.ToInt32 = WMSZ_TOPLEFT Or _ 
      m.WParam.ToInt32 = WMSZ_BOTTOMLEFT _ 
     Then 
      ' Reset the left. 
      r.left = r.right - CInt(wid) 
     Else 
      ' Reset the width to the saved value. 
      r.right = r.left + CInt(wid) 
     End If 

     ' Update the Message object's LParam field. 
     Marshal.StructureToPtr(r, m.LParam, True) 
    End If 

    MyBase.WndProc(m) 
End Sub 

답변

0

당신은 Screen.PrimaryScreen.Bounds.Bottom

+0

그래'SystemInformation.VirtualScreen.Width'은 단지뿐만 아니라 작동 Screen.PrimaryScreen.Bounds.Right와 사용자의 바탕 화면 해상도를 확인할 수 있습니다. 문제는 내가 위의 코드를 수정하여 한계에 도달했을 때 잠기는 것입니다. 왜냐하면 제한 중 하나가 트리거 될 때 aspect를 유지하지 않고 크기 조정을 시작하기 때문에, 거의 대부분 폭 제한이지만, 높이는 아마도 드물게 발생할 수 있습니다. – SSpoke

관련 문제