How to write your own custom Form

  • Thread starter Thread starter Sebastian Mark
  • Start date Start date
S

Sebastian Mark

I want to create my own form, I've tried inheriting :Form but its just
doesn't work for me since I want to do custom drawing for every part of
the form (overriding NCPAINT still does funky things)

I was trying to derive from ContainerControl but my "control" does not
appear unless I assign the parent. Why? What is the best way to approach
this problem?

Thanks
 
Hi Sebastian,

I just want to throw an idea. I don't know if you can work something out of
it. So the idea is the following. Hide the caption bar of a normal form and
draw everything yourself. Most of the details of making this form acting as
a normal form are pretty easy to handle. I'm posting a sample code that you
can compile and run. The sample has glitches, but is a good start I believe.

Here is the code. That is one very ugly form :)


--
HTH
Stoitcho Goutsev (100) [C# MVP]



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace WinApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.Info;
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 20);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(408, 370);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.BackColor = System.Drawing.Color.Aqua;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(384, 2);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(16, 16);
this.button1.TabIndex = 1;
this.button1.Text = "C";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Anchor =
((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Right)));
this.button2.BackColor = System.Drawing.Color.BlueViolet;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(360, 2);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(16, 16);
this.button2.TabIndex = 2;
this.button2.Text = "M";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(408, 390);
this.ControlBox = false;
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.DockPadding.Top = 20;
this.Name = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

protected override void OnPaint(PaintEventArgs e)
{
Rectangle caption = new Rectangle(0, 0, this.Width,
this.DockPadding.Top);
LinearGradientBrush lgb = new LinearGradientBrush(caption,
Color.Yellow,Color.Lime, 0, false);
e.Graphics.FillRectangle(lgb, caption);

}


private const int WM_NCHITTEST = 0x84;
private const int HTCAPTION = 0x2;
private const int HTCLIENT = 0x1;

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
if(m.Msg == WM_NCHITTEST && m.Result == new IntPtr(HTCLIENT))
{
m.Result = new IntPtr(HTCAPTION);

}

}

private const int WS_SYSMENU = 0x00080000;

private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
if(this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Maximized;
this.button2.Text = "R";
}
else
{
this.WindowState = FormWindowState.Normal;
this.button2.Text = "M";
}
}

protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style = cp.Style | WS_SYSMENU;
return cp;
}
}
}
}
 
Back
Top