⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple_checkbox.shtml.htm

📁 mfc资料集合5
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.0 [en] (WinNT; I) [Netscape]">
   <TITLE>CTreeCtrl: Adding Simple Checkbox for each Item</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><td>
</tr>
</table>


<CENTER>
<H3>
<FONT COLOR="#AOAO99">Adding Simple Checkbox for each Item</FONT></H3></CENTER>

<CENTER>
<H3>

<HR></H3></CENTER>
You can check items in a treeview control by using either an overlay image or a couple of state images. Both the techniques are fairly simple, but using a state image allows us to show a checkbox and makes it obvious to the user.

<P>In this section we allow an item to have only two states. Either it is checked or it is not. For a tree this is not always the best choice. We may also want imformation as to whether any of the child or descendant items are checked or not. We will cover that in the next topic.
<p><img src="simple_checkbox.gif" tppabs="http://www.codeguru.com/treeview/simple_checkbox.gif" border="1" width="154" height="85"></p>
<H4>Step 1: Create bitmap with checkbox images</H4>
Create a bitmap with an image of an unchecked checkbox and an image with a checked checkbox. Since we will be using this bitmap for the state image list, the first image will not be used. So the sequence of images in the bitmap is - blank image, empty checkbox and checked checkbox.
<P><IMG SRC="simple_check_bitmap.gif" tppabs="http://www.codeguru.com/treeview/simple_check_bitmap.gif" HEIGHT=15 WIDTH=41>
<BR>

<H4>Step 2: Initialize the state image list</H4>
Setting up state images has been covered in a previous topic. If the tree control class has a member variable m_imageState then here's how to set the image list.
<PRE><TT><FONT COLOR="#990000">
	m_tree.m_imageState.Create( IDB_STATE, 13, 1, RGB(255,255,255) );
	m_tree.SetImageList( &(m_tree.m_imageState), TVSIL_STATE );
</FONT></TT></PRE>
<BR>
<H4>Step 3: Insert items with the checkbox as a state image</H4>
If you are using TV_INSERTSTRUCT to insert an item into the tree control, then specify the state and the stateMask in the TV_ITEM member. The index of the state image is specified by using the macro INDEXTOSTATEIMAGEMASK. You can also use the SetItemState() function.

<P><PRE><TT><FONT COLOR="#990000">
SetItemState( hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK );
</FONT></TT></PRE>
<BR>

<H4>Step 4: Add code to OnLButtonDown to toggle checkbox</H4>
When the user clicks on the checkbox, then the checkbox state has to be toggled. We use the HitTest() function to determine if the click was on the checkbox. When we get the state image index to determine whether the item is checked or not, the GetItemState() function returns the value in the form used internally by the tree control. Shifting the value by 12 bit places to the right gives us the proper index value. We then toggle the state of the item.

<PRE><TT><FONT COLOR="#990000">
void CTreeCtrlX::OnLButtonDown(UINT nFlags, CPoint point) 
{
	UINT uFlags=0;
	HTREEITEM hti = HitTest(point,&uFlags);

	if( uFlags & TVHT_ONITEMSTATEICON )
	{
		int iImage = GetItemState( hti, TVIS_STATEIMAGEMASK )>>12;
		SetItemState( hti, INDEXTOSTATEIMAGEMASK(iImage == 1 ? 2 : 1), 
					TVIS_STATEIMAGEMASK );
		return;
	}
}
</FONT></TT></PRE>
<BR>

<H4>Step 5: Add code to OnKeyDown to toggle checkbox</H4>
This step provides the keyboard interface for checking and unchecking an item. The key used is usually the space key, so we will use the space key. The code is nearly the same as in the previous step. Instead of using HitTest() to determine the item handle, we use the function GetSelectedItem().

<PRE><TT><FONT COLOR="#990000">
void CTreeCtrlX::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if( nChar == VK_SPACE )
	{
		HTREEITEM hti = GetSelectedItem();
		int iImage = GetItemState( hti, TVIS_STATEIMAGEMASK )>>12;
		SetItemState( hti, INDEXTOSTATEIMAGEMASK(iImage == 1 ? 2 : 1),
					TVIS_STATEIMAGEMASK );
		return;
	}
}
</FONT></TT></PRE>
<BR>

<H4>Step 6: Define helper functions</H4>
Define helper functions to determine whether an item is checked or not and to iterate through the checked items. The purpose of the functions is very clear from the function names.

<PRE><TT><FONT COLOR="#990000">
BOOL CTreeCtrlX::IsItemChecked(HTREEITEM hItem)
{
	return GetItemState( hItem, TVIS_STATEIMAGEMASK )>>12 == 2;
}

HTREEITEM CTreeCtrlX::GetFirstCheckedItem()
{
	for ( HTREEITEM hItem = GetRootItem(); hItem!=NULL; hItem = GetNextItem( hItem ) )
		if ( IsItemChecked(hItem) )
			return hItem;

	return NULL;
}

HTREEITEM CTreeCtrlX::GetNextCheckedItem( HTREEITEM hItem )
{
	for ( hItem = GetNextItem( hItem ); hItem!=NULL; hItem = GetNextItem( hItem ) )
		if ( IsItemChecked(hItem) )
			return hItem;

	return NULL;
}

HTREEITEM CTreeCtrlX::GetPrevCheckedItem( HTREEITEM hItem )
{
	for ( hItem = GetPrevItem( hItem ); hItem!=NULL; hItem = GetPrevItem( hItem ) )
		if ( IsItemChecked(hItem) )
			return hItem;

	return NULL;
}
</FONT></TT></PRE>
<BR>


<P>
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="../index.htm" tppabs="http://www.codeguru.com/">Goto HomePage</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; 1997 Zafir Anjum</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>
<CENTER><IMG SRC="../cgi/Count.cgi-ft=2&dd=E-df=tv_simple_checkbox.cnt" tppabs="http://www.codeguru.com/cgi/Count.cgi?ft=2&dd=E%7cdf=tv_simple_checkbox.cnt" ALIGN="BOTTOM" BORDER="0"></CENTER>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -