I would also like to keep this going, So please add your own snippets in your post.
Only "rule" is it must be UI related (which is broader then most may think)
A Collection Of UI Code Snippets to enhance your users interface.
Check Mouse Click
Code:
Dim clickd As String
clickd = e.Button()
Select Case clickd
Case 2097152 '(right mouse Value)
MsgBox("You Right Clicked")
Case 1048576 '(left Mouse Value)
MsgBox("You Left Clicked")
End Select
MsgBox(clicktext)
Disable (X) On form
(I have a full tutorial in my sig, but here is the code snippet, I wanted to include it in this list because it comes in handy)
Code:
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Long) As IntPtr
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As IntPtr) As Integer
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000
Private Const MF_DISABLED = &H2
Public Sub DisableCloseButton(ByVal hwnd As IntPtr)
Dim hMenu As IntPtr
Dim menuItemCount As Integer
hMenu = GetSystemMenu(hwnd, False)
menuItemCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_DISABLED Or MF_BYPOSITION)
Call RemoveMenu(hMenu, menuItemCount - 2, _
MF_DISABLED Or MF_BYPOSITION)
Call DrawMenuBar(hwnd)
End Sub
Add this to the event that will trigger the disable
Ex: Form Load, Setting , Menu Item , etc...
Code:
DisableCloseButton(Me.Handle)
Fade Out
This goes in a Non-Class are
Code:
Friend WithEvents fader As System.Windows.Forms.Timer
Call this to fade out
Code:
Public Sub fade()
Me.fader = New System.Windows.Forms.Timer
Me.fader.Enabled = True
Me.fader.Interval = 30
End Sub
This is the actual fade
Code:
Private Sub fader_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fader.Tick
Me.Opacity -= 0.03 ('3% opacity, you can use anything youlike)
' this checks the opacity, it it is less then 5 then..end
' alot of people use 0, but I am doing it by 3% not by 1% , so check and ' see if it is less then 5 as opposed to = 0, however you can use any ' ' method you perfer, I just perfer this way (looks cleaner to me)
If Me.Opacity < 2 Then
End
End If
End Sub
Make form transparent while dragging
(this is and example of how checking the state of a mouse click can come in handy[above])
Code:
Private Const WM_NCLBUTTONDOWN As Long = &HA1
Private Const WM_NCLBUTTONUP As Long = &HA0
Private Const WM_MOVING As Long = &H216
Private Const WM_SIZE As Long = &H5
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
Static LButtonDown As Boolean
(checks left click)
If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
'(as long as left button is down LButtonDown returns true)
LButtonDown = True
ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then
' (As long as left button is up, LButtonDown returns false)
LButtonDown = False
End If
If LButtonDown Then
If CLng(m.Msg) = WM_MOVING Then
'Changes form opacity to 70% if the form is being dragged
' You can change the 0.7 to anything 0.1 = 10% 0.2 = 20 % and so on
If Me.Opacity <> 0.9 Then Me.Opacity = 0.5
ElseIf CLng(m.Msg) = WM_SIZE Then
'Set the forms opacity to 60% if user is resizing the window
If Me.Opacity <> 0.6 Then Me.Opacity = 0.6
End If
ElseIf Not LButtonDown Then
If Me.Opacity <> 1.0 Then Me.Opacity = 1.0
End If
MyBase.DefWndProc(m)
End Sub
Windows Shadow - (Tested in Xp)
Code:
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Const CS_DROPSHADOW = &H20000
Dim CrPa As CreateParams = MyBase.CreateParams
CrPa.ClassStyle = CrPa.ClassStyle Or CS_DROPSHADOW
Return CrPa
End Get
End Property
Yes, I know this is a bunch of basic code (as the title implies......) it
is a collection of some snippets, I have created this thread to spark some ideas for other users here at mpgh.net to create tuts, and their own projects,
take anything you want from here and use it, all I ask if you do use anything from here, just be sure to post it in mpgh.net VB section so we all can see how you used it.