- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现所有者绘制的 ListView
,因为基本控件占用了制表符,我需要用它来对齐列中的值。
使用 MSDN 中的示例作为基地,我能够接近。我仍然遇到的唯一问题是,当文本不适合列时使用的省略号句点比默认文本呈现更紧密地间隔在一起;重点是如果字体是粗体,句号会一起变成下划线。
下面的程序演示了这个问题。它有 4 个 ListView
:顶部的两个是使用默认渲染绘制的。底部的两个是所有者绘制的,右侧的一对是粗体。由于篇幅原因,我删除了所有不需要的内容以演示问题,这就是所有者绘制的 ListView
没有列标题的原因。
查看放大的屏幕截图,所有者绘制的 ListView
中的省略号间隔为一个像素;默认图形中的那些有两个像素的间距。当粗体将句点扩大到两个像素时,所有者绘制的句点会合并成一个看起来像下划线的实心 block 。
文本渲染也有其他细微差别;但省略号是唯一在不缩放的情况下很明显的省略号。然而,这些差异确实让我怀疑这个问题是一个更普遍的问题。 可能是 GDI 与 GDI+ 渲染?除了我认为这只会在应用程序级别有所不同。显然不会,切换 Application.SetCompatibleTextRenderingDefault()
不会影响任何东西。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class Form1 : Form
{
private void ListViewDrawSubItem(object sender,
DrawListViewSubItemEventArgs e)
{
ListView listView = sender as ListView;
using (StringFormat sf = new StringFormat())
{
// Draw the standard background.
e.DrawBackground();
sf.SetTabStops(0, new float[] {12, 12, 12, 12, 12});
sf.FormatFlags = sf.FormatFlags | StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
// Draw the header text.
// passing the controls font directly causes an
// ArguementException);
using (Font headerFont = new Font(listView.Font.Name,
listView.Font.Size,
listView.Font.Style))
{
e.Graphics.DrawString(e.SubItem.Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
}
public Form1()
{
InitializeComponent();
LoadData(listView1);
LoadData(listView2);
LoadData(listView3);
LoadData(listView4);
}
private void LoadData(ListView listView)
{
listView.Columns.Add("first", 35);
listView.Columns.Add("second", 75);
for (int i = 0; i < 5; i++)
{
listView.Items.Add("test");
listView.Items[i].SubItems.Add("test test test test");
}
}
#region from Form1.Designer
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
/// disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.listView2 = new System.Windows.Forms.ListView();
this.listView3 = new System.Windows.Forms.ListView();
this.listView4 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Location = new System.Drawing.Point(12, 12);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(121, 116);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// listView2
//
this.listView2.Font = new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.listView2.Location = new System.Drawing.Point(151, 12);
this.listView2.Name = "listView2";
this.listView2.Size = new System.Drawing.Size(121, 116);
this.listView2.TabIndex = 1;
this.listView2.UseCompatibleStateImageBehavior = false;
this.listView2.View = System.Windows.Forms.View.Details;
//
// listView3
//
this.listView3.Location = new System.Drawing.Point(12, 134);
this.listView3.Name = "listView3";
this.listView3.OwnerDraw = true;
this.listView3.Size = new System.Drawing.Size(121, 116);
this.listView3.TabIndex = 2;
this.listView3.UseCompatibleStateImageBehavior = false;
this.listView3.View = System.Windows.Forms.View.Details;
this.listView3.DrawSubItem += new
System.Windows.Forms.DrawListViewSubItemEventHandler(
this.ListViewDrawSubItem);
//
// listView4
//
this.listView4.Font = new System.Drawing.Font("Microsoft Sans Serif",
8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.listView4.Location = new System.Drawing.Point(151, 134);
this.listView4.Name = "listView4";
this.listView4.OwnerDraw = true;
this.listView4.Size = new System.Drawing.Size(121, 116);
this.listView4.TabIndex = 3;
this.listView4.UseCompatibleStateImageBehavior = false;
this.listView4.View = System.Windows.Forms.View.Details;
this.listView4.DrawSubItem += new
System.Windows.Forms.DrawListViewSubItemEventHandler(
this.ListViewDrawSubItem);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.listView4);
this.Controls.Add(this.listView3);
this.Controls.Add(this.listView2);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ListView listView2;
private System.Windows.Forms.ListView listView3;
private System.Windows.Forms.ListView listView4;
#endregion
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
最佳答案
我已经找到了绘制子项方法的临时实现。我的主要注意事项是选项卡大小是固定的(尽管如果需要更改它我可以降到 win32);并且我在我的机器上工作时需要的标志组合在 MSDN 中被报告为相互不兼容。
private void ListViewDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
//toggle colors if the item is highlighted
if (e.Item.Selected && e.Item.ListView.Focused)
{
e.SubItem.BackColor = SystemColors.Highlight;
e.SubItem.ForeColor = e.Item.ListView.BackColor;
}
else if (e.Item.Selected && !e.Item.ListView.Focused)
{
e.SubItem.BackColor = SystemColors.Control;
e.SubItem.ForeColor = e.Item.ListView.ForeColor;
}
else
{
e.SubItem.BackColor = e.Item.ListView.BackColor;
e.SubItem.ForeColor = e.Item.ListView.ForeColor;
}
// Draw the standard header background.
e.DrawBackground();
//add a 2 pixel buffer the match default behavior
Rectangle rec = new Rectangle(e.Bounds.X + 2, e.Bounds.Y+2,
e.Bounds.Width - 4, e.Bounds.Height-4);
//TODO Confirm combination of TextFormatFlags.EndEllipsis and
//TextFormatFlags.ExpandTabs works on all systems. MSDN claims they're
//exclusive but on Win7-64 they work.
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.EndEllipsis |
TextFormatFlags.ExpandTabs |
TextFormatFlags.SingleLine;
//If a different tab stop than the default is needed, will have to p/invoke
//DrawTextEx from win32.
TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.Item.ListView.Font, rec,
e.SubItem.ForeColor, flags);
}
关于c# - Listview 问题与 ownerdraw 模式文本呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358307/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!