Control disappearance after SetWindowLong

Windows specific questions.
Post Reply
spartacus13012
Posts: 18
Joined: Nov 30, 2014 12:37
Location: FRANCE

Control disappearance after SetWindowLong

Post by spartacus13012 »

Hello,

To test the 'SetWindowLong' to classify a sub control 'edit', I used a code page of the site:

http://www.freebasic.net/forum/viewtopic.php?t=13451:

Code: Select all

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	#Include "windows.bi"
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Dim Shared As ZString * 1024 txt
	Dim Shared As MSG msg
	Dim Shared As HWND hWnd, btn1, edt1
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	Function WndProc_edt1(ByVal hWnd As HWND, _ 
                 			 ByVal message As UINT, _
                 			 ByVal wParam As WPARAM, _
                 			 ByVal lParam As LPARAM) As LRESULT
   	Function = 0
   	
   	'If hWnd = edt1 Then 
   	'	Print "WndProc_edt1", Hex(message), wParam, lParam
   	'EndIf
   	'
   	'If message = WM_KEYDOWN Then
   	'	Print "WndProc_edt1 : message clavier", wParam, lParam
   	'EndIf

  	 	Function = DefWindowProc(hWnd, message, wParam, lParam)
	End Function
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	' Create window
	hWnd = CreateWindowEx(0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or _ 
											WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0)
	
	' Create button
	btn1 = CreateWindowEx(0, "BUTTON", "Button #1", WS_VISIBLE Or _  
											WS_CHILD, 20, 10, 100, 30, hWnd, 0, 0, 0)
	
	' Create edit box
	edt1 = CreateWindowEx(0, "EDIT", "Type text here...", ws_border Or _ 
											WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or _ 
											WS_VSCROLL Or ES_AUTOHSCROLL Or _ 
											ES_AUTOVSCROLL Or ES_MULTILINE, _ 
											20, 50, 200, 100, hWnd, 0, 0, 0)

	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	SetWindowLong(edt1, GWL_WNDPROC, Cast(DWORD, @WndProc_edt1))
	SetWindowPos(edt1, HWND_TOPMOST, 0, 0, 0, 0, _ 
		          SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
	
	InvalidateRect(edt1, NULL, TRUE)
	InvalidateRect(hWnd, NULL, TRUE)
	EnableWindow(edt1, TRUE)
	
	SetWindowText(edt1, "texte editbox")
	
	showWindow(edt1, SW_HIDE)
	showWindow(edt1, SW_SHOW)
	Print "IsWindowVisible", IsWindowVisible(edt1)
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	
	While GetMessage(@msg, 0, 0, 0)
		TranslateMessage(@msg)
		DispatchMessage(@msg)
		
		Select Case msg.hwnd
			Case hWnd
				Select Case msg.message
					Case 273
						End	          
					
					' If left mouse button was pressed in window area then
					' check if is edit box text = "". If it is then set
					' the edit box text to "Type text here" and set focus
					' to the window
					Case WM_LBUTTONDOWN
						Dim As ZString*1024 txt
					
						GetWindowText(edt1, txt, SizeOf(txt))
						If txt = "" Then 
							SetWindowText(edt1, "Type text here...")
						EndIf
					
						SetFocus(hWnd)  ' Set focus to the window
					Case Else
						' Create rect variable and store window size in it
						Dim As RECT rct: GetClientRect( hWnd, @rct )
						
						' Resize the edit box
						MoveWindow( edt1, 20, 50, rct.right-40, rct.bottom-60, TRUE )
				End Select
			Case btn1
				Select Case msg.message
					' If left mouse button was pressed in button area then
					' check if is edit box text = "". If it is then set
					' the edit box text to "Type text here"
					Case WM_LBUTTONDOWN
						' When button is pressed set the text
						' of button to "pressed"
						SetWindowText( btn1, "Clicked!" )
						
						Dim As ZString*1024 txt
						
						GetWindowText( edt1, txt, SizeOf( txt ) )
						If txt = "" Then 
							SetWindowText( edt1, "Type text here..." )
						EndIf
					
					' If left mouse button was released from the button area
					' then set the button text to "Button #1" and show
					' massage box with text from text box
					Case WM_LBUTTONUP
						SetWindowText( btn1, "Button #1" )
						
						Dim As ZString*1024 txt
						
						GetWindowText( edt1, txt, SizeOf( txt ) )
						MessageBox( hWnd, txt, "Hello", MB_OK )
				End Select
			Case edt1
				Select Case msg.message
					' When textbox was pressed then clar the textbox text if
					' text = "Type text here..."
					Case WM_LBUTTONDOWN
						Dim As ZString * 1024 txt
					
						GetWindowText(edt1, txt, SizeOf(txt))
						If txt = "Type text here..." Then 
							SetWindowText(edt1, "")
						EndIf
						
					Case WM_keydown
						Print "boucle messages clavier", msg.wparam, msg.lparam
						
				End Select
		End Select
	Wend
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I have the code ready inserted:

Code: Select all

         SetWindowLong(edt1, GWL_WNDPROC, Cast(DWORD, @WndProc_edt1))

As control disappeared I followed the doc function 'SetWindowLong' I added it Code:

Code: Select all

          SetWindowPos(edt1, HWND_TOPMOST, 0, 0, 0, 0, _ 
		          SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)


Surprise it does not work.

I tried a lot of things, thinking that the control was 'invisible', invalid, even see in the background. Anyway nothing seems to work. So I handed me to you, as I necessarily missed something, who can tell me what it is.

Thank you in advance
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Control disappearance after SetWindowLong

Post by SARG »

Bonjour/Hi,

Just save the old address for the window procedure then call it, if needed, in the new proc.
See oldproc and callwindowproc.

No time for a large answer, search information about setwindowlong all is explained.

Code: Select all

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    #Include "windows.bi"
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim Shared As ZString * 1024 txt
    Dim Shared As MSG msg
    Dim Shared As HWND hWnd, btn1, edt1, oldproc
    
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function WndProc_edt1(ByVal hWnd As HWND, _ 
                             ByVal message As UINT, _
                             ByVal wParam As WPARAM, _
                             ByVal lParam As LPARAM) As LRESULT
    Function = 0
    
    If hWnd = edt1 Then 
       Print "WndProc_edt1", Hex(message), wParam, lParam
    EndIf
    
    If message = WM_KEYDOWN Then
       Print "WndProc_edt1 : message clavier", wParam, lParam
    EndIf
'=============
CallWindowProc(oldproc,hwnd,message,wparam,lparam)
'==============
    End Function
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Create window
    hWnd = CreateWindowEx(0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or _ 
                                            WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0)
    
    ' Create button
    btn1 = CreateWindowEx(0, "BUTTON", "Button #1", WS_VISIBLE Or _  
                                            WS_CHILD, 20, 10, 100, 30, hWnd, 0, 0, 0)
    
    ' Create edit box
    edt1 = CreateWindowEx(0, "EDIT", "Type text here...", ws_border Or _ 
                                            WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or _ 
                                            WS_VSCROLL Or ES_AUTOHSCROLL Or _ 
                                            ES_AUTOVSCROLL Or ES_MULTILINE, _ 
                                            20, 50, 200, 100, hWnd, 0, 0, 0)

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
   oldproc= SetWindowLong(edt1, GWL_WNDPROC, Cast(DWORD, @WndProc_edt1))
   'SetWindowPos(edt1, HWND_TOPMOST, 0, 0, 0, 0, _ 
    '              SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_FRAMECHANGED)
    
    InvalidateRect(edt1, NULL, TRUE)
    InvalidateRect(hWnd, NULL, TRUE)
    EnableWindow(edt1, TRUE)
    
    SetWindowText(edt1, "texte editbox")
    
    showWindow(edt1, SW_HIDE)
    showWindow(edt1, SW_SHOW)
    Print "IsWindowVisible", IsWindowVisible(edt1)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    While GetMessage(@msg, 0, 0, 0)
        TranslateMessage(@msg)
        DispatchMessage(@msg)
        
        Select Case msg.hwnd
            Case hWnd
                Select Case msg.message
                    Case 273
                        End           
                    
                    ' If left mouse button was pressed in window area then
                    ' check if is edit box text = "". If it is then set
                    ' the edit box text to "Type text here" and set focus
                    ' to the window
                    Case WM_LBUTTONDOWN
                        Dim As ZString*1024 txt
                    
                        GetWindowText(edt1, txt, SizeOf(txt))
                        If txt = "" Then 
                            SetWindowText(edt1, "Type text here...")
                        EndIf
                    
                        SetFocus(hWnd)  ' Set focus to the window
                    Case Else
                        ' Create rect variable and store window size in it
                        Dim As RECT rct: GetClientRect( hWnd, @rct )
                        
                        ' Resize the edit box
                        MoveWindow( edt1, 20, 50, rct.right-40, rct.bottom-60, TRUE )
                End Select
            Case btn1
                Select Case msg.message
                    ' If left mouse button was pressed in button area then
                    ' check if is edit box text = "". If it is then set
                    ' the edit box text to "Type text here"
                    Case WM_LBUTTONDOWN
                        ' When button is pressed set the text
                        ' of button to "pressed"
                        SetWindowText( btn1, "Clicked!" )
                        
                        Dim As ZString*1024 txt
                        
                        GetWindowText( edt1, txt, SizeOf( txt ) )
                        If txt = "" Then 
                            SetWindowText( edt1, "Type text here..." )
                        EndIf
                    
                    ' If left mouse button was released from the button area
                    ' then set the button text to "Button #1" and show
                    ' massage box with text from text box
                    Case WM_LBUTTONUP
                        SetWindowText( btn1, "Button #1" )
                        
                        Dim As ZString*1024 txt
                        
                        GetWindowText( edt1, txt, SizeOf( txt ) )
                        MessageBox( hWnd, txt, "Hello", MB_OK )
                End Select
            Case edt1
                Select Case msg.message
                    ' When textbox was pressed then clar the textbox text if
                    ' text = "Type text here..."
                    Case WM_LBUTTONDOWN
                        Dim As ZString * 1024 txt
                    
                        GetWindowText(edt1, txt, SizeOf(txt))
                        If txt = "Type text here..." Then 
                            SetWindowText(edt1, "")
                        EndIf
                        
                    Case WM_keydown
                        Print "boucle messages clavier", msg.wparam, msg.lparam
                        
                End Select
        End Select
    Wend
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 
spartacus13012
Posts: 18
Joined: Nov 30, 2014 12:37
Location: FRANCE

Re: Control disappearance after SetWindowLong

Post by spartacus13012 »

Thank you for your help

the final code is as follows and it works perfectly

Code: Select all

	Function WndProc_edt1(ByVal hWnd As HWND, _ 
                 			    ByVal message As UINT, _
                 			    ByVal wParam As WPARAM, _
                 			    ByVal lParam As LPARAM) As LRESULT
   	        Function = 0

  	 	CallWindowProc(CAST(ANY PTR, b_oldProc), hwnd, message, wparam, lparam)
  	 	
  	 	Function = DefWindowProc(hWnd, message, wParam, lParam)
	End Function
Post Reply