Userform without caption, border, close button (I'm not craz

RogerC

Well-known Member
Joined
Mar 25, 2002
Messages
536
Is it possible to make a userform that has no border or caption bar? I found some code provided by Colo that removes the X button, but I need to hide the borders and caption also. Believe it or not, I have a perfectly sane reason for wanting to do this!
vogel.gif
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi dk, thanks for the resource. I learned a lot from that example. Although it does just about everything you can think of, FormFun doesn't do the one thing I'm trying to do... remove the border around a Userform completely. (The captionless example comes close, but there is still a raised border).
I believe this can be done because in his code to change the shape of a Userform, Ivan F Moala remarks about using a 'borderless form', although in that code it appears he does not actually do it. http://www.mrexcel.com/board/viewtopic.php?topic=10197&forum=2
Any further suggestions would be appreciated.
 
Upvote 0
Hello,

Not sure about this. I tried the following code:-

Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long

Private Const SW_SHOW = 5
Private Const WS_BORDER = &H800000
Private Const WS_CAPTION = &HC00000
Private Const GWL_STYLE = (-16)


Private Sub UserForm_Activate()
    Dim lngFormHwnd As Long, lngFormStyle As Long

    
    'First, make sure the form has it's BorderStyle set to None
    Me.BorderStyle = fmBorderStyleNone
    
    'Get the Windows handle of the userform
    If Application.Version< "9.0" Then
        lngFormHwnd = FindWindow("THUNDERXFRAME", Me.Caption)
    Else
        lngFormHwnd = FindWindow("THUNDERDFRAME", Me.Caption)
    End If


    'Get the form's window style
    lngFormStyle = GetWindowLong(lngFormHwnd, GWL_STYLE)

    'Set a new style without a border
    lngFormStyle = lngFormStyle And Not WS_BORDER

    'Set the new style i.e. without border
    SetWindowLong lngFormHwnd, GWL_STYLE, lngFormStyle

    'Refresh
    DrawMenuBar lngFormHwnd

    'This line doesn't appear to do anything
    'ShowWindow lngFormHwnd, SW_SHOW

End Sub

Even though I set the Window style to NOT WS_BORDER this removes the caption but has no effect on the border. Maybe Ivan will have an idea....?

_________________<font face="Impact">Hope this helps,
Dan</font>
This message was edited by dk on 2002-12-30 20:12
 
Upvote 0
WinXp / Excel2000
I got this to work for me ??

<PRE><FONT color=blue>Option Explicit</FONT>



Private Declare <FONT color=blue>Function </FONT>FindWindow Lib "user32" _

Alias "FindWindowA" ( _

<FONT color=blue>ByVal</FONT> lpClassName <FONT color=blue>As</FONT><FONT color=blue> String</FONT>, _

<FONT color=blue>ByVal</FONT> lpWindowName <FONT color=blue>As</FONT><FONT color=blue> String</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



Private Declare <FONT color=blue>Function </FONT>SetWindowLong Lib "user32" _

Alias "SetWindowLongA" ( _

<FONT color=blue>ByVal</FONT> hwnd <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> nIndex <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> dwNewLong <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



Private Declare <FONT color=blue>Function </FONT>GetWindowLong Lib "user32" _

Alias "GetWindowLongA" ( _

<FONT color=blue>ByVal</FONT> hwnd <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> nIndex <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



Private Declare <FONT color=blue>Function </FONT>SetWindowPos Lib "user32" ( _

<FONT color=blue>ByVal</FONT> hwnd <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> hWndInsertAfter <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> x <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> y <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> cx <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> cy <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>, _

<FONT color=blue>ByVal</FONT> wFlags <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>) <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>



Private <FONT color=blue>Const </FONT>GWL_STYLE = (-16)

Private <FONT color=blue>Const </FONT>WS_CAPTION = &HC00000 <FONT color=#ff6600>'// WS_BORDER Or WS_DLGFRAME
</FONT>
Private <FONT color=blue>Const </FONT>WS_BORDER = &H800000





Private Enum ESetWindowPosStyles

SWP_SHOWWINDOW = &H40

SWP_HIDEWINDOW = &H80

SWP_FRAMECHANGED = &H20 <FONT color=#ff6600>'// The frame changed: send WM_NCCALCSIZE
</FONT>
SWP_NOACTIVATE = &H10

SWP_NOCOPYBITS = &H100

SWP_NOMOVE = &H2

SWP_NOOWNERZORDER = &H200 <FONT color=#ff6600>'// Don't do owner Z ordering
</FONT>
SWP_NOREDRAW = &H8

SWP_NOREPOSITION = SWP_NOOWNERZORDER

SWP_NOSIZE = &H1

SWP_NOZORDER = &H4

SWP_DRAWFRAME = SWP_FRAMECHANGED

HWND_NOTOPMOST = -2

End Enum



Private<FONT color=blue> Type</FONT> RECT

Left <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

Top <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

Right <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

Bottom <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

End<FONT color=blue> Type</FONT>



<FONT color=blue>Dim </FONT>FrmWndh <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

<FONT color=blue>Dim </FONT>lStyle <FONT color=blue>As</FONT><FONT color=blue> Long</FONT>

<FONT color=blue>Dim </FONT>tR <FONT color=blue>As</FONT> RECT



<FONT color=blue>Private <FONT color=blue>Sub </FONT></FONT>UserForm_Activate()

<FONT color=#ff6600>'// NB: Must be in the Activate Event!
</FONT>


<FONT color=#ff6600>'// Get Forms window handle
</FONT>
FrmWndh = FindWindow(vbNullString, Me.Caption)



<FONT color=#ff6600>'// Modify whether title bar will be visible:
</FONT>
lStyle = GetWindowLong(FrmWndh, GWL_STYLE)

<FONT color=#ff6600>'// Now mask the Winstyle caption
</FONT>
lStyle = lStyle And Not WS_CAPTION



SetWindowLong FrmWndh, GWL_STYLE, lStyle



<FONT color=#ff6600>'// Ensure the style takes and make the window the
</FONT>
<FONT color=#ff6600>'// same size, regardless that the title bar
</FONT>
<FONT color=#ff6600>'// is now a different size:
</FONT>
SetWindowPos FrmWndh, _

0, _

tR.Left, _

tR.Top, _

tR.Right - tR.Left, _

tR.Bottom - tR.Top, _

SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED Or WS_BORDER



<FONT color=#ff6600>'// Update Form
</FONT>
Me.Repaint

<FONT color=blue>End Sub</FONT>



<FONT color=blue>Private <FONT color=blue>Sub </FONT></FONT>UserForm_DblClick(<FONT color=blue>ByVal</FONT> Cancel <FONT color=blue>As</FONT> MSForms.Return<FONT color=blue>Boolean</FONT>)

<FONT color=#ff6600>'// Leave this here as your backdoor, as you have NO CLOSE BUTTON
</FONT>
<FONT color=#ff6600>'// otherwise put in a close button
</FONT>
Unload UserForm1

<FONT color=blue>End Sub</FONT>




</PRE>
 
Upvote 0
Thank you dk. Your code gets very close to the effect I need and I do appeciate your help. I think I have found something that may help find a solution. I will explain in my post to Ivan next.
 
Upvote 0
I'm using WinXP/Excel2000 also.

Thank you Ivan for looking into this... I appreciate it. Unfortunately, when I create a new form with your code I still get a small border around the form (an effect similar to 'raised', with a white edge on top and left, grey edge on right and bottom).
I think I might have found something that may help... while studying Steven Bullen's 'Formfun.xls' file at http://www.bmsltd.co.uk/Excel/Default.htm , I made two very small edits to his code and got the effect I need! Here is what I did;

1) In the 'Option Explicit' area of the code, I inserted an apostrophy in front of "Private Const WS_THICKFRAME As Long = &H40000 'Style to add a sizable frame"

2) In the "Private Sub SetFormStyle()", I inserted an apostrophy in front of the line "If mbSizeable Then iStyle = iStyle Or WS_THICKFRAME Else iStyle = iStyle And Not WS_THICKFRAME"

3) I activated the Userform, and then removed the checkmark from the 'Caption' checkbox Steven has built into the form.

This changed the form to the exact effect I need! Unfortunately, the code in this file is very complex. I've studied it but can't understand why these edits worked or how to recreate this effect in a Userform without the checkbox examples included in the example file.
Can you tell me what to look at from here?
This message was edited by RogerC on 2002-12-31 05:47
 
Upvote 0
Thank you Ivan. I tried adding "Me.SpecialEffect = fmSpecialEffectFlat" to the UserForm_Activate subs of both yours and dk's code. It gave me the same result as your original codes (a small white border edge on top and left, grey border edge on right and bottom). This is the same border I would expect when setting a form's special effect to 'flat' normally.

When making the two small edits to Steven's code that I mentioned above, the effect I get is completely borderless... no border at the edges of the form can be seen at all.

To further explain.... if the user is in a worksheet that has a white background (and also has gridlines turned off), they then activate a completely 'borderless and captionless' form (which also has a white background), they would not even know a Userform has opened. If it had no controls on it, they would not be able to see it at all.

This 'borderless' technique works perfectly when I make the two small apostrophy edits I mentioned above to Steven Bullen's 'Formfun.xls' file... the Userform opens but it's edges cannot be distinguished from the worksheet it opens on (provided the form's background and the worksheet's background are the same color).

Unfortunately, I need to remove the functionality of the controls (checkboxes) Steven has in his file and replace them with some basic controls I need on my Userform. His example is a complex form and I'm having trouble understanding how the code works... Any tips you can provide are greatly appreicated.
This message was edited by RogerC on 2002-12-31 07:17
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top