using dialog resources in PB

Share your advanced PureBasic knowledge/code with the community.
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

using dialog resources in PB

Post by FloHimself »

ts-soft posted an example of how to use
(dialog) resources in PB on german forum .

you can find the thread here:
http://forums.purebasic.com/german/viewtopic.php?t=4188

dialog.rc

Code: Select all

// Einfache AboutBox für PureBasic 
// by TS-Soft 
1000 DIALOG 0, 0, 109, 52 
LANGUAGE LANG_NEUTRAL, 0 
STYLE DS_CENTER | DS_MODALFRAME | DS_SETFONT | WS_BORDER | WS_DLGFRAME | WS_POPUP | WS_SYSMENU | WS_VISIBLE 
FONT 9, "Arial" 
CAPTION "Über..." 
BEGIN 
    CONTROL         "PureBasic 3.94  Beta 4                   Feel the ...Pure... Power", 1001, "Static", WS_GROUP | WS_VISIBLE, 19, 7, 102, 20 
    CONTROL         "&Okay", 1002, "Button", BS_BOTTOM | BS_CENTER | BS_DEFPUSHBUTTON | BS_TOP | WS_MAXIMIZEBOX | WS_VISIBLE, 35, 30, 50, 15 
END 
pb source: (sorry for german comments, but i am lazy)

Code: Select all

; Simples Beispiel für eine AboutBox als Resource 
; erstellt von TS-Soft 

;/############################################################################ 
Procedure GetResDialog(DialogId, *DialogProcedure) 
  ProcedureReturn DialogBoxParam_(GetModuleHandle_(0), DialogId, 0, *DialogProcedure, 0) 
EndProcedure 
;/############################################################################ 


;/ Deklaration von Konstanten 
; #define IDD_DLG1 1000 
; #define IDC_LBL1 1001 
; #define IDC_BTN1 1002 
#IDD_DLG1 = 1000 
#IDC_LBL1 = 1001 
#IDC_BTN1 = 1002 

; Beispiel einer DialogProcedure 
Procedure DlgProc(hDlg, uMsg, wParam, lParam) 
  Select uMsg 
    Case #WM_INITDIALOG 
      MessageBeep_(#MB_ICONINFORMATION) 
    Case #WM_CLOSE 
      EndDialog_(hDlg, 0) 
    Case #WM_COMMAND 
      ID = wParam & $FFFF 
      Select ID 
        Case #IDC_BTN1 
          EndDialog_(hDlg, #IDC_BTN1) ; 1002 als Rückgabewert bei okay 
      EndSelect 
  EndSelect 
EndProcedure 

; Aufruf der DialogResource 
Select GetResDialog(#IDD_DLG1, @DlgProc()) 
  Case 0 
    MessageRequester("Dialog-Test", "Dialog wurde geschlossen") 
  Case #IDC_BTN1 
    MessageRequester("Dialog-Test", "Benutzer hat Okay gedrückt zum beenden") 
EndSelect
and a demo by fgk:
Image
download:
http://home.tiscali.de/pbsource/PB_Stuf ... EdDemo.exe

the best dialog / resource editor we found seens ResEd.
download: http://radasm.visualassembler.com/projects/ResEd.zip (written in MASM with source code)

after some tests playing with the dialog editor i created an
example how to use the "custom-controls" that can be found
at http://radasm.visualassembler.com/:

Image

source:
http://rzserv2.fhnon.de/~lg016586/downl ... ogTest.zip

then i modified the original ResEd to export the resource names as
PB constants.
Image
you can download this modified version of ResEd at:
http://rzserv2.fhnon.de/~lg016586/downl ... modded.exe (i will release modified source in some hours/days /EDIT: PureBasic is now supported in official release of ResEd)

thanks @ts-soft and @fgk for the thread on german forum.

i hope this info is interesting for someone here, too.
it's an good alternative (at least for me) to the windows created with the
visual designers.
Last edited by FloHimself on Thu Oct 06, 2005 6:36 pm, edited 1 time in total.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
Ziltch
User
User
Posts: 59
Joined: Sun Aug 03, 2003 12:05 am
Location: Australia

Post by Ziltch »

That is some very handy info!
thank you.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Does anyone still have a copy of PBResEdDemo.exe for the example in the original post?
thanos
Enthusiast
Enthusiast
Posts: 423
Joined: Sat Jan 12, 2008 3:25 pm
Location: Greece
Contact:

Re:

Post by thanos »

Mistrel wrote:Does anyone still have a copy of PBResEdDemo.exe for the example in the original post?
If you still have interest check out this http://www.realsource.de/tmp/dialogresource_example.zip
Regards.

Thanos
» myPersonal Banker :: Because you do not need to have a master degree in economics in order to organize your finances!
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Re: using dialog resources in PB

Post by javabean »

I adapted TS-Soft's example and put it here just for reference...
Some remarks:
(1) The dialog was composed with 'ResEdt' (you can get it here: http://www.oby.ro/rad_asm/projects/ResEd.zip)
(2) As 'porc.exe' (distributed with PureBasic) doesn't compile the resource script (because of lacking header files), I tried 'GoRC.exe' as resource compiler (you can get it here: http://www.godevtool.com/index.htm, command: gorc /r InsertDialog.rc) and imported the resulting 'InsertDialog.res' using the PureBasic 'Import' statement.

'InsertDialog.rc' (-> compile to 'InsertDialog.res')

Code: Select all

#define IDD_DLG_INSERT 1000
#define IDC_STC_INSERT 1001
#define IDC_BTN_INSERTBEFORE 1002
#define IDC_BTN_INSERTAFTER 1003
IDD_DLG_INSERT DIALOGEX 0,0,140,80
CAPTION "Insert where?"
FONT 8,"MS Shell Dlg",0,0,0	;we use the system font -> to do this you have to specify 'DS_SHELLFONT' in the following line
STYLE WS_POPUP|WS_VISIBLE|WS_CAPTION|WS_SYSMENU|DS_CENTER|DS_MODALFRAME|DS_SHELLFONT
BEGIN
  CONTROL "Where do you want to insert?",IDC_STC_INSERT,"Static",WS_CHILD|WS_VISIBLE,6,6,133,15
  CONTROL "Before",IDC_BTN_INSERTBEFORE,"Button",WS_VISIBLE|WS_TABSTOP|BS_VCENTER|BS_CENTER|BS_DEFPUSHBUTTON,9,24,120,20
  CONTROL "After",IDC_BTN_INSERTAFTER,"Button",WS_VISIBLE|WS_TABSTOP|BS_VCENTER|BS_CENTER|BS_DEFPUSHBUTTON,9,51,120,20
END
PureBasic code:

Code: Select all

;============================================
;Simple 'Dialog Box from Resource' example
;============================================
;adapted from an example by TS-Soft and WinAPI Documentation (
;http://msdn.microsoft.com/en-us/library/windows/desktop/ms644996%28v=vs.85%29.aspx)
;Tested on PureBasic 5.11 (Windows - x86)

;-----<IMPORTS>-------------------------------

;Import Dialog Resource 
Import "InsertDialog.res" : EndImport

;-----<CONSTANTS>-----------------------------

;Declare dialog constants
#IDD_DLG_INSERT       = 1000
#IDC_STC_INSERT       = 1001
#IDC_BTN_INSERTBEFORE = 1002
#IDC_BTN_INSERTAFTER  = 1003

;-----<MACROS>--------------------------------

Macro HIWORD(long)
  (long >> 16) & $FFFF
EndMacro

Macro LOWORD(long)
  long & $FFFF
EndMacro

;-----<PROCEDURES>----------------------------

;Centers dialog window over its parent window
Procedure CenterDialog(hDlg.i)
  
  Protected hwndOwner.i, rc.RECT, rcDlg.RECT, rcOwner.RECT
  
  ;Get the owner window and dialog box rectangles.
  hwndOwner = GetParent_(hDlg)
  
  If hwndOwner = #Null
    hwndOwner = GetDesktopWindow_()
  EndIf
  
  GetWindowRect_(hwndOwner, @rcOwner)
  GetWindowRect_(hDlg, @rcDlg)
  CopyRect_(@rc, @rcOwner)
  
  ;Offset the owner and dialog box rectangles so that right and bottom 
  ;values represent the width and height, and then offset the owner again 
  ;to discard space taken up by the dialog box. 
  
  OffsetRect_(@rcDlg, -rcDlg\left, -rcDlg\top)
  OffsetRect_(@rc, -rc\left, -rc\top)
  OffsetRect_(@rc, -rcDlg\right, -rcDlg\bottom)
  
  ;The new position is the sum of half the remaining space and the owner's 
  ;original position. 
  
  SetWindowPos_(hDlg, 
                #HWND_TOP, 
                rcOwner\left + (rc\right / 2), 
                rcOwner\top + (rc\bottom / 2), 
                0, 0,          ; Ignores size arguments. 
                #SWP_NOSIZE)
 
EndProcedure

;Dialog procedure
Procedure DialogProcedure(hDlg, uMsg, wParam, lParam)
  
  Protected retval.i
  
  Select uMsg
    Case #WM_INITDIALOG
      
      Debug "InitParam of 'OpenResourceDialog' = " + lParam
      
      ;Centers dialog dindow over its parent window
      ;============================================
      CenterDialog(hDlg)
      
      ;Setting focus on Dialog Gadget '#IDC_BTN_INSERTAFTER' 
      ;=====================================================
      ;Before setting the input focus, the procedure checks the control identifier of the default input focus.
      ;The system passes the window handle of the default input focus in the wParam parameter. The GetDlgCtrlID
      ;function returns the identifier for the control identified by the window handle. If the identifier does not
      ;match the correct identifier, the procedure uses the SetFocus function to set the input focus.
      ;The GetDlgItem function is required to retrieve the window handle of the desired control.
      If GetDlgCtrlID_(wParam <> #IDC_BTN_INSERTAFTER) 
        SetFocus_(GetDlgItem_(hDlg, #IDC_BTN_INSERTAFTER))
        ProcedureReturn #False
      EndIf      
      ProcedureReturn #True                   
      
    Case #WM_CLOSE
      retval = 0
      EndDialog_(hDlg, retval) ; if dialog is closed return '0'
      ProcedureReturn #True
      
    Case #WM_COMMAND
      Select LOWORD(wParam)             ;Get dialog gadget
        Case #IDC_BTN_INSERTBEFORE, #IDC_BTN_INSERTAFTER
          EndDialog_(hDlg, LOWORD(wParam))  ;return dialog gadget number
          ProcedureReturn #True
      EndSelect
      
  EndSelect
  
  ProcedureReturn #False
EndProcedure

;Open resource dialog
Procedure.i OpenResourceDialog(DialogNr.i, *DlgProc, ParentWndNr.i, InitParam.i = 0)
  
  ProcedureReturn DialogBoxParam_(GetModuleHandle_(#Null), DialogNr, WindowID(ParentWndNr), *DlgProc, InitParam)
  
EndProcedure


;-----<MAIN>----------------------------------

If OpenWindow(0, 200, 200, 500, 300 , "Resource Dialogs", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget|#PB_Window_TitleBar) 
  
  ButtonGadget(0, 10, 10, 100, 40, "Open Dialog")
  
  Repeat 
    
    Evt.i =WaitWindowEvent() 
    Select Evt 
        
      Case #PB_Event_CloseWindow
        Quit.i = #True
        
      Case #PB_Event_Gadget 
        Select OpenResourceDialog(#IDD_DLG_INSERT, @DialogProcedure(), 0, 123456)
          Case 0
            MessageRequester("Dialog Test", "Dialog was closed")
          Case #IDC_BTN_INSERTBEFORE
            MessageRequester("Dialog Test", "Dialog button 'Before' was clicked.")
          Case #IDC_BTN_INSERTAFTER
            MessageRequester("Dialog Test", "Dialog button 'After' was clicked.")
        EndSelect 
        
    EndSelect 
    
  Until Quit = #True
  End
  
EndIf
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: using dialog resources in PB

Post by Michael Vogel »

Has anyone tried to convert DialogEx resources to Purebasic statements (gadgets)?

Code: Select all

5513 DIALOGEX 0, 0, 422, 289, 0
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Title"
FONT 8, "MS Shell Dlg", 0, FALSE
{
 CONTROL "", -1, "?group text:", WS_GROUP, 14, 14, 42, 8
 CONTROL "", 6201, "?<test>", WS_GROUP | 0x00000080, 56, 14, 273, 8
 CONTROL "", 8585215, "file ame:", NOT WS_CHILD | NOT WS_VISIBLE | WS_HSCROLL | WS_SYSMENU | WS_GROUP | WS_TABSTOP | 0x0000000E, 37, 8, -1, -1, WS_EX_STATICEDGE | 0x50000000
 CONTROL "", 6202, "?", WS_VSCROLL | WS_TABSTOP | 0x00000042, 56, 25, 100, 77
 :
should be transformed to
OpenWindow(....)
FrameGadget(...)
StringGadget(...)
etc.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: using dialog resources in PB

Post by Michael Vogel »

Here's a quick start which only converts the dialog window but no gadgets, also no error handling or flag filtering/conversion is done...

Code: Select all

; Define

	#DialogExIDAuto=0
	#DialogExIDConstants=4
	#DialogExComma=",";			or ", "

	Structure CoordType
		x.i
		y.i
		w.i
		h.i
	EndStructure

; EndDefine

Procedure GetCoords(s.s,n,*c.CoordType)

	*c\x=Val(StringField(s,n,","))
	*c\y=Val(StringField(s,n+1,","))
	*c\w=Val(StringField(s,n+2,","))
	*c\h=Val(StringField(s,n+3,","))

EndProcedure
Procedure.s IDString(n)

	If #DialogExIDConstants
		ProcedureReturn "#ID"+RSet(Str(n),#DialogExIDConstants,"0")
	Else
		ProcedureReturn Str(n)
	EndIf

EndProcedure
Procedure.s CoordString(*c.CoordType)

	ProcedureReturn Str(*c\x)+#DialogExComma+Str(*c\y)+#DialogExComma+Str(*c\w)+#DialogExComma+Str(*c\h)

EndProcedure
Procedure.s FlagString(s.s)

	s=ReplaceString(s," ","")
	s=ReplaceString(s,"|","|#")

	ProcedureReturn "#"+s

EndProcedure

Procedure ConvertDialogEx()

	Protected s.s,t.s
	Protected Error,n
	Protected ID,x,y,w,h
	Protected flags.s
	Protected caption.s
	Protected coords.CoordType
	Protected constants.s

	If ReadFile(0,"dialog.txt")
		s=ReadString(0)
		If StringField(s,2," ")="DIALOGEX"
			If #DialogExIDAuto
				ID+1
			Else
				ID=Val(StringField(s,1," "))
			EndIf
			n=FindString(s," ",FindString(s," ")+1)
			s=Mid(s,n+1)
			GetCoords(s,1,coords)

			Repeat
				s=ReadString(0)
				Select Left(s,5)
				Case "STYLE"
					flags=FlagString(Mid(s,7))
				Case "CAPTI"
					caption=Mid(s,9)
				Case "FONT "
				EndSelect
			Until Error Or s="{"

			s=IDString(ID)
			constants+s+#CRLF$
			Debug "OpenWindow("+s+#DialogExComma+CoordString(coords)+#DialogExComma+caption+#DialogExComma+flags+")"
		Else
			Error=1
		EndIf

		If Error=0
			Repeat
				s=Trim(ReadString(0))
				If StringField(s,1," ")="CONTROL"
					Debug s
				EndIf
			Until Error Or s="}"

		EndIf

	EndIf

	ProcedureReturn Error

EndProcedure

ConvertDialogEx()
Target is to replace dialog ressources like the following with native purebasic commands. Even better would be a bidirectional conversion, this would allow to use different ressource kits for creating dialog forms...

Code: Select all

2000 DIALOGEX 26, 41, 261, 88, 0
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUPWINDOW | WS_CAPTION
EXSTYLE = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE
CAPTION "Go To..."
FONT 8, "MS Shell Dlg", 0, FALSE
{
 CONTROL "", 2007, "?&Line", WS_TABSTOP | 0x00000009, 5, 9, 80, 10
 CONTROL "", 2008, "?&Offset", WS_TABSTOP | 0x00000009, 98, 9, 80, 10
 CONTROL "", 2004, "?You are here :", 0, 5, 34, 95, 8
 CONTROL "", 8585215, "123456789", NOT WS_CHILD | NOT WS_VISIBLE | WS_VSCROLL | WS_GROUP | 0x00000064, 45, 8, 2002, 0, 0x50000000
 CONTROL "", 8585215, "ou want to &go to :", NOT WS_CHILD | NOT WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_GROUP | WS_TABSTOP | 0x00000005, 95, 8, 2005, 0, WS_EX_STATICEDGE | 0x50000000
 CONTROL "", 8519679, "", NOT WS_CHILD | NOT WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP | 0x00000062, 71, 12, 2001, 0, WS_EX_RTLREADING | WS_EX_CONTROLPARENT | 0x50800000
 CONTROL "", 8585215, "ou can't go further than :", NOT WS_CHILD | NOT WS_VISIBLE | WS_DLGFRAME | WS_THICKFRAME | 0x00000005, 92, 8, 2006, 0, 0x50000000
 CONTROL "", 2003, "?0123456789", 0, 100, 68, 45, 8
 CONTROL "", 8454143, "o", NOT WS_CHILD | NOT WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | 0x000000B5, 70, 14, 1, 0, WS_EX_DLGMODALFRAME | WS_EX_LEFTSCROLLBAR | WS_EX_CONTROLPARENT | 0x50000000
 CONTROL "", 8454143, "'m going nowhere", NOT WS_CHILD | NOT WS_VISIBLE | WS_DLGFRAME | WS_GROUP | 0x000000B5, 70, 14, 2, 0, WS_EX_LEFTSCROLLBAR | WS_EX_CONTROLPARENT | 0x50000000
}
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: using dialog resources in PB

Post by Michael Vogel »

The actual iteration creates a source code showing a dialog...

Code: Select all

; Define

	#DialogExIDAuto=1
	#DialogExIDConstants=2
	#DialogExComma=",";			or ", "

	Structure CoordType
		x.i
		y.i
		w.i
		h.i
	EndStructure

; EndDefine

Procedure Max(a,b)
	If a>b
		ProcedureReturn a
	Else
		ProcedureReturn b
	EndIf
EndProcedure
Procedure GetCoords(s.s,n,*c.CoordType)

	*c\x=Val(StringField(s,n,","))
	*c\y=Val(StringField(s,n+1,","))
	*c\w=Val(StringField(s,n+2,","))
	*c\h=Val(StringField(s,n+3,","))

EndProcedure
Procedure.s IDString(n)

	If #DialogExIDConstants
		ProcedureReturn "#ID"+RSet(Str(n),#DialogExIDConstants,"0")
	Else
		ProcedureReturn Str(n)
	EndIf

EndProcedure
Procedure.s CoordString(*c.CoordType)

	ProcedureReturn Str(Max(0,*c\x))+#DialogExComma+Str(Max(0,*c\y))+#DialogExComma+Str(Max(0,*c\w))+#DialogExComma+Str(Max(0,*c\h))

EndProcedure
Procedure.s FlagString(s.s,mode)

	s=ReplaceString(s," ","")
	s=ReplaceString(s,"|","|#")
	s="#"+s

	If mode=0
		s=ReplaceString(s,"#DS_FIXEDSYS","")
	Else
		s=ReplaceString(s,"#0x","$")
		s=ReplaceString(s,"#NOTWS_CHILD","")
		s=ReplaceString(s,"#NOTWS_VISIBLE","")
	EndIf

	While Left(s,1)="|"
		s=Mid(s,2)
	Wend

	s=ReplaceString(s,"|||","|")
	s=ReplaceString(s,"||","|")

	ProcedureReturn s

EndProcedure

Procedure ConvertDialogEx()

	Protected s.s,t.s
	Protected Error,n
	Protected ID,x,y,w,h
	Protected flags.s
	Protected caption.s
	Protected gadget.s
	Protected coords.CoordType
	Protected constants.s
	Protected code.s

	If ReadFile(0,"dialog.txt")
		s=ReadString(0)
		If StringField(s,2," ")="DIALOGEX"
			If #DialogExIDAuto
				ID+1
			Else
				ID=Val(StringField(s,1," "))&$7FFFFFFF
			EndIf
			n=FindString(s," ",FindString(s," ")+1)
			s=Mid(s,n+1)
			GetCoords(s,1,coords)

			Repeat
				s=ReadString(0)
				Select Left(s,5)
				Case "STYLE"
					flags=FlagString(Mid(s,7),0)
				Case "CAPTI"
					caption=Mid(s,9)
				Case "FONT "
				EndSelect
			Until Error Or s="{"

			s=IDString(ID)
			constants+#TAB$+s+#CRLF$
			code+"OpenWindow("+s+#DialogExComma+CoordString(coords)+#DialogExComma+caption+#DialogExComma+flags+")"+#CRLF$
		Else
			Error=1
		EndIf

		If Error=0
			Repeat
				s=Trim(ReadString(0))
				If StringField(s,1," ")="CONTROL"
					s=Mid(s,9)
					If #DialogExIDAuto
						ID+1
					Else
						ID=Val(StringField(s,2,","))&$7FFFFFFF
					EndIf
					caption=Trim(StringField(s,3,","))
					flags=FlagString(StringField(s,4,","),1)
					GetCoords(s,5,coords)
					gadget="TextGadget"
					s=IDString(ID)
					constants+#TAB$+s+#CRLF$
					code+gadget+"("+s+#DialogExComma+CoordString(coords)+#DialogExComma+caption+#DialogExComma+flags+")"+#CRLF$
				EndIf
			Until Error Or s="}"

		EndIf

	EndIf
	
	constants="Enumeration"+#CRLF$+constants+"EndEnumeration"+#CRLF$
	code+#CRLF$+"Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow"+#CRLF$
	Debug constants
	Debug code
	
	SetClipboardText(constants+#CRLF$+code)
	
	ProcedureReturn Error

EndProcedure

ConvertDialogEx()
Post Reply