This one always crashes my app

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Hi all,

I want to create a TreeView Control on my own, becourse i want to
enable drag&drop which seems to be disabled in TreeGadget().

I read the TreeView Stuff in the Platform SDK, and also found an example there, wich i translated to PureBasic. But it always crashes
at the SendMessage_() command.

I allready have some experience in adding/deleting items (see viewtopic.php?t=1929), so this part can not be the problem.

Here's the code:

Code: Select all

#WC_TREEVIEW.s = "SysTreeView"
#ID_TREEVIEW = 107
#Flags = #WS_VISIBLE | #WS_CHILD | #WS_BORDER | #TVS_HASLINES
  
Structure TVITEM
  mask.l
  hItem.l
  state.l
  stateMask.l
  pszText.l
  cchTextMax.l
  iImage.l
  iSelectedImage.l
  cChildren.l
  lParam.l
EndStructure
  
Structure TVINSERTSTRUCT
  hParent.l   
  hInsertAfter.l
  item.TVITEM
EndStructure
  
hWnd = OpenWindow(0, 0, 0, 640, 480, #PB_Window_SystemMenu, "")
  
; There must be some fault! This is the example code from SDK:
; -----------------------------------------------------------
;    hwndTV = CreateWindowEx(0, WC_TREEVIEW, "Tree View", 
;        WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 
;        0, 0, 640, 480, 
;        hwndParent, (HMENU) ID_TREEVIEW, g_hinst, NULL); 
hwndTV = CreateWindowEx_(0, #WC_TREEVIEW, "Tree View", #Flags, 0, 0, 640, 480, hWnd, #ID_TREEVIEW, GetModuleHandle_(0), #NULL)
  
text.s = "Hello World!"          ; This stuff used to work with a TreeGadget()
lpis.TVINSERTSTRUCT
lpis\hParent = #TVI_ROOT
lpis\hInsertAfter = #TVI_ROOT
lpis\item\mask = #TVIF_TEXT
lpis\item\pszText = @text
lpis\item\cchTextMax = Len(text)
hItem = SendMessage_(hwndTV, #TVM_INSERTITEM, 0, @lpis)  ; prog crashes here!
   
   
Repeat                                             
Until WaitWindowEvent() = #PB_EventCloseWindow
End
Hope anyone can help me here...

Timo

--------------------------------
Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs and the universe trying to produce bigger and better idiots.

...So far, the universe is winning.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

2 errors:
1.) the class is named "SysTreeView32", NOT "SysTreeView"
2.) Initialize the Common Controls with InitCommonControls_()
(this makes sure the DLL is loaded, so you can access the class)

Code: Select all

#WC_TREEVIEW.s = "SysTreeView32"
#ID_TREEVIEW = 107
#Flags = #WS_VISIBLE | #WS_CHILD | #WS_BORDER | #TVS_HASLINES
  
Structure TVITEM
  mask.l
  hItem.l
  state.l
  stateMask.l
  pszText.l
  cchTextMax.l
  iImage.l
  iSelectedImage.l
  cChildren.l
  lParam.l
EndStructure
  
Structure TVINSERTSTRUCT
  hParent.l   
  hInsertAfter.l
  item.TVITEM
EndStructure
  
hWnd = OpenWindow(0, 0, 0, 640, 480, #PB_Window_SystemMenu, "")
  
; There must be some fault! This is the example code from SDK:
; -----------------------------------------------------------
;    hwndTV = CreateWindowEx(0, WC_TREEVIEW, "Tree View", 
;        WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES, 
;        0, 0, 640, 480, 
;        hwndParent, (HMENU) ID_TREEVIEW, g_hinst, NULL); 
InitCommonControls_()

hwndTV = CreateWindowEx_(0, #WC_TREEVIEW, "Tree View", #Flags, 0, 0, 640, 480, hWnd, #ID_TREEVIEW, GetModuleHandle_(0), #NULL)
  
text.s = "Hello World!"          ; This stuff used to work with a TreeGadget()
lpis.TVINSERTSTRUCT
lpis\hParent = #TVI_ROOT
lpis\hInsertAfter = #TVI_ROOT
lpis\item\mask = #TVIF_TEXT
lpis\item\pszText = @text
lpis\item\cchTextMax = Len(text)
hItem = SendMessage_(hwndTV, #TVM_INSERTITEM, 0, @lpis)  ; prog crashes here!
   
   
Repeat                                             
Until WaitWindowEvent() = #PB_EventCloseWindow
End
If you test something like this, you should add some checks -
like "hWndTV = CreateWindowEx_... : If hWndTV = 0 : ERROR(): EndIf"

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Thanks Danilo, it works fine now.

> If you test something like this, you should add some checks -
> like "hWndTV = CreateWindowEx_... : If hWndTV = 0 : ERROR(): EndIf"

The bad thing was that the wrong code also returned hWndTV other that 0 which confused me even more.

Anyway, thanks for the help...

Timo
Post Reply