Tech Note 38: Adding Controls to the NS Basic Toolbar

June 27, 2002

© NSB Corporation. All rights reserved.

Contributed by Alex Yakhnin, Software Developer, Morganville, NJ.

This Tech Note applies to Pocket PC units. It is not applicable to Pocket PC 2002.

Since the introduction of the Mark Gamber's Win32 API object, unlimited possibilities have been opened for NS Basic developers to expand the boundaries of this tool. Many of us would like to place some controls on the Toolbar, freeing up some more display space for our applications. As a first step, let's place a CommandButton on a Toolbar:

Sample 1
sample sample

(Before running this sample, please save it under the name "TlbButton.nsb")

Option Explicit

'Declare variables and objects
AddObject "MGCEWin32.API","API"
AddObject "CommandButton","cmdTool", 50, 3, 20, 20
cmdTool.BackColor = 12632256 ' Gray
cmdTool.Caption="OK"

Dim AppWnd
Dim tlbWnd

'Get Application Window Handle
AppWnd = API.FindWindow("TlbButton.nsb")
'Get ToolBar Window Handle
tlbWnd = API.ChildWindowFromPoint(AppWnd, 0, 0)

'Assign to our Button a new Parent
API.WindowParent(cmdTool.hWnd) = tlbWnd

Sub cmdTool_Click
  MsgBox "Tool Button Clicked"
End Sub

Now, lets try to add an Icon button to a Toolbar. For this purposes we can use PictureBox control with loaded "bmp" file in it.

"But PictureBox doesn't have a window handle property!" you say.

Absolutely correct, and the knowledge of the Windows API's will come to our help again. The trick is in knowing that the main application window that is created by NS Basic has 2 children: ToolBar and Dialog and all controls that are added to the project become, actually, the children of the Dialog window:

Sample 2
sample sample

(Before running this sample, please save it under the name "TlbIcon.nsb")

Option Explicit

'Declare variables and objects
AddObject "MGCEWin32.API","API"
AddObject "PictureBox","picTool", 50, 3, 21, 21
' Load picture file into control ( Make sure that the file exists in your system)
picTool.DrawPicture "\Windows\property.2bp",0,0
picTool.BorderStyle = 0

Dim AppWnd
Dim dialogWnd
Dim pictWnd

'Get Application Window Handle
AppWnd = API.FindWindow("TlbIcon.nsb")
'Get ToolBar Window Handle
tlbWnd = API.ChildWindowFromPoint(AppWnd, 0, 0)
' Get Dialog Window handle
dialogWnd = API.ChildWindowFromPoint(AppWnd, 100, 100)
'Get PictureBox handle using dialogWnd as a Parent and PictureBox
coordinates
pictWnd = API.ChildWindowFromPoint(dialogWnd, 50, 3)

'Assign to our Icon a new Parent
API.WindowParent(pictWnd) = tlbWnd

Sub picTool_Click
  MsgBox "Tool Icon Clicked"
End Sub

' Lets Change BorderStyle to show that it was clicked
Sub picTool_MouseDown(b, s, x, y)
   picTool.BorderStyle = 1
End Sub

' Restore BorderStyle
Sub picTool_MouseUp(b, s, x, y)
   picTool.BorderStyle = 0
End Sub

Alex Yakhnin, Software Developer, Morganville, NJ