Moving a captionless userform

UPDATE:
Check Out Andy Popes code, a much better method of moving a userform with out a caption:

Here is some code to move the form around without the need for API coding.
Create a couple of private variables in the userform to remember the position when the left mouse button is pressed.

VBA:
  1. Private m_sngX as single
  2. Private m_sngY as singlePrivate Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  3.  
  4. If Button = 1 Then
  5. m_sngX = X
  6. m_sngY = Y
  7. End If
  8. End Sub
  9. Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  10.  
  11. If Button = 1 Then
  12.  
  13. Me.Left = X + Me.Left - m_sngX
  14. Me.Top = Y + Me.Top - m_sngY
  15.  
  16. End If
  17. End Sub

Orginal post

Yesterday I was playing around with Jan Karel Pieterse Watch Other Cell tool. I wanted to make the user form captionless and transparent. That's easy enough to do but a problem arose. With the caption removed the form could not be moved.
One work around was to use the forms mouse move event and an API Call to hook the cursor location. Another API call is made to reposition the form. The Down and Up events are used to "turn on" weather or not we what to move the form. For the user it is much the same as a normal move process.

The API routines are included in the example file attached. If your thinking, "why not just use mouse X, Y and top and left?", it's because the API's returns cursor position and will place the form with in your systems co-ordinate structure, not just excels. The results are more reliable.
There is one problem still outstanding with this method. When you first click the form to reposition it the form "jumps" to the places where you clicked the mouse. If it's near the top left corner, then the result is hardly noticeable, it's a different story down in the right hand corner. I think code could be written to correct for this but maybe you could simply put in an icon near the top left to allow the move?

Enjoy!
Moveable Captionless Userform

Related posts:

  1. Using the immediate window from within the Excel desktop
  2. Captionless worksheets
  3. Get RGB value of control at runtime

Comments

  1. ross says:

    Thanks Andy, that sure is a better way to do it!!!!!!!!!!!!!!

  2. [...] Why on earth would you want to do it I hear you ask! That's a very good question! When I started this I thought it was really not very useful at all. I'm still thinking that, but maybe there might be a few practical uses for it. I'm thinking tool tips and popup type things. I have covered using captionless user forms (and worksheets!), and even popup forms previously. I was never happy with the way the captionless forms looked in VBA, it's ok in VB6 but in VBA they has a wafty boarder. [...]

  3. [...] few practical uses for it. I’m thinking tool tips and popup type things. I have covered using captionless user forms (and worksheets!), and even popup forms previously. I was never happy with the way the captionless [...]

Submit a Comment