0
Windows命令控制檯只允許您將窗口大小調整爲字符大小的倍數。窗口大小的這種「捕捉」是瞬時的,不閃爍。在本機代碼中,這是通過處理WM_SIZING
消息並相應地修改RECT
結構來完成的。將窗口大小修改爲固定倍數
在C#中,我試着覆蓋OnResize
方法,計算「snapped」大小,並相應地設置Form的ClientSize
屬性。不幸的是,大小一直在快照大小和光標當前規定的大小之間跳躍。
protected override void OnResize(EventArgs e)
{
int tgtCols = (ClientSize.Width + 4)/8;
int tgtLines = (ClientSize.Height + 8)/15;
if (cols != tgtCols || lines != tgtLines)
{
cols = tgtCols;
lines = tgtLines;
int tgtWidth = cols * 8;
int tgtHeight = lines * 15;
//ClientSize = new Size(tgtWidth, tgtHeight);
Size = new Size(tgtWidth + exWidth, tgtHeight + exHeight);
}
base.OnResize(e);
}
正如你所看到的,我同時使用Size
和ClientSize
性嘗試,但都產生相同的效果。有沒有更好的方法來限制尺寸?或者我是否需要手動攔截WM_SIZING
消息?
編輯:我也嘗試過手動攔截WM_SIZING
,但我得到了相同的結果:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_SIZING:
DoSizeSnap(ref m);
m.Result = new IntPtr(1);
break;
default:
base.WndProc(ref m);
break;
}
}
private unsafe void DoSizeSnap(ref Message m)
{
int edge = m.WParam.ToInt32();
RECT *pRect = (RECT *)m.LParam.ToPointer();
int tgtCols = (pRect->right - pRect->left - exWidth + 4)/8;
int tgtLines = (pRect->bottom - pRect->top - exHeight + 8)/15;
if (cols != tgtCols || lines != tgtLines)
{
cols = tgtCols;
lines = tgtLines;
int tgtWidth = cols * 8;
int tgtHeight = lines * 15;
// TODO: handle edge
pRect->right = pRect->left + tgtWidth + exWidth;
pRect->bottom = pRect->top + tgtHeight + exHeight;
}
}
訂閱到Resize
事件也不起作用。
它看起來像你的解決方案[應該工作](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.resize(v = vs.110).aspx)。您是否嘗試訂閱「Resize」事件? – Zer0 2014-09-10 15:24:32
@ Zer0同樣的事情發生。嘗試您鏈接的方形尺寸示例似乎可行,儘管閃爍。 – 2014-09-10 15:34:09