Page 1 of 1

Changing windows Title via combobox

Posted: Wed Apr 22, 2015 9:03 pm
by ly47
Hi
Again I'm using native API code for learning purpose.
I'm trying to change Window title by combobox.
instead of a string I get a number (may be string address).
Please help
thanks
ly

Code: Select all

EnableExplicit

Declare SetWinColor(hWnd, clr)
Declare addStringCombo()
Declare xHiWord(a.l) 
Declare xLoWord(a.l) 

Global wc.WNDCLASSEX, hInstance, msg.MSG,  hCb, hue, title.s

hInstance = GetModuleHandle_(#Null)

Enumeration
	#Button_1
	#Button_2
	#Button_3
	#Combo_1
EndEnumeration

#Style = #WS_OVERLAPPEDWINDOW | #WS_VISIBLE

Procedure WindowCallback(hWnd, uMsg, wParam, lParam) 
	Protected hBtn1, hBtn2, hBtn3, Result, indx
	
	Select uMsg 
			
		Case #WM_CREATE
						
			hBtn1 = CreateWindowEx_(0, "Button", "Exit", #WS_CHILD | #WS_VISIBLE, 520, 328, 60, 25, hWnd, #Button_1, hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "RED", #WS_CHILD | #WS_VISIBLE, 440, 328, 60, 25, hWnd, #Button_2,hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "BLUE", #WS_CHILD | #WS_VISIBLE, 360, 328, 60, 25, hWnd, #Button_3, hInstance, 0)
			hCb = CreateWindowEx_(0, "combobox", 0, #WS_CHILD | #WS_VISIBLE |#WS_TABSTOP|#CBS_DROPDOWNLIST|#WS_VSCROLL,
			                    250, 328, 90, 235, hWnd, #Combo_1, hInstance, 0)
			addStringCombo()
			
		Case #WM_COMMAND
			
			Select xLoWord(wParam) 
				
				Case #Button_1
					
; 					Debug "#Button_1"
					
					PostQuitMessage_(0)
					
				Case #Button_2
					
; 					Debug "#Button_2"
					
					SetWinColor(hWnd,RGB(233,95,85))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Button_3
					
; 					Debug "#Button_3"
					
					SetWinColor(hWnd, RGB(85,95,233))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Combo_1 
					
					If xHiWord(wParam) = #CBN_SELCHANGE
						
						indx = SendMessage_(hcb,#CB_GETCURSEL,0,0)
						
						SendMessage_(hCb, #CB_GETLBTEXT,indx, @hue)     ;  HERE IS THE PROBLEM 
						
						title = "Simple -  " + @hue
						
						SendMessage_(hWnd,#WM_SETTEXT,0,@title)
						
						Select indx
								
							Case 0	;default
								
								SetWinColor(hWnd,RGB(255,255,255))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 1	;red
								
 								SetWinColor(hWnd,RGB(233,95,85))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 2  ;blue

								SetWinColor(hWnd, RGB(85,95,233))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
						EndSelect
											
					EndIf
					
			EndSelect

		Case #WM_CLOSE 
			DestroyWindow_(hWnd) 
			Result  = 0 
		Case #WM_DESTROY 
			PostQuitMessage_(0) 
			Result  = 0 
		Default 
			Result  = DefWindowProc_(hWnd, uMsg, wParam, lParam) 
	EndSelect 
	ProcedureReturn Result 
EndProcedure  

Define.s appName
appName = "Win32" 

	wc\style         = #CS_HREDRAW | #CS_VREDRAW 
	wc\cbsize  = SizeOf(WNDCLASSEX) 
	wc\lpfnWndProc  = @WindowCallback() 
	wc\hInstance     = hInstance 
	wc\hIcon         = LoadIcon_(0, #IDI_APPLICATION ) 
	wc\hCursor  = LoadCursor_(0,#IDC_ARROW)
	wc\hbrBackground  = #COLOR_WINDOW + 1
	wc\lpszMenuName  = 0
	wc\lpszClassName  = @appName
	
	If ( RegisterClassEx_( @wc ) = #False ) 
        MessageBox_(#Null, "Failed to register wc!", appName, #MB_ICONERROR )
        End 1
    EndIf 

Define hWnd  = CreateWindowEx_(0, appName, "Simple", #Style, 100, 100, 600, 450, 0, 0, hInstance, 0) 

ShowWindow_(hWnd,  #SW_SHOWDEFAULT) 
UpdateWindow_(hWnd)
SetForegroundWindow_(hWnd)

While GetMessage_(msg, 0, 0, 0 ) 
	TranslateMessage_(msg) 
	DispatchMessage_(msg) 
Wend
    
; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Procedure SetWinColor(hWnd, clr)
	Static obj, hbr

	hbr = CreateSolidBrush_(clr)
	obj  = SetClassLongPtr_(hWnd ,#GCL_HBRBACKGROUND ,hbr)
	DeleteObject_(obj)
	RedrawWindow_(hWnd, 0, 0, 0)
	
	ProcedureReturn 0
EndProcedure

Procedure addStringCombo()
	SendMessage_(hCb,#CB_ADDSTRING,0, "DEF")
	SendMessage_(hCb,#CB_ADDSTRING,0, "RED")
	SendMessage_(hCb,#CB_ADDSTRING,0,"BLUE")
	SendMessage_(hCb,#CB_SETCURSEL,0,0)
EndProcedure


Procedure xHiWord(a.l) 
	ProcedureReturn Int(a / $10000) 
EndProcedure 

Procedure xLoWord(a.l) 
	ProcedureReturn Int(a - (Int(a/$10000)*$10000)) 
EndProcedure

Re: Changing windows Title via combobox

Posted: Wed Apr 22, 2015 9:38 pm
by ly47
Hi
Problem solved.

Code: Select all

EnableExplicit

Declare SetWinColor(hWnd, clr)
Declare addStringCombo()
Declare xHiWord(a.l) 
Declare xLoWord(a.l) 

Global wc.WNDCLASSEX, hInstance, msg.MSG,  hCb, title.s, hue$=Space(6)
; hue$ = Space(8)
hInstance = GetModuleHandle_(#Null)

Enumeration
	#Button_1
	#Button_2
	#Button_3
	#Combo_1
EndEnumeration

#Style = #WS_OVERLAPPEDWINDOW | #WS_VISIBLE

Procedure WindowCallback(hWnd, uMsg, wParam, lParam) 
	Protected hBtn1, hBtn2, hBtn3, Result, indx
	
	Select uMsg 
			
		Case #WM_CREATE
						
			hBtn1 = CreateWindowEx_(0, "Button", "Exit", #WS_CHILD | #WS_VISIBLE, 520, 328, 60, 25, hWnd, #Button_1, hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "RED", #WS_CHILD | #WS_VISIBLE, 440, 328, 60, 25, hWnd, #Button_2,hInstance, 0)
			hBtn2 = CreateWindowEx_(0, "Button", "BLUE", #WS_CHILD | #WS_VISIBLE, 360, 328, 60, 25, hWnd, #Button_3, hInstance, 0)
			hCb = CreateWindowEx_(0, "combobox", 0, #WS_CHILD | #WS_VISIBLE |#WS_TABSTOP|#CBS_DROPDOWNLIST|#WS_VSCROLL,
			                    250, 328, 90, 235, hWnd, #Combo_1, hInstance, 0)
			addStringCombo()
			
		Case #WM_COMMAND
			
			Select xLoWord(wParam) 
				
				Case #Button_1
					
; 					Debug "#Button_1"
					
					PostQuitMessage_(0)
					
				Case #Button_2
					
; 					Debug "#Button_2"
					
					SetWinColor(hWnd,RGB(233,95,85))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Button_3
					
; 					Debug "#Button_3"
					
					SetWinColor(hWnd, RGB(85,95,233))
					InvalidateRect_(hWnd,#False,#True)
					UpdateWindow_(hWnd)
					
				Case  #Combo_1 
					
					If xHiWord(wParam) = #CBN_SELCHANGE
						
						indx = SendMessage_(hcb,#CB_GETCURSEL,0,0)
						
						SendMessage_(hCb, #CB_GETLBTEXT,indx, @hue$)
						
						title = "Simple -  " + hue$
						
								SendMessage_(hWnd,#WM_SETTEXT,0,@title)
						
						Select indx
								
							Case 0	;default
								
								SetWinColor(hWnd,RGB(255,255,255))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 1	;red
								
 								SetWinColor(hWnd,RGB(233,95,85))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
							Case 2  ;blue

								SetWinColor(hWnd, RGB(85,95,233))
								InvalidateRect_(hWnd,#False,#True)
								UpdateWindow_(hWnd)
								
						EndSelect
											
					EndIf
					
			EndSelect

		Case #WM_CLOSE 
			DestroyWindow_(hWnd) 
			Result  = 0 
		Case #WM_DESTROY 
			PostQuitMessage_(0) 
			Result  = 0 
		Default 
			Result  = DefWindowProc_(hWnd, uMsg, wParam, lParam) 
	EndSelect 
	ProcedureReturn Result 
EndProcedure  

Define.s appName
appName = "Win32" 

	wc\style         = #CS_HREDRAW | #CS_VREDRAW 
	wc\cbsize  = SizeOf(WNDCLASSEX) 
	wc\lpfnWndProc  = @WindowCallback() 
	wc\hInstance     = hInstance 
	wc\hIcon         = LoadIcon_(0, #IDI_APPLICATION ) 
	wc\hCursor  = LoadCursor_(0,#IDC_ARROW)
	wc\hbrBackground  = #COLOR_WINDOW + 1
	wc\lpszMenuName  = 0
	wc\lpszClassName  = @appName
	
	If ( RegisterClassEx_( @wc ) = #False ) 
        MessageBox_(#Null, "Failed to register wc!", appName, #MB_ICONERROR )
        End 1
    EndIf 

Define hWnd  = CreateWindowEx_(0, appName, "Simple", #Style, 100, 100, 600, 450, 0, 0, hInstance, 0) 

ShowWindow_(hWnd,  #SW_SHOWDEFAULT) 
UpdateWindow_(hWnd)
SetForegroundWindow_(hWnd)

While GetMessage_(msg, 0, 0, 0 ) 
	TranslateMessage_(msg) 
	DispatchMessage_(msg) 
Wend
    
; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Procedure SetWinColor(hWnd, clr)
	Static obj, hbr

	hbr = CreateSolidBrush_(clr)
	obj  = SetClassLongPtr_(hWnd ,#GCL_HBRBACKGROUND ,hbr)
	DeleteObject_(obj)
	RedrawWindow_(hWnd, 0, 0, 0)
	
	ProcedureReturn 0
EndProcedure

Procedure addStringCombo()
	SendMessage_(hCb,#CB_ADDSTRING,0, "DEF")
	SendMessage_(hCb,#CB_ADDSTRING,0, "RED")
	SendMessage_(hCb,#CB_ADDSTRING,0,"BLUE")
	SendMessage_(hCb,#CB_SETCURSEL,0,0)
EndProcedure


Procedure xHiWord(a.l) 
	ProcedureReturn Int(a / $10000) 
EndProcedure 

Procedure xLoWord(a.l) 
	ProcedureReturn Int(a - (Int(a/$10000)*$10000)) 
EndProcedure