c# - 如何在运行时确定 .NET ListView 的维度?

标签 c# .net listview

我有一个带有拆分容器的表单,每个面板都有一个 ListView 和一些按钮。我想确保标题和至少两行在 ListView 中可见。我最初通过微调视觉(在我的 XP 开发机器上)的值来确定 panel1MinSize 和 panel2MinSize 的值。在我在 Windows Vista 上测试我的应用程序之前,这种方法一直很好—— 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]: 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

关于c# - 如何在运行时确定 .NET ListView 的维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2765272/

相关文章:

c# - 隐式类型变量必须初始化

c# - Windows 窗体中列表框内的复选框

android - 带有表格布局的 ListView

java - 自定义 ParseQueryAdapter 未填充 Android ListView

Android ListView 与 CardView : Last Card not fully displayed when scrolling

c# - 如何在运行时更改 wpf 中的 xmldataprovider 源?

c# - 什么是编程语言的反射属性?

.net - 版本控制友好、可扩展的二进制文件格式

c# - 处理异步方法同步部分的异常

c# - 打开 C : Directly with `FileStream` without `CreateFile` API