Visual Studio 2015. Nowy projekt - Windows Forms. Chciałbym mieć okno o stałej wysokości - żeby user nie mógł jej zmienić. Za to niech sobie zmienia szerokość - co mi tam. Forma w tym nowym projekcie ma wymiar (Size
) 300x300. Robię jedną (dokładnie jedną) zmianę jej Properties
. Wpisuję do MaximumSize
wartość 200 dla Height
. IDE wpisuje mi te moje 200 do Height
w Size
. Ale także wpisuje, tak "od siebie", 112 (ciekawe dlaczego akurat tyle) do Width
w tymże Size
. W rezultacie mam Size
112x200. Kompiluję i uruchamiam. Pokazuje się okno, któremu nie można zmienić rozmiaru, mimo, że kursor myszy zamienia się na odpowiednią strzałkę przy brzegu tego okna. Ki diabeł?
Jak uzyskać tę stałą wysokość zmieniając ustawienia w Properties
?
0
0
Wielkie dzięki. Co prawda nie jest to w Properties
, ale działa. No i w vs2015 potrzeba i wystarcza ustawić wysokość (Size
-> Height
).
Jeszcze raz dzięki - ja nie wiedziałem, o co wujka pytać.
0
Oczywiście pisałem o drugim i trzecim linku - pierwszy to druciarstwo.
0
Jeszcze raz dziękuję za:
error91 napisał(a):
...
vertically-only-resizable-windows-form-in-c-sharp
limiting-form-resizing-to-horizontal
...
Pomyślałem sobie, że pewnie już dam radę zrobić sobie taką formę, która w IDE, w oknie Properties
będzie miała o dwie properties więcej - FixedWidth i FixedHeight. No i zrobiłem to tak:
- Wystartowałem nowy projekt w WindowsForm. Nie ruszając (na razie) automatycznie utworzonej formy dodałem do projektu drugą i zmieniłem jej kod tak:
using System;
using System.Windows.Forms;
namespace MyFixedSizes
{
public partial class myForm : Form
{
public myForm()
{
InitializeComponent();
FixedHeight = false;
FixedWidth = false;
}
private delegate void ExecuteMessage(ref Message m);
private void EmptyExecuteMessage(ref Message m) { }
private ExecuteMessage doFixedWidth;
private ExecuteMessage doFixedHeight;
static private bool _fixedWidth = false;
static private bool _fixedHeight = false;
public bool FixedHeight
{
get
{
return _fixedHeight;
}
set
{
_fixedHeight = value;
if (value) { doFixedHeight = fixedHeight; } else { doFixedHeight = EmptyExecuteMessage; };
}
}
public bool FixedWidth
{
get
{
return _fixedWidth;
}
set
{
_fixedWidth = value;
if (value) { doFixedWidth = fixedWidth; } else { doFixedWidth = EmptyExecuteMessage; };
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
doFixedHeight(ref m);
doFixedWidth(ref m);
}
private void fixedWidth(ref Message m)
{
switch (m.Msg)
{
case 0x84: //WM_NCHITTEST
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Left || result == HitTest.Right) m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.TopRight) m.Result = new IntPtr((int)HitTest.Top);
if (result == HitTest.BottomLeft || result == HitTest.BottomRight) m.Result = new IntPtr((int)HitTest.Bottom);
break;
}
}
private void fixedHeight(ref Message m)
{
switch (m.Msg)
{
case 0x84: //WM_NCHITTEST
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Top || result == HitTest.Bottom) m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.BottomLeft) m.Result = new IntPtr((int)HitTest.Left);
if (result == HitTest.TopRight || result == HitTest.BottomRight) m.Result = new IntPtr((int)HitTest.Right);
break;
}
}
enum HitTest
{
Error = -2,
Transparent = -1,
Nowhere = 0,
ClientArea = 1,
Caption = 2,
SysMenu = 3,
GrowBox = 4,
Menu = 5,
HorizontalScrollBar = 6,
VerticalScrollBar = 7,
MinimizeButton = 8,
MaximizeButton = 9,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18,
CloseButton = 20,
HelpButton = 21
}
}
}
- W kodzie automatycznie wygenerowanej pierwszej formy dopisałem dwie literki - my (i na razie nic więcej) uzyskując:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyFixedSizes
{
public partial class Form1 : myForm
{
public Form1()
{
InitializeComponent();
}
}
}
No i to działa. Przynajmniej nie widzę problemów. Ale może dostanę jakieś krytyczne uwagi? Bo najlepiej uczyć się na błędach.