用鼠标调整没有边框的窗体[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 hbxtlhx 的 blog


这里的代码是一个用来模拟用鼠标调整一个窗口的大小或位置的,有意思啊,是俺的生用msdn查出来的,觉得好用好玩,拿来和大家共享下,希望高人指点,以改进程序.

using system;
using system.drawing;
using system.windows.forms;

namespace movecontrollocation
{
 /// <summary>
 /// formbase 的摘要说明。
 /// 可以调整窗体的大小和移动窗体的位置,如需要可以从这个
 /// 类继承一个新的俱有这个特性的新的窗体类或窗体实例
 /// </summary>
 public class formbase : system.windows.forms.form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private system.componentmodel.container components = null;

  public formbase()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  #region windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   //
   // formbase
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(192, 146);
   this.name = "formbase";
   this.text = "formbase";

  }
  #endregion

  private const int wm_nchittest = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
  private const int htclient = 0x1;//工作区
  private const int htsysmenu = 3;//系统菜单
  private const int htcaption = 0x2; //标题栏

  private const int htleft = 10;//向左
  private const int htright = 11;//向右
  private const int httop = 12;//向上
  private const int httopleft = 13;//向左上
  private const int httopright = 14;//向右上
  private const int htbottom = 15;//向下
  private const int htbottomleft = 16;//向左下
  private const int htbottomright = 17;//向右下

  private const int borderwidth = 5;//自己定义的窗体边的宽度

  //可以调整窗体的大小和移动窗体的位置
  protected override void wndproc(ref message m)
  {
   switch(m.msg)
   {
    case wm_nchittest:
     base.wndproc(ref m);
     if (designmode)
     {
      return;
     }

     if ((int)m.result == htclient)//在客户区
      if ((cursor.position.x<=this.left + borderwidth) && (cursor.position.y <= this.top + borderwidth))
       m.result = (intptr)httopleft;//左上
      else if ((cursor.position.x>=this.left + this.width-borderwidth) && (cursor.position.y<=this.top +borderwidth))
       m.result = (intptr)httopright;//右上
      else if ((cursor.position.x <= this.left + borderwidth) && (cursor.position.y>=this.top + this.height-borderwidth))
       m.result = (intptr)htbottomleft;//左下
      else if ((cursor.position.x>=this.left + this.width-borderwidth) && (cursor.position.y>=this.top + this.height-borderwidth))
       m.result = (intptr)htbottomright;//右下
      else if (cursor.position.x<=this.left + borderwidth)
       m.result = (intptr)htleft;//左
      else if (cursor.position.x>=this.left + this.width-borderwidth)
       m.result = (intptr)htright;//右
      else if (cursor.position.y<=this.top + borderwidth)
       m.result = (intptr)httop;//上
      else if (cursor.position.y>=this.top + this.height-borderwidth)

本文关键:用鼠标调整没有边框的窗体
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top