Tech Support > Microsoft Windows > Development Resources > can't check treeview checkboxes - what am I missing?
can't check treeview checkboxes - what am I missing?
Posted by dan on December 30th, 2006


I need the program to check/uncheck treeview checkboxes. The checkboxes
appear on the treeview. I can check/uncheck them by hand. But I can't
get TreeView_SetCheckState to check them. I compile with mingw
cross-compiler. I included a test program below.

Thanks,
Daniel Goldman

#define _WIN32_IE 0x0500

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>

HWND Tree_g;
HWND MainWin_g ;
HINSTANCE HInstance_g ;

#if !defined TreeView_SetCheckState
#define TreeView_SetCheckState(hwndTV, hti, fCheck) \
TreeView_SetItemState(hwndTV, hti, \
INDEXTOSTATEIMAGEMASK((fCheck)?2:1), TVIS_STATEIMAGEMASK)
#endif /* #if !defined TreeView_SetCheckState */

LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
HTREEITEM hItem ;
TVITEM tvi;
TVINSERTSTRUCT tvis;

switch (message)
{
case WM_CREATE:
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

TreeView_DeleteAllItems (Tree_g);

memset (&tvis, 0, sizeof (tvis));
memset (&tvi, 0, sizeof (tvi));
tvi.mask |= TVIF_TEXT;
tvi.pszText = "1";

tvis.hParent = TVI_ROOT;
tvis.hInsertAfter = TVI_LAST;
tvis.item = tvi;

hItem = TreeView_InsertItem (Tree_g, &tvis);
TreeView_SetCheckState (Tree_g, hItem, TRUE);

EndPaint (hwnd, &ps) ;

return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("chkbox2") ;
MSG msg ;
WNDCLASS wndclass ;
DWORD dwStyle;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

HInstance_g = hInstance;
RegisterClass (&wndclass);

InitCommonControls();

MainWin_g = CreateWindow (szAppName, TEXT ("chkbox2"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;

Tree_g = CreateWindow (WC_TREEVIEW, NULL,
TVS_HASBUTTONS | WS_VISIBLE | WS_BORDER | WS_CHILD,
10, 10, 100, 100, MainWin_g, (HMENU) 3, HInstance_g, NULL) ;

dwStyle = (DWORD) GetWindowLongPtr (Tree_g, GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;
SetWindowLongPtr (Tree_g, GWL_STYLE, (LONG_PTR) dwStyle);

ShowWindow (MainWin_g, iCmdShow) ;
UpdateWindow (MainWin_g) ;

while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

Posted by Barry on December 30th, 2006



"dan" <dagoldman@yahoo.com> wrote in message
news:1167464237.609656.183530@v33g2000cwv.googlegr oups.com...
That seems to work on my system.



Posted by dan on December 30th, 2006


Barry wrote:
Thanks for testing. Do you mean the checkbox is checked when program
starts? All by itself? That's what I'm trying to do.

What compiler are you using? Today I compiled directly on Windows
(mingw) and checkbox still not checked. Maybe it's a problem with
mingw.

Could anyone else help verify by running and see if checkbox is
checked? And let me know what compiler you used?

Thanks,
Daniel


Posted by Barry on December 30th, 2006



"dan" <dagoldman@yahoo.com> wrote in message
news:1167502246.853885.211510@73g2000cwn.googlegro ups.com...
The box was checked when the program started and unchecked
when I changed you statement to check it to FALSE.

I was using gcc 3.4.2 (mingw special) compiled and executed on ME.

One thing I have noticed that show up using mingw with XP systems
as a target is the necessity to explicitly initalise the structures. But,
your
problem may be your cross compile environment.




Posted by Barry on December 30th, 2006



"Barry" <barryg@nullhighstream.net> wrote in message
news:12pdeer1kv48ee1@corp.supernews.com...
In case it wasn't clear my comment on explicitly initilize in your bit
of code would start with a memset, ZeroMemory of the tvis struct
and then making sure you fill all fields.



Posted by Barry on December 30th, 2006



"Barry" <barryg@nullhighstream.net> wrote in message
news:12pdeuj4iatkpa1@corp.supernews.com...
Never mind :-). I see you did this when you smacked tvi.



Posted by dan on December 30th, 2006



Barry wrote:
The linux cross-compiler says gcc version 3.4.2 (mingw-special)
The windows compiler says gcc version 3.2.3 (mingw special 20030504-1)

Thanks for being so detailed (more detailed than I was) about compiler
version. My windows mingw is out-of-date. Maybe something got fixed
between versions. I'll download the current 3.4.2 windows mingw and
try.

Daniel


Posted by dan on December 30th, 2006


dan wrote:
I downloaded the current 3.4.2 windows mingw. The box is checked now.
I'll go elsewhere to figure out why the cross-compiler doesn't check
the box.

Thanks for solving problem.

Daniel


Posted by Barry on December 30th, 2006



"dan" <dagoldman@yahoo.com> wrote in message
news:1167515741.267703.102260@s34g2000cwa.googlegr oups.com...
I didn't do anything that solved your problem.

You did the exact thing someone should do when asking for
help on a newsgroup of this sort. You posted a small
compilable program which exhibited the problem. I just checked
it in one similar environment for you :-). Little help like that is
often a good start.

Barry



Posted by dan on February 3rd, 2007


On Dec 30 2006, 2:30 pm, "Barry" <bar...@nullhighstream.net> wrote:
I upgraded my debian linux computer from sarge to etch (a bother
because dist-upgrade did not work), which let me install the newest
3.4.5 version of mingw cross-compiler (new version wouldn't install to
sarge), which solved the problem (TreeView_SetCheckState works now).

Daniel



Similar Posts