Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge the code for the two dialog functions
#1
Hi,
I created two dialog functions, function A and function B
The code of these two functions differs only in two places:

1.Title bar text (Line 1)
2.The value of the _s variable in subfunction right_menu (Line 56)

I want to merge the same part of the two codes into one function, so that it will be convenient to modify it

Thanks in advance for any advice and help
david


Function A
 
Code:
Copy      Help
str t="A"

str dd=
F
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "{t}_Dialog" "4"
;3 Edit 0x54030080 0x200 68 52 96 13 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""

str controls = "3"
str e3
if(!ShowDialog(dd &sub.DlgProc &controls)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,SetTimer hDlg 1 100 0 ;;Edit
,case WM_TIMER
,sel wParam
,,case 1 ;;Edit
,,int ec=id(3 hDlg); if(ec=0) ret
,,SetWindowSubclass(ec &sub.WndProc_Subclass_Edit 3 0)
,,KillTimer hDlg wParam
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

#sub WndProc_Subclass_Edit ;;Edit
function# hwnd message wParam lParam uIdSubclass dwRefData

;OutWinMsg message wParam lParam ;;uncomment to see received messages
sel message
,case WM_CONTEXTMENU
,sub.right_menu hwnd
,ret

int R=DefSubclassProc(hwnd message wParam lParam)

sel message
,case WM_NCDESTROY
,RemoveWindowSubclass(hwnd &sub.WndProc_Subclass_Edit uIdSubclass)
ret R

#sub right_menu
function hwnd
str s=
;1 1    A test1
;2 2    A test2
;3 3    A test3

MenuPopup m.AddItems(s)
int i=m.Show
str s1 s2
if(i and m.GetItemText(i s1 s2))
,EditReplaceSel hwnd 0 s2 1|2|4

Function B
Code:
Copy      Help
str t="B"

str dd=
F
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "{t}_Dialog" "4"
;3 Edit 0x54030080 0x200 68 52 96 13 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""

str controls = "3"
str e3
if(!ShowDialog(dd &sub.DlgProc &controls)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,SetTimer hDlg 1 100 0 ;;Edit
,case WM_TIMER
,sel wParam
,,case 1 ;;Edit
,,int ec=id(3 hDlg); if(ec=0) ret
,,SetWindowSubclass(ec &sub.WndProc_Subclass_Edit 3 0)
,,KillTimer hDlg wParam
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

#sub WndProc_Subclass_Edit ;;Edit
function# hwnd message wParam lParam uIdSubclass dwRefData

;OutWinMsg message wParam lParam ;;uncomment to see received messages
sel message
,case WM_CONTEXTMENU
,sub.right_menu hwnd
,ret

int R=DefSubclassProc(hwnd message wParam lParam)

sel message
,case WM_NCDESTROY
,RemoveWindowSubclass(hwnd &sub.WndProc_Subclass_Edit uIdSubclass)
ret R

#sub right_menu
function hwnd
str s=
;1 1    B test1
;2 2    B test2
;3 3    B test3

MenuPopup m.AddItems(s)
int i=m.Show
str s1 s2
if(i and m.GetItemText(i s1 s2))
,EditReplaceSel hwnd 0 s2 1|2|4
#2
use the v attribute with  sub.right_menu then can access the t variable in the subfunction
 
Code:
Copy      Help
str t="A"

str dd=
F
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "{t}_Dialog" "4"
;3 Edit 0x54030080 0x200 68 52 96 13 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""

str controls = "3"
str e3
if(!ShowDialog(dd &sub.DlgProc &controls)) ret

#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,SetWindowSubclass(id(3 hDlg) &sub.WndProc_Subclass_Edit 3 0)
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

#sub WndProc_Subclass_Edit ;;Edit
function# hwnd message wParam lParam uIdSubclass dwRefData

;OutWinMsg message wParam lParam ;;uncomment to see received messages
sel message
,case WM_CONTEXTMENU
,sub.right_menu hwnd
,ret

int R=DefSubclassProc(hwnd message wParam lParam)

sel message
,case WM_NCDESTROY
,RemoveWindowSubclass(hwnd &sub.WndProc_Subclass_Edit uIdSubclass)
ret R

#sub right_menu v
function hwnd
str s=
F
;1 1    {t} test1
;2 2    {t} test2
;3 3    {t} test3

MenuPopup m.AddItems(s)
int i=m.Show
str s1 s2
if(i and m.GetItemText(i s1 s2))
,EditReplaceSel hwnd 0 s2 1|2|4
#3
It seems impossible to completely separate the same code

I put DlgProc in a separate function and there is a problem: the subfunction right_menu cannot output

Function C  
Code:
Copy      Help
str t="A"

str dd=
F
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "{t}_Dialog" "4"
;3 Edit 0x54030080 0x200 68 52 96 13 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""

str controls = "3"
str e3
if(!ShowDialog(dd &DlgProc &controls)) ret

Function DlgProc
Code:
Copy      Help
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,SetTimer hDlg 1 100 0 ;;Edit
,case WM_TIMER
,sel wParam
,,case 1 ;;Edit
,,int ec=id(3 hDlg); if(ec=0) ret
,,SetWindowSubclass(ec &sub.WndProc_Subclass_Edit 3 0)
,,KillTimer hDlg wParam
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

#sub WndProc_Subclass_Edit ;;Edit
function# hwnd message wParam lParam uIdSubclass dwRefData

;OutWinMsg message wParam lParam ;;uncomment to see received messages
sel message
,case WM_CONTEXTMENU
,sub.right_menu hwnd
,ret

int R=DefSubclassProc(hwnd message wParam lParam)

sel message
,case WM_NCDESTROY
,RemoveWindowSubclass(hwnd &sub.WndProc_Subclass_Edit uIdSubclass)
ret R

#sub right_menu v
function hwnd
str s=
;1 1    A test1
;2 2    A test2
;3 3    A test3

MenuPopup m.AddItems(s)
int i=m.Show
str s1 s2
if(i and m.GetItemText(i s1 s2))
,EditReplaceSel hwnd 0 s2 1|2|4
#4
There seems to be  bug in either the forum software or the copy for qm forum function that is adding an extra space after the tab in the menu definitions first is with copy for forum second is bbcode. 
 
Code:
Copy      Help
str s=
;1 normal
;2 disabled
;3 checked
;-
;4 radio group
;5 radio group, checked
;6 radio group
;-
;7 delete
;8 delete
;9 delete
;10 bold
;11 left-text    right-text

MenuPopup m.AddItems(s)

m.DisableItems("2")
m.CheckItems("3")
m.CheckRadioItem(4 6 5)
m.DeleteItems("7-9")
m.SetBold(10)

int i=m.Show

str s1 s2
if(i and m.GetItemText(i s1 s2))
,out s1
,out s2

str s=
;1 normal
;2 disabled
;3 checked
;-
;4 radio group
;5 radio group, checked
;6 radio group
;-
;7 delete
;8 delete
;9 delete
;10 bold
;11 left-text    right-text

MenuPopup m.AddItems(s)

m.DisableItems("2")
m.CheckItems("3")
m.CheckRadioItem(4 6 5)
m.DeleteItems("7-9")
m.SetBold(10)

int i=m.Show

str s1 s2
if(i and m.GetItemText(i s1 s2))
,out s1
,out s2
#5
The two pieces of code are identical when pasted into the QM editor


Forum Jump:


Users browsing this thread: 1 Guest(s)