How to write your own custom Form

本文讨论了如何在.NET中创建自定义窗体并进行全窗口范围的绘制,提供了使用LinearGradientBrush实现标题栏自绘的例子,并探讨了隐藏默认标题栏后的自定义绘制方案。
From:  Sebastian Mark - view profile
Date:  Fri, Nov 12 2004 3:18 am
Email:  Sebastian Mark <trap_rem...@terianREMOVE.net>
Groups:  microsoft.public.dotnet.framework.windowsforms
Not yet rated
Rating:
 
show options

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


From:  Bob Powell [MVP] - view profile
Date:  Fri, Nov 12 2004 5:50 am
Email:  "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net>
Groups:  microsoft.public.dotnet.framework.windowsforms
Not yet rated
Rating:
 
show options

You might be better off looking at NativeWindow.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml

 

"Sebastian Mark" <trap_rem ...@terianREMOVE.net> wrote in message

news:%238MrqNCyEHA.2568@TK2MSFTNGP11.phx.gbl...

 

- Show quoted text -

From:  Stoitcho Goutsev (100) [C# MVP] - view profile
Date:  Fri, Nov 12 2004 11:44 am
Email:  "Stoitcho Goutsev /(100/) [C# MVP]" <1...@100.com>
Groups:  microsoft.public.dotnet.framework.windowsforms
Not yet rated
Rating:
 
show options

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;
   }
  }
 }

 

}
"Sebastian Mark" <trap_rem ...@terianREMOVE.net> wrote in message

news:%238MrqNCyEHA.2568@TK2MSFTNGP11.phx.gbl...

 

>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

- Hide quoted text -
 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值