如何模拟一个象窗体一样的控件(标题栏、焦点、拖动、改变大小、关闭等等)

原创 2002年06月13日 13:29:00

     用过SQL Server视图设计或Access查询设计的都见过这样的控件,控件外形象一个窗体,有边框、标题栏、图标、关闭按钮,可拖动、改变大小等等

     我前一段时间在做一个自定义查询,想把界面做成象SQL Server的设计视图那样,终于在MSDN里面找到了一些资料

MSDN的一些URL(把msdn的安装路径改成你自己的路径):

mk:@MSITStore:d:/Program%20Files/Microsoft%20Visual%20Studio/MSDN/2001JAN/1033/winui.chm::/hh/winui/mousinpt_7ik4.htm

mk:@MSITStore:d:/Program%20Files/Microsoft%20Visual%20Studio/MSDN/2001JAN/1033/winui.chm::/hh/winui/mousinpt_6085.htm

一、添加一个User Control,控件结构如下

VERSION 5.00
Begin VB.UserControl TableView
   AutoRedraw      =   -1  'True
   ClientHeight    =   4260
   ClientLeft      =   0
   ClientTop       =   0
   ClientWidth     =   3855
   EditAtDesignTime=   -1  'True
   KeyPreview      =   -1  'True
   ScaleHeight     =   4260
   ScaleWidth      =   3855
   Begin VB.PictureBox picTitle
      BackColor       =   &H80000003&
      BorderStyle     =   0  'None
      Height          =   315
      Left            =   120
      ScaleHeight     =   315
      ScaleWidth      =   2715
      TabIndex        =   1
      Top             =   120
      Width           =   2715
      Begin VB.Image imgClose
         Height          =   210
         Index           =   1
         Left            =   2400
         Picture         =   "TableView.ctx":0000
         Top             =   0
         Width           =   240
      End
      Begin VB.Image imgTitle
         Height          =   180
         Left            =   60
         Picture         =   "TableView.ctx":02E2
         Top             =   60
         Width           =   180
      End
      Begin VB.Image imgClose
         Height          =   210
         Index           =   0
         Left            =   1560
         Picture         =   "TableView.ctx":04D4
         Top             =   0
         Width           =   240
      End
      Begin VB.Label lblTitle
         BackColor       =   &H80000003&
         BeginProperty Font
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         ForeColor       =   &H8000000F&
         Height          =   255
         Left            =   240
         TabIndex        =   3
         Top             =   120
         Width           =   1995
      End
   End
   Begin VB.ListBox lstColumn
      Height          =   1275
      IntegralHeight  =   0   'False
      ItemData        =   "TableView.ctx":07B6
      Left            =   360
      List            =   "TableView.ctx":07B8
      OLEDragMode     =   1  'Automatic
      OLEDropMode     =   1  'Manual
      Style           =   1  'Checkbox
      TabIndex        =   0
      TabStop         =   0   'False
      Top             =   600
      Width           =   2175
   End
   Begin VB.CommandButton cmdBack
      Height          =   2655
      Left            =   0
      TabIndex        =   2
      TabStop         =   0   'False
      Top             =   0
      Width           =   2895
   End
End
Attribute VB_Name = "TableView"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False

二、声明

' WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes
Const HTERROR = (-2)
Const HTTRANSPARENT = (-1)
Const HTNOWHERE = 0
Const HTCLIENT = 1
Const HTCAPTION = 2
Const HTSYSMENU = 3
Const HTGROWBOX = 4
Const HTSIZE = HTGROWBOX
Const HTMENU = 5
Const HTHSCROLL = 6
Const HTVSCROLL = 7
Const HTMINBUTTON = 8
Const HTMAXBUTTON = 9
Const HTLEFT = 10
Const HTRIGHT = 11
Const HTTOP = 12
Const HTTOPLEFT = 13
Const HTTOPRIGHT = 14
Const HTBOTTOM = 15
Const HTBOTTOMLEFT = 16
Const HTBOTTOMRIGHT = 17
Const HTBORDER = 18
Const HTREDUCE = HTMINBUTTON
Const HTZOOM = HTMAXBUTTON
Const HTSIZEFIRST = HTLEFT
Const HTSIZELAST = HTBOTTOMRIGHT


Const WM_SIZE = &H5

Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2
Const WM_CLOSE = &H10

Const WM_LBUTTONDOWN = &H201
Const MK_LBUTTON = &H1
Const WM_MOUSEMOVE = &H200
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

 

三、代码

'拖动
Private Sub picTitle_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 1 Then
        ReleaseCapture
        SendMessage UserControl.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
    End If
End Sub

Private Sub UserControl_Resize()
    On Error Resume Next
    CloseBt = True
   
    cmdBack.left = 0
    cmdBack.width = UserControl.width
    cmdBack.top = 0
    cmdBack.height = UserControl.height
   
    picTitle.left = 60
    picTitle.top = 60
    picTitle.width = UserControl.width - 150
    picTitle.height = 255
   
    imgClose(0).top = 30
    imgClose(0).left = picTitle.width - 240
    imgClose(0).Visible = CloseBt
    imgClose(1).top = 30
    imgClose(1).left = picTitle.width - 240
    imgClose(1).Visible = (Not CloseBt)
   
    lstColumn.left = 60
    lstColumn.top = picTitle.height + 60
    lstColumn.width = UserControl.width - lstColumn.left - 60
    lstColumn.height = UserControl.height - lstColumn.top - 60
   
    lblTitle.top = 60
    lblTitle.left = 300
    lblTitle.width = picTitle.width - 720
End Sub
Private Sub cmdBack_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim mvDir As Integer
    If Button <> 1 Then Exit Sub
    ReleaseCapture
   
    If (X <= 60 And Y <= 60) Then
        mvDir = HTTOPLEFT
    ElseIf (cmdBack.width - X <= 60 And cmdBack.height - Y <= 60) Then
        mvDir = HTBOTTOMRIGHT
    ElseIf (X <= 60 And cmdBack.height - Y <= 60) Then
        mvDir = HTBOTTOMLEFT
    ElseIf (Y <= 60 And cmdBack.width - X <= 60) Then
        mvDir = HTTOPRIGHT
    ElseIf Y <= 60 And X > 60 And cmdBack.width - X > 60 Then
        mvDir = HTTOP
    ElseIf cmdBack.height - Y <= 60 And X > 60 And cmdBack.width - X > 60 Then
        mvDir = HTBOTTOM
    ElseIf X <= 60 And Y > 60 And cmdBack.height - Y > 60 Then
        mvDir = HTLEFT
    ElseIf cmdBack.width - X <= 60 And Y > 60 And cmdBack.height - Y > 60 Then
        mvDir = HTRIGHT
    End If
   
    SendMessage UserControl.hwnd, WM_NCLBUTTONDOWN, mvDir, 0&
    SendMessage UserControl.hwnd, WM_SIZE, 0, 0
    UserControl_Resize
    lstColumn.SetFocus
End Sub

Private Sub cmdBack_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If (X <= 60 And Y <= 60) Then
        cmdBack.MousePointer = 8
    ElseIf (cmdBack.width - X <= 60 And cmdBack.height - Y <= 60) Then
        cmdBack.MousePointer = 8
    ElseIf (X <= 60 And cmdBack.height - Y <= 60) Then
        cmdBack.MousePointer = 6
    ElseIf (Y <= 60 And cmdBack.width - X <= 60) Then
        cmdBack.MousePointer = 6
    ElseIf Y <= 60 And X > 60 And cmdBack.width - X > 60 Then
        cmdBack.MousePointer = 7
    ElseIf cmdBack.height - Y <= 60 And X > 60 And cmdBack.width - X > 60 Then
        cmdBack.MousePointer = 7
    ElseIf X <= 60 And Y > 60 And cmdBack.height - Y > 60 Then
        cmdBack.MousePointer = 9
    ElseIf cmdBack.width - X <= 60 And Y > 60 And cmdBack.height - Y > 60 Then
        cmdBack.MousePointer = 9
    End If
End Sub

c++builder如何通过鼠标拖动改变无边框窗体的大小(bsnone)

转自http://www.ccrun.com/article.asp?i=990&d=70jx1b 在C++Builder中,将Form的BorderStyle设为bsNone以后该窗体将没有边框,...
  • lz465350
  • lz465350
  • 2014年11月06日 10:02
  • 1067

C#实现窗体拖动时各个控件同比自动放缩大小

C#实现窗体拖动时各个控件同比自动放缩大小
  • bjtbjt
  • bjtbjt
  • 2014年08月20日 13:33
  • 4644

vb.net 实现无标题栏窗体拖拽功能

'较好用,没有闪动   Declare Auto Function ReleaseCapture Lib "user32.dll" Alias "ReleaseCapture" () As Bool...
  • haoduo123456789001
  • haoduo123456789001
  • 2016年04月27日 09:32
  • 1258

C# WinForm 改变无边框窗体尺寸

c# 自定义透明winform和创建
  • e421083458
  • e421083458
  • 2014年11月25日 13:42
  • 6692

如何禁止双击标题栏不改变窗口的大小和拖动标题栏窗口不移动的问题

做项目中遇到了这两个问题,主要是对非客户区的处理,现在把做法写出来, 第一个问题:只要禁止了非客户区的双击消息即可实现 (1)添加WM_NCLBUTTONDBLCLK事件处理函数 (2)...
  • monaso
  • monaso
  • 2016年08月03日 14:28
  • 643

完美拖拽+拖动改变Div的宽高+关闭按钮

/** * Created by Spades-k on 2016/8/17. */ define('h5/js/page/imageMagnification', [ 'jquery' ...
  • bbsyi
  • bbsyi
  • 2016年08月17日 17:39
  • 1096

Visual Studio 设置窗体控件,随窗体拖动变化大小

前几天做NCRE项目的时候遇到了问题,项目的目的是这样的:考生在使用的过程中,更想通过拖动主窗体放到合适的位置,来方便自己答题。那么问题来了~~~     问题重现     就原来的项目窗体来看,...
  • u013034793
  • u013034793
  • 2015年12月26日 16:55
  • 2258

C# 实现拖拉控件改变位置与大小

前言:   很多时候我们需要在运行时,动态地改变控件的位置以及大小,以获得更好的布局。比如说实际项目中的可自定义的报表、可自定义的单据等诸如此类。它们有个特点就是允许客户或者二次开发人员设计它们...
  • hugo20
  • hugo20
  • 2016年05月30日 00:30
  • 4063

Qt无边框窗口实现拖动和改变大小(修改)

Qt无边框窗口实现拖动和8个方向改变大小
  • qq_16952303
  • qq_16952303
  • 2016年07月20日 23:52
  • 2358

Winform鼠标拖动改变控件大小、移动控件位置

基本逻辑: 1、鼠标移动过程(未按下鼠标左键)进行边界检测,当移动到控件边界时(某范围内),改变显示的光标; 2、此时,若按下鼠标左键(也可是右键),记录鼠标的位置和控件的大小; 3、按下鼠标左键移动...
  • beibeisong
  • beibeisong
  • 2015年12月28日 15:26
  • 1549
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何模拟一个象窗体一样的控件(标题栏、焦点、拖动、改变大小、关闭等等)
举报原因:
原因补充:

(最多只允许输入30个字)