Notifications
Clear all

[Closed] Resizable form with no border style

I want to write a minimal UI for a sort of pop-up for one of my programs, that doesn’t have a titlebar. Here is the code, based on LoneRobot’s post from this thread: http://forums.cgsociety.org/archive/index.php?t-551473-p-2.html

try (Form.close()) catch()

fn maxHW =
	(
	nw = DotNetObject "NativeWindow"
	nw.AssignHandle (DotNetObject "System.IntPtr" (Windows.GetMaxHWND()))
	nw
	)

fn MaxFormClass = 
	(
	source = ""
	source += "Imports System.Windows.Forms
"
	source += "Public Class MaxFormPlus
"
	source += "Inherits MaxCustomControls.MaxForm
"
	source += "Const WM_NCHITTEST As Integer = &H84
"
	source += "Const HTCLIENT As Integer = &H1
"
	source += "Const HTCAPTION As Integer = &H2
"
	source += "Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
"
	source += "Select Case m.Msg
"
	source += "Case WM_NCHITTEST
"
	source += "MyBase.WndProc(m)
"
	source += "If m.Result = HTCLIENT Then m.Result = HTCAPTION
"	
	source += "Case Else
"
	source += "MyBase.WndProc(m)
"
	source += "End Select
"
	source += "End Sub
"
	source += "End Class"
	VBProvider = dotnetobject "Microsoft.VisualBasic.VBCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
	compilerParams.ReferencedAssemblies.add (getdir #maxroot +"MaxCustomControls.dll")
	compilerParams.ReferencedAssemblies.add "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll"	
	compilerParams.GenerateInMemory = on
	compilerResults = VBProvider.CompileAssemblyFromSource compilerParams #(source)
	return compilerResults.CompiledAssembly.CreateInstance "MaxFormPlus"
	)

global Form = MaxFormPlusClass()
Form.ShowInTaskBar = false
Form.formBorderStyle=(dotNetClass "System.Windows.Forms.FormBorderStyle").None

CloseButton = dotNetObject "System.Windows.Forms.Button"
Form.controls.add CloseButton
fn closeform sender arg = if arg.button==arg.button.left then Form.close()
dotNet.addEventHandler CloseButton "mouseDown" closeform


Form.Show(maxHW())

My problem is that I want the form to be resizable, but when I set the border style to “None”, it loses that functionality. There are all sorts of solutions on the internet when you google “c# resize form border style none”, but I can’t for the life of me integrate any of them through maxscript. I’m just getting my feet wet with dotnet and this is just beyond my capabilities at the moment. I’d hugely appreciate any help.

8 Replies

I tried using this code, but it just gives an error.

	source = ""
	source += "Imports System.Windows.Forms
"
	source += "Public Class MaxFormPlus
"
	source += "Inherits MaxCustomControls.MaxForm
"
	source += "Private Const cGrip As Integer = 16
"
	source += "Private Const cCaption As Integer = 32
"
    source += "Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
"
	source += "If m.Msg = &H84 Then
"
	source += "Dim pos As New Point(m.LParam.ToInt32() And &HFFFF, m.LParam.ToInt32() >> 16)
"
	source += "pos = Me.PointToClient(pos)
"
	source += "If pos.Y < cCaption Then
"
	source += "m.Result = IntPtr.op_Explicit(2)
"
	source += "Return
"
	source += "End If
"
	source += "If pos.X >= Me.ClientSize.Width - cGrip AndAlso pos.Y >= Me.ClientSize.Height - cGrip Then
"
	source += "m.Result = IntPtr.op_Explicit(17)
"
	source += "Return
"
	source += "End If
"
	source += "End If
"
	source += "MyBase.WndProc(m)
"
	source += "End Sub
"
	source += "End Class"

http://stackoverflow.com/questions/2575216/how-to-move-and-resize-a-form-without-a-border

the first answer looks right for me. it’s what i would try.

Even if it was correct, when I copy/paste it in the source , it just gives this error:

“– Runtime error: dotNet runtime exception: Cannot access a disposed object.
Object name: ‘MaxFormPlus’.”

I’ve actually already tried the code in this link as well as a few more and they all give the same error. Maybe the mistake is in my formating?

here is my attempt:

fn MaxFormPlusClass = 
	(
	source = ""
	source += "Imports System.Windows.Forms
"
	source += "Inherits MaxCustomControls.MaxForm
"
	source += "public partial class MaxFormPlus : Form {
"
    source += "public MaxFormPlus() {
"
	source += "InitializeComponent();
"
	source += "this.FormBorderStyle = FormBorderStyle.None;
"
	source += "this.DoubleBuffered = true;
"
    source += "this.SetStyle(ControlStyles.ResizeRedraw, true);
"
    source += "}
"
	source += "private const int cGrip = 16;      // Grip size
"
    source += "private const int cCaption = 32;   // Caption bar height;
"
    source += "protected override void OnPaint(PaintEventArgs e) {
"
	source += "Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
"
	source += "ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
"
	source += "rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
"
	source += "e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
"
    source += "}
"
    source += "protected override void WndProc(ref Message m) {
"
	source += "if (m.Msg == 0x84) {  // Trap WM_NCHITTEST
"
	source += "Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
"
	source += "pos = this.PointToClient(pos);
"
	source += "if (pos.Y < cCaption) {
"
	source += "m.Result = (IntPtr)2;  // HTCAPTION
"
	source += "return;
"
	source += "}
"
	source += "if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip) {
"
    source += "m.Result = (IntPtr)17; // HTBOTTOMRIGHT
"
    source += "return;
"
    source += "}
"
    source += "}
"
    source += "base.WndProc(ref m);
"
    source += "}
"
	source += "}
"
	source += "End Class"
	VBProvider = dotnetobject "Microsoft.VisualBasic.VBCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
	compilerParams.ReferencedAssemblies.add (getdir #maxroot +"MaxCustomControls.dll")
	compilerParams.ReferencedAssemblies.add "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll"	
	compilerParams.GenerateInMemory = on
	compilerResults = VBProvider.CompileAssemblyFromSource compilerParams #(source)
	return compilerResults.CompiledAssembly.CreateInstance "MaxFormPlus"
	)

do you know c#?
why do you use VB compiler?

I don’t I just wanted to use the functionality that it provides just for the form itself and do everything else in maxscript.
As for why I use VB compiler? I don’t know… Sorry for the noobish response, but I just tried to use LoneRobot’s script and work around it to get the result I wanted. I am a complete beginner with c# and I’ve only just started learning the language… I’m eager to improve though.

Welp, this was embarrassing. Shows you just how much I know about the C languages when I can’t even distinguish between C# and VB… I tried placing this after the souce:

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"
	compilerParams.ReferencedAssemblies.add (getdir #maxroot +"MaxCustomControls.dll")
	compilerParams.ReferencedAssemblies.add "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll"
-- 	compilerParams.ReferencedAssemblies.AddRange #("System.dll", "System.Windows.Forms.dll")
	compilerParams.GenerateInMemory = true
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
	return compilerResults.CompiledAssembly.CreateInstance "MaxFormPlus"

But it still gives an error where it says it is missing a dll with a different name every time in the AppData\Local\Temp folder.
Also my first attempt was in VB and the compiler was VB as well and both times they just don’t work. What am I missing here?

i hope it’s not a tendency of using anyone’s else code without trying to understand how it works.

so here is a solution based on http://stackoverflow.com/questions/2575216/how-to-move-and-resize-a-form-without-a-border


global _customFormsAssembly
fn CreateCustomFormsAssembly = 
(
source  = ""
source += "using System;
"
source += "using System.Windows.Forms;
"
source += "using System.Drawing;
"
source += "namespace CustomForms
"
source += "{
"
source += "    public partial class NoBorderForm : Form
"
source += "    {
"
source += "        private void InitializeComponent()
"
source += "        {
"
source += "            this.FormBorderStyle = FormBorderStyle.None;
"
source += "            this.DoubleBuffered = true;
"
source += "            this.SetStyle(ControlStyles.ResizeRedraw, true);
"
source += "        }
"
source += "        public NoBorderForm()
"
source += "        {
"
source += "            InitializeComponent();
"
source += "        }
"
source += "        private const int cGrip = 8;      // Grip size
"
source += "        private const int cCaption = 16;   // Caption bar height;
"
source += "        const int WM_NCHITTEST = 0x84;
"
source += "        const int HTCAPTION = 2;
"
source += "        const int HTLEFT = 10;
"
source += "        const int HTRIGHT = 11;
"
source += "        const int HTBOTTOM = 15;
"
source += "        const int HTBOTTOMRIGHT = 17;
"
source += "        
"
source += "        /* Draw Caption Bar and Grip if you need
"
source += "        protected override void OnPaint(PaintEventArgs e)
"
source += "        {
"
source += "            Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
"
source += "            ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
"
source += "            rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
"
source += "            e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
"
source += "        }
"
source += "        */
"
source += "        protected override void WndProc(ref Message m)
"
source += "        {
"
source += "            if (m.Msg == WM_NCHITTEST)
"
source += "            {  // Trap WM_NCHITTEST
"
source += "                Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
"
source += "                pos = this.PointToClient(pos);
"
source += "                if (pos.Y < cCaption)
"
source += "                {
"
source += "                    m.Result = (IntPtr)HTCAPTION;  // HTCAPTION
"
source += "                    return;
"
source += "                }
"
source += "                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
"
source += "                {
"
source += "                    m.Result = (IntPtr)HTBOTTOMRIGHT; // BOTTOMRIGHT
"
source += "                    return;
"
source += "                }
"
source += "                if (pos.X >= this.ClientSize.Width - cGrip && pos.Y > cCaption)
"
source += "                {
"
source += "                    m.Result = (IntPtr)HTRIGHT; // RIGHT
"
source += "                    return;
"
source += "                }
"
source += "                if (pos.X <= cGrip && pos.Y > cCaption)
"
source += "                {
"
source += "                    m.Result = (IntPtr)HTLEFT; // RIGHT
"
source += "                    return;
"
source += "                }
"
source += "                if (pos.Y >= this.ClientSize.Height - cGrip)
"
source += "                {
"
source += "                    m.Result = (IntPtr)HTBOTTOM; // BOTTOM
"
source += "                    return;
"
source += "                }
"
source += "            }
"
source += "            base.WndProc(ref m);
"
source += "        }
"
source += "    }
"
source += "}
"

	csharpProvider = dotnetobject "Microsoft.CSharp.CSharpCodeProvider"
	compilerParams = dotnetobject "System.CodeDom.Compiler.CompilerParameters"

	compilerParams.ReferencedAssemblies.AddRange #("System.dll", "System.Windows.Forms.dll", "System.Drawing.dll")

	compilerParams.GenerateInMemory = true
	compilerResults = csharpProvider.CompileAssemblyFromSource compilerParams #(source)
		
	if (compilerResults.Errors.Count > 0) then MessageBox "Errors encountered while compiling C# code" title:"Error" else
	(
		_customFormsAssembly = compilerResults.CompiledAssembly
	)
)
CreateCustomFormsAssembly()

/******** run code below as an example of the using: ********

form = _customFormsAssembly.createinstance "CustomForms.NoBorderForm"
(
	MaxHandleWrapper = dotnetobject "MaxCustomControls.Win32HandleWrapper" (dotnetobject "System.IntPtr" (windows.getmaxhwnd()))

	form.Text = "No Border Form"
	form.ShowInTaskbar = off

	bt = dotnetobject "Button"
	bt.Text = bit.intaschar 215 --"×"
	bt.Dock = bt.Dock.Right
	bt.FlatStyle = bt.FlatStyle.Flat
	bt.FlatAppearance.BorderSize = 0
	bt.MaximumSize = dotnetobject "System.Drawing.Size" 16 16

	form.controls.addrange #(bt)	

	fn onClick s e = 
	(
		f = s.TopLevelControl
		f.Close()
	)
	dotnet.addEventHandler bt "Click" onClick

	form.Show MaxHandleWrapper
)
--form.Close()
*/

Thank you for your help
I’m sorry if I gave the impression I wasn’t trying to understand the code. I never ask anything in this forum without first trying to tackle the problem for less than 1-2 weeks. I’m just new to dotnet and this problem was just way over my level of programming. I’ll do my very best to understand your solution, I assure you. It’s the only way to learn after all.
I appreciate your help a lot