给自定义Control加上SnapLine(VS2005新特性)

Posted on 2005-12-19 21:22  Vitoria  阅读(1938)  评论(2编辑  收藏  举报

简单介绍:

VS2005提供了一种新的Layout设计方式:基准线(Snaplines)。其中包括 Text baselinemargin / padding snaplines.当拖动控件或者调整控件的大小,都可以看到这些基准线。同时用Ctrl+方向键也可以看到这些基准线。如图1

         图1

如果是自定义Control,怎么让它在拖动的时候也出现这样漂亮的基准线呢,本文为你介绍。
首先当然是需要写一个Control了,本例极为简单,旨在介绍SnapLine
public class CustomControl1 : Control
  
{
         
public CustomControl1()
         
{}
         
protected override void OnPaint(PaintEventArgs pe)   
       
{
              pe.Graphics.FillRectangle(
new SolidBrush(this.BackColor), pe.ClipRectangle);
              
base.OnPaint(pe);
         }

    }


然后为CustomControl1写一个Designer
public class CustomControlDesigner : ControlDesigner
  
{  
         
public override System.Collections.IList SnapLines
        
{
            
get
             
{
                 List
<SnapLine> snaplines = new List<SnapLine>();
                 snaplines.Add(
new SnapLine(SnapLineType.Baseline, 3));
                   
return snaplines;
               }

         }

   }

 最后把CustomControlDesigner挂在CustomControl
[Designer( typeof( CustomControlDesigner))]

public partial class CustomControl1 : Control

 

注意,关键就在SnapLines属性。它表明了可以出现哪些SnapLine。本文只添加了一个BaseLine

Member name

Description

Baseline

与其他Control之间的横方向的主要基准线,当然另一个Control也得有baseline 

Bottom

与其他Control之间的横方向的底部基准线,当然另一个Control也得有Bottom snapline 

Horizontal

与其他Control之间的横方向的中部基准线,当然另一个Control也得有horizontal snapline 

Left

与其他Control之间的纵方向的左基准线,当然另一个Control也得有Left snapline

Right

与其他Control之间的纵方向的右基准线,当然另一个Control也得有Right snapline 

Top

与其他Control之间的横方向的顶部基准线,当然另一个Control也得有Top snapline 

Vertical

与其他Control之间的纵方向的中部基准线,当然另一个Control也得有vertical snapline 



图2
 

结束语

SnapLine是一个比较好用的功能,如果是基于VS2005做一个自定义Control的话,这方面考虑细一些用起来会很舒服。细节就是成功的关键。

Copyright © 2024 Vitoria
Powered by .NET 8.0 on Kubernetes