C# 如何在运行时确定.NET listview的维度?

C# 如何在运行时确定.NET listview的维度?,c#,.net,listview,C#,.net,Listview,我有一个带有拆分容器的表单,每个面板都有一个列表视图和一些按钮。我想确保标题和至少两行在listview中可见。我最初是通过目视微调值(在我的XP开发机器上)来确定panel1MinSize和panel2MinSize的值的。在我在WindowsVista上测试我的应用程序之前,这种方法还不错——listview的基本尺寸不同,而且listview太小 为了克服这个问题,我认为我需要在运行时确定listview维度,并进行一些数学运算来设置面板的最小大小,但我不知道如何做到这一点。这是正确的方法

我有一个带有拆分容器的表单,每个面板都有一个列表视图和一些按钮。我想确保标题和至少两行在listview中可见。我最初是通过目视微调值(在我的XP开发机器上)来确定panel1MinSize和panel2MinSize的值的。在我在WindowsVista上测试我的应用程序之前,这种方法还不错——listview的基本尺寸不同,而且listview太小


为了克服这个问题,我认为我需要在运行时确定listview维度,并进行一些数学运算来设置面板的最小大小,但我不知道如何做到这一点。这是正确的方法还是更简单的方法?

听起来像是屏幕DPI问题。当涉及到DPI和控件大小时,Winforms是一个经典的“PITA”。在一个集成开发环境中,我们有96 dpi屏幕分辨率的windows XP开发人员和120 dpi笔记本电脑上的windows Vista开发人员,这导致windows窗体设计人员不断重新安排。最后,我们确定所有控件在设置屏幕上的大小/位置时必须使用4的倍数,以尽量减少问题

WPF在较小程度上解决了这个问题,当使用不同的DPI时,Vista/Windows7/XP应用程序看起来基本相同。但WindowsForms到WPF并不是一个直接的技术变化。我不确定您能否在Windows窗体应用程序中轻松获得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] :“MSDN听起来像是屏幕DPI问题。Winforms是经典的“PITA”“当涉及到DPI和控件大小时。在一个集成开发环境中,我们有96 dpi屏幕分辨率的windows XP开发人员和120 dpi笔记本电脑上的windows Vista开发人员,这导致windows窗体设计人员不断重新安排。最后,我们确定所有控件在设置屏幕上的大小/位置时必须使用4的倍数,以尽量减少问题

WPF在较小程度上解决了这个问题,当使用不同的DPI时,Vista/Windows7/XP应用程序看起来基本相同。但WindowsForms到WPF并不是一个直接的技术变化。我不确定您能否在Windows窗体应用程序中轻松获得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] :“MSDN

我理解您为什么要这样做,但我不确定WPF是否真的希望您能够这样做。不同的DPI分辨率(如前所述)如何,不同的字体、边框类型、列表视图控件中的更改等?我理解您为什么要这样做,但我不确定WPF是否真的希望您能够这样做。不同的DPI分辨率(如前所述)、不同的字体、边框类型、列表视图控件中的更改等如何。?