簡體   English   中英

如何在運行時確定.NET列表視圖的尺寸?

[英]How do I determine the dimensions of a .NET listview at runtime?

我有一個帶有拆分容器的表單,每個面板都有一個列表視圖和一些按鈕。 我想確保標題和至少兩行在列表視圖中可見。 我最初是通過直觀地微調值(在我的XP開發機器上)來確定panel1MinSize和panel2MinSize的值的。 在我在Windows Vista上測試我的應用程序之前,這種方法很好-列表視圖的基本尺寸不同,並且列表視圖太小。

為了克服這個問題,我相信我需要在運行時確定listview的尺寸,並做一些數學運算來設置面板的最小尺寸,但是我看不到如何做到這一點。 這是正確的方法還是有更簡單的方法?

聽起來像是屏幕DPI問題。 當涉及DPI和控件大小時,Winforms是經典的“ PITA”。 在集成開發環境中,我們擁有96 dpi屏幕分辨率的Windows XP開發人員和120 dpi筆記本電腦上的Windows Vista開發人員,這導致Windows窗體設計人員不斷地重新安排。 最后,我們確定在屏幕上設置大小/位置時,所有控件都必須使用4的倍數,以最大程度地減少問題。

在較小程度上,WPF解決了此問題,並且使用不同的DPI時,Vista / Windows7 / XP應用程序看起來基本相同。 但是WindowsForms到WPF並不是一項直接的技術更改。 我不確定您是否可以在Windows Forms應用程序中輕松獲得ListViewItem的大小。

當我查看Windows窗體的屏幕打印代碼時,在將其渲染為位圖之前,它使用Size方法獲取尺寸。 也許這可能有效。

Public Class PrintScreen

    '   Code that explicity finds the outter rectangle size of the current form and then 
    '   takes a screen print of that image.
    Shared Sub CurrentForm(ByRef myForm As Windows.Forms.Form)
        Dim memoryImage As Bitmap

        Dim myGraphics As Graphics = myForm.CreateGraphics()
        Dim s As Size = myForm.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(myForm.Location.X, myForm.Location.Y, 0, 0, s)
        memoryGraphics.DrawImage(memoryImage, 0, 0)

        SaveImage(memoryImage, myForm)
    End Sub

    '   Method to save the screen to a file in the \My Pictures\AppName\ folder
    Private Shared Sub SaveImage(ByVal b As Bitmap, ByVal form As Windows.Forms.Form)
        Dim AppPictureFolder As String
        Dim fileName As String
        '   Create a file with the forms title + datetime stamp.
        fileName = String.Format("{0} {1}.png", form.Text, Date.Now.ToString("yyyyMMdd HHmmss"))
        AppPictureFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), [Assembly].GetExecutingAssembly.GetName.Name)
        If Not System.IO.Directory.Exists(AppPictureFolder) Then
            System.IO.Directory.CreateDirectory(AppPictureFolder)
        End If
        b.Save(System.IO.Path.Combine(AppPictureFolder, fileName))
        System.Diagnostics.Process.Start(System.IO.Path.Combine(AppPictureFolder, fileName))
    End Sub
End Class

在WPF中,ListViewItem包含您可以使用的更多詳細信息。 例如

System.Windows.Controls.ListView.ListViewItem.ActualHeight請參閱[MSDN] [1]

另外,我認為WPF還可以為列表提供一些相當靈活的布局選項。 我定期將包含復雜列表設計的WPF控件插入WindowsForms應用程序中,以獲得更好的布局控件。 但是就像我說的那樣,WPF是另一種魚。

[1]: http : //msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1 &l= EN-US &k=k% 28SYSTEM.WINDOWS.FRAMEWORKELEMENT.ACTUALHEIGHTPROPERTY%29; k% 28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION %3dV3.5%22%29; k%28DevLang-VB%29&rd = true “ MSDN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM