PDA

View Full Version : [c#] Problema con ridimensionamento di un Form


stdecden
24-02-2009, 18:03
Salve a tutti,

Sto creando un piccolo progetto in c#. Ho creato un Form senza barra del titolo al cui interno ho messo una Listbox(modificata per poter visualizzare le immagini) e ho settato dock = fill;

http://img4.imageshack.us/img4/2931/60387358.gif

Come sapete una Listbox modifica la dimensione in modo che nessun'elemento sia visibile solo per metà. Vorrei avere lo stesso comportamente anche sul Form, così da non avere spazi vuoti(come nell'immagine). Ho provato a sovrascrivere la funzione OnResize:

protected override void OnResize(EventArgs e)
{
this.Height = (this.Height / 16) * 16 + 4;
base.OnResize(e);
}

Funziona tutto tranne il fatto che l'azione non è fluida come vorrei io. (come per esempio l'intellisense di Visual C#)

MarcoGG
24-02-2009, 23:35
Come sapete una Listbox modifica la dimensione in modo che nessun'elemento sia visibile solo per metà.


Non è necessariamente vero. Succede solo se lasci la proprietà IntegralHeight su True ( valore di default ), mentre se la setti a False, col controllo in Dock Fill, la ListBox prende comunque tutta la Form, quale che sia la dimensione del Font... ;)

stdecden
25-02-2009, 10:40
Non è necessariamente vero. Succede solo se lasci la proprietà IntegralHeight su True ( valore di default ), mentre se la setti a False, col controllo in Dock Fill, la ListBox prende comunque tutta la Form, quale che sia la dimensione del Font... ;)

Hai ragione, comunque io intendevo raggiungere quel comportamento. Ho risolto andando un po'piú low-level:


public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

public const Int32 WM_NCHITTEST = 132;
public const Int32 HTTOP = 12;
public const Int32 HTTOPLEFT = 13;
public const Int32 HTTOPRIGHT = 14;
public const Int32 HTBOTTOMLEFT = 16;
public const Int32 HTBOTTOMRIGHT = 17;

public const int WM_SIZING = 0x214;
public const Int32 WMSZ_LEFT = 1;
public const Int32 WMSZ_RIGHT = 2;
public const Int32 WMSZ_TOP = 3;
public const Int32 WMSZ_BOTTOM = 6;
public const Int32 WMSZ_BOTTOMLEFT = 7;
public const Int32 WMSZ_BOTTOMRIGHT = 8;

protected override void WndProc(ref Message m)
{
Int32 res;
switch (m.Msg)
{
case WM_NCHITTEST:
{
base.WndProc(ref m);
res = m.Result.ToInt32();
}
break;

case WM_SIZING:
{
RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
res = m.WParam.ToInt32();
if (res == WMSZ_RIGHT || res == WMSZ_LEFT || res == WMSZ_BOTTOM || res == WMSZ_BOTTOMLEFT || res == WMSZ_BOTTOMRIGHT)
{
int deltaW = this.Width - this.ClientSize.Width;
int deltaH = this.Height - this.ClientSize.Height;
rc.Bottom = rc.Top + Math.Min(((rc.Bottom - rc.Top - deltaH) / 16), this.Count) * 16 + 4 + deltaH; // Adjust size
}
else
{
int deltaW = this.Width - this.ClientSize.Width;
int deltaH = this.Height - this.ClientSize.Height;
rc.Top = rc.Bottom - (Math.Min(((rc.Bottom - rc.Top - deltaH) / 16), this.Count) * 16 + 4 + deltaH); // Adjust size
}
Marshal.StructureToPtr(rc, m.LParam, true);
}
break;

default:
break;
}
base.WndProc(ref m);
}