关于控件left & Top的问题

watt 2002-07-08 03:52:06
用VB做一个自定义控件,然后在另一个程序中使用此控件。
程序中有如下代码
Private Sub UserControl1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage UserControl1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub

控件能正常移动,但是控件的left & Top的值不变,但是如果用此代码移动VB
自带的标准控件,它的left & top 值都发生变化,谁知道这是为什么?
...全文
70 8 点赞 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
banweihui 2002-07-11
我是来学习的~
  • 打赏
  • 举报
回复
watt 2002-07-11
如果有更好的办法,继续加分!
  • 打赏
  • 举报
回复
watt 2002-07-11
难道没有别的方法吗?我认为应该有某个消息通知控件其位置发生了变化,让控件自动刷新,如果不是这样的话,为什么VB自带的控件移动好用。而我们自定义控件不行,现在我在尝试用setwindowpos函数看可不可以。楼上的方法也是一个方法,我试一试。有没有最优的解决方法?
  • 打赏
  • 举报
回复
griefforyou 2002-07-10
弄了半天也只能这样了,用自已的属性ULeft,UTop来读控件位置,用Left,Top设置控件位置
Option Explicit

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) 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 Declare Function ReleaseCapture Lib "user32" () As Long

Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type


Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

Public ULeft As Single
Public UTop As Single

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage UserControl.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub


Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Uhwnd As POINTAPI
Dim FrmHwnd As POINTAPI

ClientToScreen UserControl.hwnd, Uhwnd
ClientToScreen UserControl.Parent.hwnd, FrmHwnd

ULeft = (Uhwnd.X - FrmHwnd.X) * Screen.TwipsPerPixelX
UTop = (Uhwnd.Y - FrmHwnd.Y) * Screen.TwipsPerPixelY

End Sub
  • 打赏
  • 举报
回复
griefforyou 2002-07-10
弄了半天我只能做到这样了,你抛弃Left,Top属性自已创建ULeft,和UTop属性来保存控件的位置,不过还只能用Left和Top去设置控件的位置了!

Option Explicit

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) 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 Declare Function ReleaseCapture Lib "user32" () As Long

Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Type POINTAPI
X As Long
Y As Long
End Type


Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2

Public ULeft As Single
Public UTop As Single

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage UserControl.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub


Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Uhwnd As POINTAPI
Dim FrmHwnd As POINTAPI

ClientToScreen UserControl.hwnd, Uhwnd
ClientToScreen UserControl.Parent.hwnd, FrmHwnd

ULeft = (Uhwnd.X - FrmHwnd.X) * Screen.TwipsPerPixelX
UTop = (Uhwnd.Y - FrmHwnd.Y) * Screen.TwipsPerPixelY

End Sub
  • 打赏
  • 举报
回复
watt 2002-07-10
Who can help me?
  • 打赏
  • 举报
回复
yufenfeila 2002-07-08
自定义控件的属性应该在编写自定义控件的时候添加
  • 打赏
  • 举报
回复
griefforyou 2002-07-08
我试过了,是不改变

不过你可以换个方法,自已添加UserControl的Left和Top属性
我想这样应该没问题
  • 打赏
  • 举报
回复
相关推荐
发帖
控件
加入

1430

社区成员

VB 控件
申请成为版主
帖子事件
创建了帖子
2002-07-08 03:52
社区公告
暂无公告