Tutorials > VSPrinter Examples > Custom NavBar Button |
This example shows how you can add your own custom buttons the VSPrinter's built-in navigation bar. The custom buttons are created using standard VB controls and "blend in" with the built-in buttons.
To create the custom buttons, start by adding the following controls to a new VB form:
VSPrinter control (name it vp)
Two Image controls (name them img(0) and img(1))
Two PictureBox controls (name them picBtn(0) and picBtn(1))
Set the Picture property on the Image controls to icons of your choice. The buttons will look best for icons about 10 pixels tall by 14 pixels wide, but any image will work.
Then add the following code to the form:
Example Title |
Copy Code
|
---|---|
Private Sub Form_Load() ' align VSPrinter control to the top of the form vp.Align = vbAlignTop ' constants for custom buttons Const pixLeft = 180, pixTop = 2 Const pixWid = 20, pixHei = 14, pixSpace = 4 ' get conversion constants Dim tppX!, tppY! tppX = Screen.TwipsPerPixelX tppY = Screen.TwipsPerPixelY ' configure button controls Dim i%, x! For i = 0 To 1 With picBtn(i) ' initialize button appearance .AutoRedraw = True .BorderStyle = 0 .Picture = img(i).Picture ' move button into place x = (pixLeft + (pixWid + pixSpace) * i) * tppX .Move vp.Left + x, vp.Top + pixTop * tppY, pixWid * tppX, pixHei * tppY .ZOrder ' frame button FrameBtn picBtn(i), False End With Next End Sub Private Sub Form_Resize() On Error Resume Next vp.Height = ScaleHeight End Sub |
This code initializes the appearance of each button, moves it into place, then draws a frame around the button. (The code assumes that the navigation bar is drawn on the top of the control). The following FrameBtn routine can draw two types of frame: pushed and released.
Example Title |
Copy Code
|
---|---|
Private Sub FrameBtn(pic As PictureBox, bPushed%) ' get conversion constants Dim tppX!, tppY! tppX = Screen.TwipsPerPixelX tppY = Screen.TwipsPerPixelY ' draw button frame With pic If bPushed Then .ForeColor = &H80000010 Else .ForeColor = &H80000014 pic.Line (0, 0)-(pic.Width, pic.Height), , B If bPushed Then .ForeColor = &H80000014 Else .ForeColor = &H80000010 pic.Line (-100, -100)-(pic.Width - tppX, pic.Height - tppY), , B End With End Sub |
At this point, you can run the project and see that the custom buttons are visible and look like the standard built-in buttons.
To make the buttons work, we need some mouse-handling code. We'll start by making the buttons respond to double-clicks. The first button will zoom in and the second will zoom out:
Example Title |
Copy Code
|
---|---|
Private Sub picBtn_DblClick(Index As Integer) Select Case Index Case 0 ' zoom in vp.Zoom = vp.Zoom + vp.ZoomStep Case 1 ' zoom out vp.Zoom = vp.Zoom - vp.ZoomStep End Select End Sub |
Finally, add the following code to make the buttons respond to the MouseDown, MouseMove, and MouseUp events:
Example Title |
Copy Code
|
---|---|
Private Sub picBtn_MouseDown(Index As Integer, Button As Integer, _ Shift As Integer, X As Single, Y As Single) If Button <> 1 Then Exit Sub picBtn(Index).Tag = "*" ' ** PUSHED FrameBtn picBtn(Index), True End Sub Private Sub picBtn_MouseMove(Index As Integer, Button As Integer, _ Shift As Integer, X As Single, Y As Single) If (Button And 1) = 0 Then Exit Sub With picBtn(Index) If X < 0 Or Y < 0 Or X > .Left Or Y > .Height Then picBtn(Index).Tag = "" ' ** RELEASED FrameBtn picBtn(Index), False Else picBtn(Index).Tag = "*" ' ** PUSHED FrameBtn picBtn(Index), True End If End With End Sub Private Sub picBtn_MouseUp(Index As Integer, Button As Integer, _ Shift As Integer, X As Single, Y As Single) If Button <> 1 Then Exit Sub If picBtn(Index).Tag = "*" Then picBtn_DblClick Index ' ** HANDLE CLICK End If picBtn(Index).Tag = "" FrameBtn picBtn(Index), False End Sub |
The MouseDown routine makes sure the left button was pushed, sets the picBtn(Index).Tag property to a flag that indicates the button is pushed, and draws the pushed frame around the button. The MouseMove routine pushes or releases the button depending on whether the cursor is over the button. The MouseUp routine checks whether the button in the pushed state when the mouse was released. If so, it calls the picBtn_DblClick routine to handle the click (as a double-click would).
If you run the project again, you will see that the custom buttons behave just like the built-in ones.