Tips & Tricks - How to make fancy shmancy forms (Part 1)

Posted by: SilentRage

Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 05:29 PM

I just read in one of Gizmo's mass emails that moderators are actually supposed to ENCOURAGE conversation in their forum, so huh, guess what, I guess I better get off my lazy mod *** and start thinking of something to say to a group of mostly non-programmers.

Wanna create cool looking windows but don't know how because VB tends to by default give you a plain jane window with a title bar and all windowy features? Well looky here, I'm going to talk about making fancy shmancy windows in VB.

first of all, we need to start with a clean slate. To get a plain square window with no features whatsoever, create a form and set the following properties:

BorderStyle = 0
Caption = ""
ControlBox = False
ShowInTaskbar = True
(ShowInTaskbar is automatically set to false when you set the ControlBox to false - you should set it back to true)

There we go! Now it's borderless and has no title bar whatsoever. Now we need to make it cool looking. I'm going to change the background to black (BackColor = 0) and create a label to be my new title bar. I change the label's background and foreground colors and the font type/size and position it and size it where I want it. I'm also going to use the line control to draw a few lines on the edges of my form to create the effect of a border (for the borders, you may want to consider using the shape controls). Feel free to add any pictures or a background or whatever fits your style.

Now all that is pretty obvious stuff. Now for the REAL tip & trick. You want to have your user move the form around right? I chose to have it so that the custom title bar is draggable, but you can make it so that they can drag any part of the form. Here's the best way to do it:

Declare these functions and constants:

Private Declare Sub ReleaseCapture Lib "user32" ()
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
Const WM_NCLBUTTONDOWN = &HA1
Const HTCAPTION = 2

Now I choose any control with a MouseMove event to act as my drag-point. I chose my titlebar label, you may choose your form:

Private Sub lblHeader_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'insert code
End Sub

Here's the code:

If Button = 1 Then
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End If

Button = 1 will make sure the left button is pressed. ReleaseCapture will release the mouse's capture, and SendMessage will send a message to the window to let it know it needs to be dragged. If you want to be able to drag with either button, say "If Button > 0 Then". Also, I change my cursor to show a user that you can drag (lblHeader.MousePointer = 15)

Example: Pre-release DNS Lookup v2.0

Well that does it! You're well on your way to making fancy shmancy forms. Next time I will tell you how to shape your forms into circles or rounded corners or any shape you want!
Posted by: olosoft

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 06:25 PM

also, heres a simple way of creating custom graphical buttons.

ok, first of all you have to create the images. you need three images for each button: a normal state, hover state, and down state.

once youve create the images open up your project in vb. i always create one form that just holds all the images in one spot for organizations, say, call it frmImages. open that form up and create an Image. for our example well call it imgOK. set that Picture property to your buttons Normal state. now, copy and paste that Image, when it asks you if you wanna create a control array, say yes. the new image should be called imgOK(1). now set the picture to the Hover state. and copy, paste, and set imgOK(2) to the Down state.

aight, go back to your form where you want the button. create an image, well call it cmdOK. set the default picture to the Norm state. now add this code:

Private Sub cmdOK_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
cmdOK = frmImages.imgOK(2)
End Sub

Private Sub cmd_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not Button = 1 Then
cmdOK = frmImages.imgOK(1)
End If
End Sub

and in the Form_MouseMove add cmdOK = frmImages.imgOK(0)

oh, and you might also wanna add the actualy button's code to the cmdOK_Click event.

example: http://olosoft.topcities.com/suite/Calculator.exe 24kb

example code: http://olosoft.topcities.com/suite/Calculator.zip 22kb
Posted by: olosoft

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 06:29 PM

err, yea, those links dont work, copy them into like Download Accelertor plus, or go to olosoft.topcities.com and then type those links in... god**** free host
Posted by: Gremelin

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 07:39 PM

good boy ::rubs yoru head::
Posted by: ninjaneo

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 07:43 PM

Heh, I guess that means me to hey SR?...

Umm... I could show you guys how to make rounf forms.. If you want to know go ahead ans ask me. :rolleyes:
Posted by: olosoft

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/13/02 09:01 PM

sure, tell us how to make rounf forms
/me isnt a big fan of that whole BitBlt thing tho...
Posted by: GeneX

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/14/02 09:37 AM

Sample to make round form (put this behind form):
---------------------------------------------
Private Declare Function CreateEllipticRgn Lib "gdi32" _
(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
ByVal Y2 As Long) As Long

Private Declare Function SetWindowRgn Lib "user32" _
(ByVal hWnd As Long, ByVal hRgn As Long, _
ByVal bRedraw As Boolean) As Long

Private Sub Form_Load()
SetWindowRgn hWnd, CreateEllipticRgn(0, 0, Form1.Width * 15, Form1.Heigth * 15), True
End Sub
Posted by: ninjaneo

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 03/15/02 06:20 PM

Well, I guess that would work my way was a little bit different and you could control roundness of corners and hiegth / width of forum... :rolleyes:
Posted by: nomel

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 04/08/03 09:06 AM

Fancy Buttons!

You can make nice buttons with labels.
Just set the border option to single and do the normal name_click() function for it. Makes nice little flat buttons.

I like making everything Flat instead of 3D too...
Posted by: SilentRage

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 04/08/03 01:40 PM

I have fixed the link in the first post to the example form. of course, now DNS Lookup v2 is no longer pre release.
Posted by: BackSlash

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 04/08/03 08:15 PM

i think you could make the form draggable without api if you did the following....
Code:
Dim lastx, lasty

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
lastx = X
lasty = Y
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Form1.Left = Form1.Left + (X - lastx)
Form1.Top = Form1.Top + (Y - lasty)
End If
End Sub  
you could put that code for a label instead of the form to drag the form with the title bar.
Posted by: SilentRage

Re: Tips & Tricks - How to make fancy shmancy forms (Part 1) - 04/08/03 10:37 PM

yes, but that requires more code than the API technique. And besides, why reinvent the wheel? Just use technology already in place. It's more efficient.