Date: 02-16-2022
Return to Index
created by gbSnippets
'The F2 key is typically used to start editing text, such as changing the
'filename in Explorer. In a PowerBASIC application, setting F2 as an
'accelerator key to a menu item is a common approach to using the key.
'Primary Code:
Sub BuildAcceleratorTable
Local c As Long, ac() As ACCELAPI, hAccelerator As Dword ' for keyboard accelator table values
Dim ac(0)
ac(c).fvirt = %FVIRTKEY : ac(c).key = %VK_F2 : ac(c).cmd = %ID_Menu : Incr c
Accel Attach hDlg, AC() To hAccelerator
End Sub
'Compilable Example: (Jose Includes)
'This shows how to define F2 as an accelerator key that runs a menu.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
%ID_SayHello = 100 : %IDC_Button = 200
Global hDlg As Dword, hMenu As Dword, hMenuOptions As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_Button,"Push", 50,10,100,20
AddMenu
BuildAcceleratorTable
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = %ID_SayHello Then SayHello
End Function
Sub AddMenu()
Menu New Bar To hMenu
Menu New Popup To hMenuOptions
Menu Add Popup, hMenu, "&Option", hMenuOptions, %MF_Enabled
Menu Add String, hMenuOptions, "&Say Hello" + $Tab + "F2", %ID_SayHello, %MF_Enabled
Menu Attach hMenu, hDlg
End Sub
Sub BuildAcceleratorTable
Local c As Long, ac() As ACCELAPI, hAccelerator As Dword ' for keyboard accelator table values
Dim ac(0)
ac(c).fvirt = %FVIRTKEY : ac(c).key = %VK_F2 : ac(c).cmd = %ID_SayHello : Incr c
Accel Attach hDlg, AC() To hAccelerator
End Sub
Sub SayHello
? "Hello"
End Sub
'gbs_00193
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm