关于TREE的问题,定义结点的层级,有这个功能吗

udSoft2020 2004-08-23 10:34:03
要想建成如下的TREE,可以吗

厂长--A科长--A工段长--A班长
|_B科长
|________B工段长__B班长
|_C科长___________C班长
|________________D班长

就象图中的“B工段长”,想直接归“厂长”管,但他的层级是在第3层

也就是说 增加一个结点时 ,可以定义这个结点在第几层。
例如:11 的上层是1,默认来说,11是放在1的下一层
但是我想把11设置成第3层!

不知道意思说清楚了没有?
有办法吗解决吗?

----在线等----

...全文
260 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
udSoft2020 2004-08-27
  • 打赏
  • 举报
回复
汗ing...
zyl910 2004-08-27
  • 打赏
  • 举报
回复

Requesting item-specific notifications
If your application returns CDRF_NOTIFYITEMDRAW to the initial prepaint custom draw notification, the control will send notifications for each item it draws during that paint cycle. These item-specific notifications will have the CDDS_ITEMPREPAINT value in the dwDrawStage member of the accompanying NMCUSTOMDRAW structure. You can request that the control send another notification when it is finished drawing the item by returning CDRF_NOTIFYPOSTPAINT to these item-specific notifications. Otherwise, return CDRF_DODEFAULT and the control will not notify the parent window until it starts to draw the next item.

Drawing the item yourself
If your application draws the entire item, return CDRF_SKIPDEFAULT. This allows the control to skip items that it does not need to draw, thereby decreasing system overhead. Keep in mind that returning this value means the control will not draw any portion of the item.

Changing fonts and colors
Your application can use custom draw to change an item's font. Simply select the HFONT you want into the device context specified by the hdc member of the NMCUSTOMDRAW structure associated with the custom draw notification. Since the font you select might have different metrics than the default font, make sure you include the CDRF_NEWFONT bit in the return value for the notification message. For more information on using this functionality, see the sample code in Using Custom Draw. The font that your application specifies is used to display that item when it is not selected. Custom draw does not allow you to change the font attributes for selected items.

To change text colors for all controls that support custom draw except for the list view and tree view, simply set the desired text and background colors in the device context supplied in the custom draw notification structure with theSetTextColor andSetBkColor functions. To modify the text colors in the list view or tree view, you need to place the desired color values in the clrText and clrTextBk members of the NMLVCUSTOMDRAW or NMTVCUSTOMDRAW structure.

Using Custom Draw
The following application-defined function processes custom draw notification messages sent by a child list view control. Upon receiving the prepaint notification (CDDS_PREPAINT), the function requests item-specific notifications by returning CDRF_NOTIFYITEMDRAW. When it receives the subsequent item-specific notifications, it selects a previously created font into the provided device context and specifies new colors before returning CDRF_NEWFONT.

LRESULT DoNotify(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;

switch (pnm->hdr.code){
case NM_CUSTOMDRAW:{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;

/*
CDDS_PREPAINT is at the beginning of the paint cycle. You
implement custom draw by returning the proper value. In this
case, we're requesting item-specific notifications.
*/
if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
// Request prepaint notifications for each item.
return CDRF_NOTIFYITEMDRAW;

/*
Because we returned CDRF_NOTIFYITEMDRAW in response to
CDDS_PREPAINT, CDDS_ITEMPREPAINT is sent when the control is
about to paint an item.
*/
if(lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT){
/*
To change the font, select the desired font into the
provided HDC. We're changing the font for every third item
in the control, starting with item zero.
*/
if(!(lplvcd->nmcd.dwItemSpec % 3))
SelectObject(lplvcd->nmcd.hdc, g_hNewFont);
else
return(CDRF_DODEFAULT);

/*
To change the text and background colors in a list view
control, set the clrText and clrTextBk members of the
NMLVCUSTOMDRAW structure to the desired color.

This differs from most other controls that support
CustomDraw. To change the text and background colors for
the others, call SetTextColor and SetBkColor on the provided HDC.
*/
lplvcd->clrText = RGB(150, 75, 150);
lplvcd->clrTextBk = RGB(255,255,255);

/*
We changed the font, so we're returning CDRF_NEWFONT. This
tells the control to recalculate the extent of the text.
*/
return CDRF_NEWFONT;
}
}

default:
break;
}

return 0;
}








Custom Draw是Windows的TreeView具有的功能
但是MicroSoft将其封装成VB控件的时候没有提供这一功能
zyl910 2004-08-27
  • 打赏
  • 举报
回复
Custom Draw


Custom draw is not a common control; it is a service that many common controls provide. Custom draw services allow an application greater flexibility in customizing a control's appearance. Your application can harness custom draw notifications to easily change the font used to display items or manually draw an item without having to do a full-blown owner draw.

About Custom Draw


Using Custom Draw


Custom Draw Reference


About Custom Draw
This section contains general information about custom draw functionality and provides a conceptual overview of how an application can support custom draw.

Currently, the following controls support custom draw functionality:

Header controls
List view controls
Rebar controls
Toolbar controls
Tooltip controls
Trackbar controls
Tree view controls
Note Custom draw is implemented in version 4.70 and later of Comctl32.dll.

About Custom Draw Notification Messages
All common controls that support custom draw send NM_CUSTOMDRAW notification messages at specific points during drawing operations. These notifications describe drawing operations that apply to the entire control as well as drawing operations specific to items within the control. Like many notification messages, NM_CUSTOMDRAW notifications are sent as WM_NOTIFY messages.

The lParam parameter of a custom draw notification message will be the address of an NMCUSTOMDRAW structure or a control-specific structure that contains an NMCUSTOMDRAW structure as its first member. The following table illustrates the relationship between the controls and the structures they use. Structure Used by
NMCUSTOMDRAW Rebar, trackbar, and header controls
NMLVCUSTOMDRAW List view controls
NMTBCUSTOMDRAW Toolbar controls
NMTTCUSTOMDRAW Tooltip controls
NMTVCUSTOMDRAW Tree view controls


Paint Cycles, Drawing Stages, and Notification Messages
Like all Microsoft® Windows® applications, common controls periodically paint and erase themselves based on messages received from the system or other applications. The process of a control painting or erasing itself is called a paint cycle. Controls that support custom draw send NM_CUSTOMDRAW notification messages periodically through each paint cycle. This notification message is accompanied by an NMCUSTOMDRAW structure or another structure that contains an NMCUSTOMDRAW structure as its first member.

One piece of information that the NMCUSTOMDRAW structure contains is the current stage of the paint cycle. This is referred to as the draw stage and is represented by the value in the structure's dwDrawStage member. A control informs its parent about four basic draw stages. These basic, or global, draw stages are represented in the structure by the following flag values (defined in Commctrl.h). Global draw stage values
CDDS_PREPAINT Before the paint cycle begins.
CDDS_POSTPAINT After the paint cycle is complete.
CDDS_PREERASE Before the erase cycle begins.
CDDS_POSTERASE After the erase cycle is complete.


Each of the preceding values can be combined with the CDDS_ITEM flag to specify draw stages specific to items. For convenience, Commctrl.h contains the following item-specific values. Item-specific draw stage values
CDDS_ITEMPREPAINT Before an item is drawn.
CDDS_ITEMPOSTPAINT After an item has been drawn.
CDDS_ITEMPREERASE Before an item is erased.
CDDS_ITEMPOSTERASE After an item has been erased.


Your application must process the NM_CUSTOMDRAW notification message and then return a specific value that informs the control what it must do. See the following sections for more information about these return values.

Taking Advantage of Custom Draw Services
The key to harnessing custom draw functionality is in responding to the NM_CUSTOMDRAW notification messages that a control sends. The return values your application sends in response to these notifications determine the control's behavior for that paint cycle.

This section contains information about how your application can use NM_CUSTOMDRAW notification return values to determine the control's behavior. Details are broken into the following topics:

Responding to the prepaint notification
Requesting item-specific notifications
Drawing the item yourself
Changing fonts and colors
Responding to the prepaint notification
At the beginning of each paint cycle, the control sends the NM_CUSTOMDRAW notification message, specifying the CDDS_PREPAINT value in the dwDrawStage member of the accompanying NMCUSTOMDRAW structure. The value that your application returns to this first notification dictates how and when the control sends subsequent custom draw notifications for the rest of that paint cycle. Your application can return a combination of the following flags in response to the first notification. Return value Effect
CDRF_DODEFAULT The control will draw itself. It will not send additional NM_CUSTOMDRAW notifications for this paint cycle. This flag cannot be used with any other flag.
CDRF_NOTIFYITEMDRAW The control will notify the parent of any item-specific drawing operations. It will send NM_CUSTOMDRAW notification messages before and after it draws items.
CDRF_NOTIFYPOSTPAINT The control will send an NM_CUSTOMDRAW notification when the painting cycle for the entire control is complete.
CDRF_SKIPDEFAULT The control will not perform any painting at all.

zyl910 2004-08-27
  • 打赏
  • 举报
回复
A:找支持层级显示显示的控件
B:自己编支持层级显示显示的控件,用Label、Image、Line、Shape拼出来就行了
C:自绘TreeView,Custom Draw技术,实现起来最麻烦,但效果最完美



NM_CUSTOMDRAW (tree view)


NM_CUSTOMDRAW
lpNMCustomDraw = (LPNMTVCUSTOMDRAW) lParam;

Sent by a tree view control to notify its parent window about drawing operations. This notification is sent in the form of a WM_NOTIFY message.

The value your application can return depends on the current drawing stage. The dwDrawStage member of the associated NMCUSTOMDRAW structure holds a value that specifies the drawing stage. You must return one of the following values:
When dwDrawStage equals CDDS_PREPAINT: CDRF_DODEFAULT
The control will draw itself. It will not send any additional NM_CUSTOMDRAW messages for this paint cycle.
CDRF_NOTIFYITEMDRAW
The control will notify the parent of any item-related drawing operations. It will send NM_CUSTOMDRAW notification messages before and after drawing items.
CDRF_NOTIFYITEMERASE
The control will notify the parent when an item will be erased. It will send NM_CUSTOMDRAW notification messages before and after erasing items.
CDRF_NOTIFYPOSTERASE
The control will notify the parent after erasing an item.
CDRF_NOTIFYPOSTPAINT
The control will notify the parent after painting an item.
CDRF_NOTIFYSUBITEMDRAW
Version 4.71. The control will notify the parent when a list view subitem is being drawn.


When dwDrawStage equals CDDS_ITEMPREPAINT: CDRF_NEWFONT
Your application specified a new font for the item; the control will use the new font. For more information on changing fonts, see Changing fonts and colors.
CDRF_SKIPDEFAULT
Your application drew the item manually. The control will not draw the item.
TVCDRF_NOIMAGES
Version 4.71. The control will not draw the image for the item.


lpNMCustomDraw
Address of a NMTVCUSTOMDRAW structure that contains and receives information about the drawing operation. The dwItemSpec member of the nmcd member of this structure contains the handle of the item being drawn. The lItemlParam member of the nmcd member of this structure contains the lParam of the item being drawn.
Version 4.70

See also Using Custom Draw


udSoft2020 2004-08-27
  • 打赏
  • 举报
回复
有人解决吗?
udSoft2020 2004-08-24
  • 打赏
  • 举报
回复
那就是无法实现了

楼上的代码我试过了,就是点击节点,可以显示现在点击节点属第几层

但是没办法直观地看出来 节点 在第几层,节点只能在下一层

只能这样了吗
flyingZFX 2004-08-23
  • 打赏
  • 举报
回复
没有必要这样做,,
LGYAN 2004-08-23
  • 打赏
  • 举报
回复
没有
udSoft2020 2004-08-23
  • 打赏
  • 举报
回复
有别的办法吗
落伍者 2004-08-23
  • 打赏
  • 举报
回复
设置一个特殊的第二层,仍然把"B工段长"放在第三层.凡是处在特殊的第二层的下级都直接归
厂长管
online 2004-08-23
  • 打赏
  • 举报
回复
也只能加空格处理了
不过只是看起来像三层的节点而已
LGYAN 2004-08-23
  • 打赏
  • 举报
回复
你把"B工段长"的父级设为"厂长",而在"B工段长"前加足够的空格不就可以了?
starsoulxp 2004-08-23
  • 打赏
  • 举报
回复
//就象图中的“B工段长”,想直接归“厂长”管,但他的层级是在第3层

使用 Treeview 实现吗?有点难。可以在视图上实现在第三层,具体归谁管还得在代码中控制,视图上没法实现,会有冲突的。
rainstormmaster 2004-08-23
  • 打赏
  • 举报
回复
呵呵,没看懂我的代码的意思吧:node该怎么添加还怎么添加,用tag属性保存附加信息

不然的话,没有第2层是无法添加一个节点使它位于第3层的,这是和盖楼一样的道理
udSoft2020 2004-08-23
  • 打赏
  • 举报
回复
李版主在吗...
udSoft2020 2004-08-23
  • 打赏
  • 举报
回复
结果还是画不那种效果呀

没有办法了吗
loverpyh 2004-08-23
  • 打赏
  • 举报
回复
up
rainstormmaster 2004-08-23
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Form_Load()
Dim mnode As Node, node1 As Node, node2 As Node, node3 As Node
Set mnode = Me.TreeView1.Nodes.Add(, , , "厂长")
mnode.Tag = "1"
Set node1 = Me.TreeView1.Nodes.Add(mnode, tvwChild, , "A科长")
node1.Tag = "2"
Set node2 = Me.TreeView1.Nodes.Add(node1, tvwChild, , "A工段长")
node2.Tag = "3"
Set node3 = Me.TreeView1.Nodes.Add(node2, tvwChild, , "A班长")
node3.Tag = "4"
Set node1 = Me.TreeView1.Nodes.Add(mnode, tvwChild, , "B科长")
node1.Tag = "2"
Set node1 = Me.TreeView1.Nodes.Add(mnode, tvwChild, , "B工段长")
node1.Tag = "3"
Set node2 = Me.TreeView1.Nodes.Add(node1, tvwChild, , "B班长")
node2.Tag = "4"
Set node1 = Me.TreeView1.Nodes.Add(mnode, tvwChild, , "C科长")
node1.Tag = "2"
Set node2 = Me.TreeView1.Nodes.Add(node1, tvwChild, , "C班长")
node2.Tag = "4"
Set node1 = Me.TreeView1.Nodes.Add(mnode, tvwChild, , "D班长")
node1.Tag = "4"
Dim a As Node
For Each a In Me.TreeView1.Nodes
a.Expanded = True
Next
End Sub

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
If Not Node.Parent Is Nothing Then
MsgBox "等级:" + Node.Tag + ",直属领导:" + Node.Parent.Text
Else
MsgBox "等级:" + Node.Tag
End If
End Sub

1,453

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧