Discussion:
How can i set the style of a CTreeCtrl within a CTreeView ?
(too old to reply)
PRMARJORAM
2008-05-14 15:01:02 UTC
Permalink
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.

There is no easy way to do this, the documentation points to using
SetWindowLong.

void CMyTreeView::OnInitialUpdate()

{

CTreeView::OnInitialUpdate();

CTreeCtrl& tree = GetTreeCtrl();

HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);

ASSERT(tree.GetSafeHwnd() != NULL );

::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES |TVS_LINESATROOT);
}

Iv tried using this to set the styles as in the above code, but the program
gets corrupted.

How can i set the style of a CTreeCtrl nested in a CTreeView?
M. Shoaib Surya
2008-05-14 15:10:54 UTC
Permalink
You should call ModifyStyle() member function of CTreeCtrl to set the style.
The following line of code should achieve your purpose.

tree.ModifyStyle(NULL, TVS_HASLINES |TVS_LINESATROOT);

Regards,
Shoaib.
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES
|TVS_LINESATROOT);
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
M. Shoaib Surya
2008-05-14 15:18:25 UTC
Permalink
Also, the call to the OnInitialUpdate() of the base class, that is, the
following line of code,
CTreeView::OnInitialUpdate();
should be the last line of the overridden CMyTreeView::OnInitialUpdate()
instead of the first line

- Shoaib
Post by M. Shoaib Surya
You should call ModifyStyle() member function of CTreeCtrl to set the
style. The following line of code should achieve your purpose.
tree.ModifyStyle(NULL, TVS_HASLINES |TVS_LINESATROOT);
Regards,
Shoaib.
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES
|TVS_LINESATROOT);
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
PRMARJORAM
2008-05-14 16:03:00 UTC
Permalink
Thanks ill look into that modify style, looks alot better...

I have cracked it though now, needed to do the following
LONG style = ::GetWindowLong(tree.GetSafeHwnd(),GWL_STYLE);

::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE, style |
TVS_HASLINES |TVS_LINESATROOT);

So needed to combine the current style with the additions.
Post by M. Shoaib Surya
Also, the call to the OnInitialUpdate() of the base class, that is, the
following line of code,
CTreeView::OnInitialUpdate();
should be the last line of the overridden CMyTreeView::OnInitialUpdate()
instead of the first line
- Shoaib
Post by M. Shoaib Surya
You should call ModifyStyle() member function of CTreeCtrl to set the
style. The following line of code should achieve your purpose.
tree.ModifyStyle(NULL, TVS_HASLINES |TVS_LINESATROOT);
Regards,
Shoaib.
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES
|TVS_LINESATROOT);
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
Ali BEN MAHMOUD
2011-11-21 11:29:56 UTC
Permalink
Thank Shoaib.
Your response is exactly what I am seeking for :)
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
Post by M. Shoaib Surya
You should call ModifyStyle() member function of CTreeCtrl to set the style.
The following line of code should achieve your purpose.
tree.ModifyStyle(NULL, TVS_HASLINES |TVS_LINESATROOT);
Regards,
Shoaib.
Post by M. Shoaib Surya
Also, the call to the OnInitialUpdate() of the base class, that is, the
following line of code,
CTreeView::OnInitialUpdate();
should be the last line of the overridden CMyTreeView::OnInitialUpdate()
instead of the first line
- Shoaib
Post by PRMARJORAM
Thanks ill look into that modify style, looks alot better...
I have cracked it though now, needed to do the following
LONG style = ::GetWindowLong(tree.GetSafeHwnd(),GWL_STYLE);
TVS_HASLINES |TVS_LINESATROOT);
So needed to combine the current style with the additions.
Post by Joseph M. Newcomer
See below...
****
(a) this is not good style for setting styles. You have cleared ALL the style bits,
EXCEPT the two you set. At the VERY MINIMUM this code should say
DWORD style = tree.GetStyle();
style |= TVS_HASLINES | TVS_LINESATROOT;
but why go through all that work? Instead, just call
tree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT);
which should be sufficient (no need to get the old style, etc.)
joe
****
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Post by PRMARJORAM
Exactly,
i was looking for a method something like SetStyle, then looking at the msdn
documentation for using CTreeCtrl, stated had to use SetWindowLong
I could not believe they had left something so fundamental out of the class
interface, they had not...
Post by David Connet
BOOL CMyTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= TVS_HASLINES | TVS_LINESATROOT;
return __super::PreCreateWindow(cs);
}
Some styles need to be done in OnInitialUpdate (or OnCreate)
[I just copied some listview code for an example]
GetListCtrl().SetExtendedStyle(GetListCtrl().GetExtendedStyle() |
LVS_EX_FULLROWSELECT);
Dave Connet
Post by Joseph M. Newcomer
They didn't. This is a documentation error, and I will add it to my list of documentation
errors.
joe
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer
2008-05-14 16:42:27 UTC
Permalink
See below...
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES |TVS_LINESATROOT);
****
(a) this is not good style for setting styles. You have cleared ALL the style bits,
EXCEPT the two you set. At the VERY MINIMUM this code should say

DWORD style = tree.GetStyle();
style |= TVS_HASLINES | TVS_LINESATROOT;
::SetWindowLong(tree.GetSafeHwnd(), GWL_STYLE, style);

but why go through all that work? Instead, just call

tree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT);

which should be sufficient (no need to get the old style, etc.)
joe
****
Post by PRMARJORAM
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
PRMARJORAM
2008-05-14 17:40:01 UTC
Permalink
Exactly,

i was looking for a method something like SetStyle, then looking at the msdn
documentation for using CTreeCtrl, stated had to use SetWindowLong

I could not believe they had left something so fundamental out of the class
interface, they had not...
Post by Joseph M. Newcomer
See below...
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES |TVS_LINESATROOT);
****
(a) this is not good style for setting styles. You have cleared ALL the style bits,
EXCEPT the two you set. At the VERY MINIMUM this code should say
DWORD style = tree.GetStyle();
style |= TVS_HASLINES | TVS_LINESATROOT;
::SetWindowLong(tree.GetSafeHwnd(), GWL_STYLE, style);
but why go through all that work? Instead, just call
tree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT);
which should be sufficient (no need to get the old style, etc.)
joe
****
Post by PRMARJORAM
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer
2008-05-15 03:39:04 UTC
Permalink
They didn't. This is a documentation error, and I will add it to my list of documentation
errors.
joe
Post by PRMARJORAM
Exactly,
i was looking for a method something like SetStyle, then looking at the msdn
documentation for using CTreeCtrl, stated had to use SetWindowLong
I could not believe they had left something so fundamental out of the class
interface, they had not...
Post by Joseph M. Newcomer
See below...
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do. I need
to change the style of the nested CTreeCtrl.
There is no easy way to do this, the documentation points to using
SetWindowLong.
void CMyTreeView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
HTREEITEM root_item = tree.InsertItem(CString("MyProject"));
HTREEITEM init_request = tree.InsertItem(CString("Item 1"), root_item);
HTREEITEM doc_package = tree.InsertItem(CString("Item 2"), root_item);
ASSERT(tree.GetSafeHwnd() != NULL );
::SetWindowLong(tree.GetSafeHwnd(),GWL_STYLE,TVS_HASLINES |TVS_LINESATROOT);
****
(a) this is not good style for setting styles. You have cleared ALL the style bits,
EXCEPT the two you set. At the VERY MINIMUM this code should say
DWORD style = tree.GetStyle();
style |= TVS_HASLINES | TVS_LINESATROOT;
::SetWindowLong(tree.GetSafeHwnd(), GWL_STYLE, style);
but why go through all that work? Instead, just call
tree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT);
which should be sufficient (no need to get the old style, etc.)
joe
****
Post by PRMARJORAM
}
Iv tried using this to set the styles as in the above code, but the program
gets corrupted.
How can i set the style of a CTreeCtrl nested in a CTreeView?
Joseph M. Newcomer [MVP]
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
David Connet
2008-05-14 23:52:29 UTC
Permalink
Post by PRMARJORAM
Im using a MFC CTreeView which fits nicely with what im trying to do.
I need to change the style of the nested CTreeCtrl.
The easiest way is to override PreCreateWindow:

BOOL CMyTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= TVS_HASLINES | TVS_LINESATROOT;
return __super::PreCreateWindow(cs);
}

Some styles need to be done in OnInitialUpdate (or OnCreate)
[I just copied some listview code for an example]
GetListCtrl().SetExtendedStyle(GetListCtrl().GetExtendedStyle() |
LVS_EX_FULLROWSELECT);

Dave Connet
Loading...