I'll put this in the Source Code forum, but wanted to see what comments on changes/issues might be appropriate before doing so.
I wanted to custom handle the TAB key when it was pressed inside a RichEdit control. Subclassing will let me see the key (via WM_Char) but I had to use the WM_GetDlgCode message to send the DLGC_WantAllKeys value to the RichEdit window procedure to prevent TAB from moving focus to the next control.
I looked through the forum messages and never quite found a complete example. It may have been there but all I found were general suggestions on using subclassing or intercepting the IsDialogMessage in an SDK solution.
So I put together this working example. Comments are welcome.
I tried the DLGC_WANTTAB value, but that seemed to stop all other keys from being recognized. I thought it might simply modify just the TAB behavior, but apparently not.
I wanted to custom handle the TAB key when it was pressed inside a RichEdit control. Subclassing will let me see the key (via WM_Char) but I had to use the WM_GetDlgCode message to send the DLGC_WantAllKeys value to the RichEdit window procedure to prevent TAB from moving focus to the next control.
I looked through the forum messages and never quite found a complete example. It may have been there but all I found were general suggestions on using subclassing or intercepting the IsDialogMessage in an SDK solution.
So I put together this working example. Comments are welcome.
Code:
#Compile Exe #Dim All #Include "RichEdit.inc" #Include "CommCtrl.inc" %ID_RichEdit = 500 Global hDlg As Dword, hRichEdit As Dword, OldProc& Function PBMain() As Long Local style&, buf$ buf$ = "This is sample" + $CrLf + "text for the" + $CrLf + "edit control." style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _ Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop Dialog New Pixels, 0, "Test Code",300,300,200,155, %WS_OverlappedWindow To hDlg Control Add Button, hDlg, 100,"Paste", 30,10,140,20 LoadLibrary("riched32.dll") : InitCommonControls Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,40,160,100, style& SendMessage hRichEdit, %EM_SETEVENTMASK, 0, %ENM_SELCHANGE Or %ENM_CHANGE Or %ENM_LINK Or %ENM_KeyEvents Control Handle hDlg, %ID_RichEdit To hRichEdit Dialog Show Modal hDlg Call DlgProc End Function CallBack Function DlgProc() As Long Local temp$ Select Case CB.Msg Case %WM_InitDialog OldProc& = SetWindowLong(GetDlgItem(hDlg, %ID_RichEdit), %GWL_WndProc, CodePTR(NewProc)) 'subclass Case %WM_Destroy SetWindowLong hRichEdit, %GWL_WNDPROC, OldProc& 'un-subclass Case %WM_Command If CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then MsgBox "No action asssigned." End If End Select End Function Function NewProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case Msg Case %WM_GETDLGCODE Function = %DLGC_WANTALLKEYS 'establish control by the RichEdit Exit Function Case %WM_Char 'responds when the TAB key is pressed Select Case wParam Case %VK_Tab 'detects TAB key - take custom action here Static iTabCount& : Local temp$ : Incr iTabCount Control Get Text hDlg, %ID_RichEdit TO temp$ Control Set Text hDlg, %ID_RichEdit, temp$ + Str$(iTabCount) Dialog Set Text hDlg, "Tab pressed" 'just to show the TAB key was seen Function = 0 : Exit Function 'do not further process the TAB key End Select End Select Function = CallWindowProc(OldProc&, hWnd, Msg, wParam, lParam) End Function
Comment