You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms DataGridView缩放至150%时字体变粗问题求助

解决DataGridView 150%缩放时文本异常加粗的问题

以下是针对该问题的几种可行解决思路:

  • 强制启用Per-Monitor V2 DPI感知
    WinForms默认的DPI处理在部分缩放比例下可能出现控件渲染异常,通过显式设置进程的DPI感知模式为Per-Monitor V2,可以让控件更精准地适配不同缩放比例。在项目的Program.cs中添加如下代码:

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool SetProcessDpiAwarenessContext(int dpiFlag);
    private const int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4;
    
    static void Main()
    {
        SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new YourMainForm());
    }
    

    也可以通过修改项目的app.manifest文件,取消注释并配置DPI感知节点:

    <application xmlns="urn:schemas-microsoft-com:asm.v3">
      <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
        <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
      </windowsSettings>
    </application>
    
  • 自定义单元格绘制逻辑
    绕过DataGridView默认的文本渲染机制,手动绘制表头和单元格文本,避免系统缩放时的加粗失真。绑定CellPainting事件并实现自定义绘制:

    private void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        // 跳过空单元格或列索引无效的情况
        if (e.ColumnIndex < 0 || e.Value == null) return;
    
        // 绘制单元格背景、边框等基础元素
        e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground);
    
        // 手动绘制文本
        var text = e.Value.ToString();
        var font = e.CellStyle.Font;
        var foreColor = e.CellStyle.ForeColor;
        var formatFlags = e.RowIndex == -1 
            ? TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter 
            : TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
    
        using (var brush = new SolidBrush(foreColor))
        {
            TextRenderer.DrawText(e.Graphics, text, font, e.CellBounds, foreColor, formatFlags);
        }
    
        e.Handled = true;
    }
    
  • 显式指定字体的DPI兼容属性
    确保DataGridView使用的字体明确设置为常规样式,并指定正确的度量单位,避免系统自动调整字体样式:

    var regularFont = new Font("Segoe UI", 9, FontStyle.Regular, GraphicsUnit.Point);
    dataGridView1.DefaultCellStyle.Font = regularFont;
    dataGridView1.ColumnHeadersDefaultCellStyle.Font = regularFont;
    
  • 禁用自动缩放并手动适配
    若上述方法无效,可以尝试关闭DataGridView的自动缩放,根据当前DPI手动调整控件和字体大小:

    dataGridView1.AutoScaleMode = AutoScaleMode.None;
    float dpiScale = this.CreateGraphics().DpiX / 96f;
    var scaledFont = new Font("Segoe UI", 9 * dpiScale, FontStyle.Regular);
    dataGridView1.DefaultCellStyle.Font = scaledFont;
    dataGridView1.ColumnHeadersDefaultCellStyle.Font = scaledFont;
    // 手动调整列宽、行高适配缩放比例
    foreach (DataGridViewColumn col in dataGridView1.Columns)
    {
        col.Width = (int)(col.Width * dpiScale);
    }
    

内容的提问来源于stack exchange,提问作者Victor Calarasu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 01:20:18