为什么响应WM_NCCALCSIZE后我的表单越来越小? [英] Why my form is getting smaller and smaller after responding to WM_NCCALCSIZE?
本文介绍了为什么响应WM_NCCALCSIZE后我的表单越来越小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想自己绘制标题栏,但标题栏的高度与系统的高度不同,要增加标题栏的高度,然后我响应WM_NCCALCSIZE
但是,如果我不断地最大化和还原,形式将变得越来越小
发生了什么事?
希望有人能回答,谢谢!
I would like to paint the title bar by myself, but my title bar height is not the same as the system''s, to increase the height of the title bar, I then respond to WM_NCCALCSIZE
But if i constantly maximize and restore,the form will become increasingly smaller
what happended?
Hope someone can answer, thank you!
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32")]
public static extern int GetSystemMetrics(int nIndex);
private const int SM_CYCAPTION = 4;
private const int SM_CXFRAME = 32;
private const int SM_CYFRAME = 33;
private const int WM_NCCALCSIZE = 0x83;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct PWINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public uint flags;
}
[StructLayout(LayoutKind.Sequential)]
public struct NCCALCSIZE_PARAMS
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public RECT[] rgrc;
public PWINDOWPOS lppos;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCCALCSIZE && m.WParam != IntPtr.Zero)
{
NCCALCSIZE_PARAMS Params;
Params = (NCCALCSIZE_PARAMS)m.GetLParam(typeof(NCCALCSIZE_PARAMS));
Params.rgrc[0].Top += 40 - (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)); //caption height
Marshal.StructureToPtr(Params, m.LParam, false);
}
base.WndProc(ref m);
}
}
}
推荐答案
在进行计算时,您必须考虑Windows主题,以免将40之类的硬编码数字添加到Top属性中. br/>
请看以下链接,了解有关其完成方式的示例:在Codeplex上的CustomBorderForm
You have to take into account the windows themes when doing your calculations so you can''t hard code numbers like 40 into the Top property.
Take a look at the following link for samples on how it''s done : CustomBorderForm on codeplex
好吧,也许我找到了答案?
well,maybe i have found the answer?
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, height + 40 - (GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME)), specified);
}
但是什么是SetBoundsCore?
but what is SetBoundsCore?
这篇关于为什么响应WM_NCCALCSIZE后我的表单越来越小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文