中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
用鼠标调整没有边框的窗体
作者:未知 时间:2004-11-11 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无


这里的代码是一个用来模拟用鼠标调整一个窗口的大小或位置的,有意思啊,是俺的生用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)
       m.Result = (IntPtr)HTBOTTOM;//下
      else if (Cursor.Position.Y<=this.Top + BorderWidth + 10 )
      {
       if (Cursor.Position.X<=this.Left + this.Width-BorderWidth + 10)
       {
        m.Result = (IntPtr)HTSYSMENU;//系统菜单,可以在这里双击鼠标关闭窗体。
       }
      }
      else
       m.Result = (IntPtr)HTCAPTION;//移动窗体,双击可以最大化窗体或还原。
     return;
    default:
     base.WndProc(ref m);
     break;
   }
  }
 }
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有