Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
  首页  :: 联系 :: 订阅 订阅  :: 管理

CreateParams Is Pretty Cool!

Posted on 2006-04-29 23:30  Sheva  阅读(2500)  评论(7编辑  收藏  举报
    Probably I've been in the WPF world for so long that I need to try something different, yesterday, I come across a Channel9 forum thread on how to use windows forms' Control.CreateParams property, which give me some light on how to customize the style of classical Win32 window.
    First thing I plan to do is make the System.Windows.Forms.Form window support dropshadow, then I write a custom NumberBox control which only accepts numbers, traditionaly if you want a TexBox to only accept numbers, you can hookup it's KeyDown event to verify the actual keyboard input, in this post, I will show another way to do it, aka using edit control style as the following code demonstrates:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CreateParamsDemo
{
    
public partial class MyCustomForm : Form
    {
        
private static Int32 CS_DROPSHADOW = 0x00020000;
        
        
public MyCustomForm()
        {
            InitializeComponent();
            NumberBox box 
= new NumberBox();
            box.Multiline 
= true;
            box.Dock 
= DockStyle.Fill;
            
this.Controls.Add(box);
        }

        
protected override CreateParams CreateParams
        {
            
get
            {
                CreateParams parameters 
= base.CreateParams;
                parameters.ClassStyle 
|= CS_DROPSHADOW;

                
return parameters;
            }
        }
    }

    
public class NumberBox : TextBox
    {
        
private static Int32 ES_NUMBER = 0x2000;
        
protected override CreateParams CreateParams
        {
            
get
            {
                CreateParams parameters 
= base.CreateParams;
                parameters.Style 
|= ES_NUMBER;

                
return parameters;
            }
        }
    }
}
   Now, the code is up and ready for running, here comes the screenshot:

 

    Please pay closer attention to the shadow around the window border, you will get the dropshadow for the window, and if you press any key except number keys, a balloon tool tip will show up, saying "Unacceptable Character", the implementation is quite simple, but you can actually get your job done quickly, you know, sometimes, simple approach is the best:)