如何在获胜10表格中替换标题栏上的图标



我正在尝试用更大的.png替换获胜表单的图标无论我是尝试绘制矩形还是加载图像;我在标题栏里什么也没得到。我正在使用visual studio 2019上的一个胜利64位。下面的代码就是我正在使用的。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public const uint WM_NCPAINT = 0x85;
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
public Form1()
{
InitializeComponent();
this.ShowIcon = false;
this.Text = String.Empty;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCPAINT)
{
IntPtr dc = GetWindowDC(m.HWnd);
try
{
using (Graphics g = Graphics.FromHdc(dc))
{
//Rectangle rect = new Rectangle(0, 0, 30, 20);
//g.FillRectangle(Brushes.Blue, rect);
//g.Flush();
g.DrawImage(Bitmap.FromFile("C:\Picture1.png"), new Point(0, 0));
}
}
finally
{
ReleaseDC(m.HWnd, dc);
}
}
}
}
}

这就是我想要实现的。1

感谢的任何帮助

根据WM_NCPAINT:

返回值:如果应用程序处理此消息,则返回零。

如果已处理WM_NCPAINT,则无法调用默认的WndProc

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCPAINT)
{
IntPtr dc = GetWindowDC(m.HWnd);

try
{
using (Graphics g = Graphics.FromHdc(dc))
{
//Rectangle rect = new Rectangle(0, 0, 30, 20);
//g.FillRectangle(Brushes.Blue, rect);
//g.Flush();
g.DrawImage(Bitmap.FromFile("C:\head.png"), new Point(0, 0));
InvalidateRect(m.HWnd, IntPtr.Zero, 0);
}
}
finally
{
ReleaseDC(m.HWnd, dc);
}
}
else
base.WndProc(ref m);
}

但你需要自己画其他东西,比如系统按钮。作为注释,您可以使用文档创建自定义窗口。

WM_NCCALCSIZE:移除标准帧

从Windows Vista开始,只需当wParam为TRUE 时返回0

if (m.Msg == WM_NCPAINT)
{
...
}
else if (m.Msg == WM_NCCALCSIZE)
{
if(m.WParam == IntPtr.Zero)
base.WndProc(ref m);
}
else
base.WndProc(ref m);

这将删除标准帧,并禁用点击、调整大小和移动。您可以在文档中使用HitTestNCA来启用它们。

最后,尝试在PaintCustomCaption函数中绘制标题图标。

或者你可以试试这个由@Aland Li提供的定制表格样本答案。

最新更新