Tree-List views 設計範例 |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
Tree-List viewsBy David Bridges 資料來源: http://www.bridgespublishing.com One of the big advantages that Borland C Builder has over other C development environments is its ease of building sophisticated user interfaces. Chances are that if you have developed any kind of database application, you’ve used the TListView class, which is a wrapper for the Windows ListView common control. Many times I’ve wanted the ListView control to do more than it does (it already does a lot, but I always want more!). Specifically, there are times when I need to display hierarchical data as in the TreeView control. However, one of the disadvantages of using the TreeView control is that it doesn’t support multiple columns of data. What I really want to have is a "TreeListView" control that does both. You can see an example of this kind of control in the C Builder Project Manager window (shown in Figure A). This is a "TreeListView" type of control—it’s a standard TreeView control but it also shows additional data in a column to the right. 8Figure A The TTreeView class allows creating TreeView controls with extended columns such as the Project Manager window. As it turns out, it’s not too hard to do this. All that’s needed is a little custom drawing and a header control that’s associated with the TreeView. The TTLVController class As I have often done before, I have implemented my new control type as a "Controller" class—a non-visual component that binds other objects together and handles interaction between them. To use this class, you simply create an instance of it and bind it to an existing TTreeView and THeaderControl, and it automatically adds the "TreeListView" behavior. This is demonstrated in Listing A. Figure B shows the TreeListView in action. When the header columns are resized, the column text moves along with it, just as in a ListView. Figure B A winow created by TTreeListView. Now let’s take a look at what the TTLVController class does. In its constructor, it "binds" itself to a TTreeView and a THeaderControl by installing its own handlers for the TreeView’s OnAdvancedCustomDraw event and the Header’s OnSectionTrack event. The HeaderSectionTrack function simply invalidates the TreeView, causing it to re-draw. This ensures that the text will always be redisplayed after resizing a column header. The AdvancedCustomDrawItem function is where most of the work takes place. The OnAdvancedCustomDrawItem event of TTreeView allows you to write custom code for drawing a TreeView item. I have chosen to take advantage of some of the default drawing of TTreeView, such as the image icons. Therefore, the function only draws the text when the DrawStage is cdPostPaint. This will have the effect of drawing on top of what has already been drawn for the list view item. Having a good deal of experience with the Windows GDI functions, I’ve chosen to grab the device context handle of the TreeView and use the GDI functions to do the drawing, rather than using the TCanvas functions. In a nutshell, the function goes through each column of the header control and calculates the rectangular area to be drawn for the "cell" of data. Then it decides what foreground and background color to use, then fires the OnGetItemInfo event to get the actual data to be drawn. Implementing the "data getter" using an event allows the component to be independent of any application logic, and allows it to be easily reused. Conclusion TTLVController is a relatively simple component, which can add some nice functionality to the TTreeView class, and demonstrates the use of application independent code for extending user interface controls. Listing A#includeCopyright © 2002, Bridges Publishing. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Bridges Publishing is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners. 網路志工聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2003/01/07 12:16:30 |
skcc
中階會員 發表:36 回覆:96 積分:88 註冊:2002-08-28 發送簡訊給我 |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
引言: 謝謝 axsoft 大大分享。不過小弟依文件實作,好像有一些問題。問題是在執期間,若在 TTreeListView 元件 select 其中一項目後,若跳選另外一項目時,會出現四方的虛線在上一次所選的項目上。 其情況如下圖: 這問題可以解決嗎? Anthony Lee因Post出來的文章有掉Code的現象,您參考原始的文章試試! http://www.bridgespublishing.com/articles/0702/July02-02.htm 網路志工聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- |
skcc
中階會員 發表:36 回覆:96 積分:88 註冊:2002-08-28 發送簡訊給我 |
引言: 因Post出來的文章有掉Code的現象,您參考原始的文章試試! http://www.bridgespublishing.com/articles/0702/July02-02.htm 網路志工聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---謝謝 axsoft 大大,我自己看看吧。 Anthony Lee |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |