第三步 設置風格
1、 通過第一步和第二步,我們已經創建出了一個有一行並且有四列的內容的REPORT風格的List Control。可是,只是簡單的方框中有一行數據而已,我們能不能讓它們看起來更漂亮呢?或者我們能不能讓它們可以被選取從而讓我們可以做一些別的操作呢?當然可以,這一步我們就來看看簡單的風格設置。
2、 要學習風格設置,我們首先來看一個函數:
CListCtrl::SetExtendedStyle
DWORD SetExtendedStyle(DWORD dwNewStyle)
返回值:
返回列表視圖控件使用的以前擴展風格的組合。
參數:
dwNewStyle |
列表視圖控件使用的擴展風格的組合。若希望進一步了解這些風格,請參閱聯機文檔“平台SDK”中的“擴展列表視圖風格” |
說明:
該成員函數實現了Win32宏ListView_SetExtentedListViewStyle的功能,如同聯機文檔“平台SDK”中所描述的。
對了,這就是我們需要的函數,只要調用它,我們就要可以設置風格了。下面我們一起來試試吧。
3、只是這個函數的參數怎么給呢?只能求助於MSDN了,看看我從MSDN中找到了什么吧。
Windows Vista. Automatically arrange icons if no icon positions have been set (Similar to LVS_AUTOARRANGE).
Windows Vista. Automatically select check boxes on single click.
Windows Vista. Automatically size listview columns.
Version 4.71. Changes border color whan an item is selected, instead of highlighting the item.
Version 4.70. Enables check boxes for items in a list-view control. When set to this style, the control creates and sets a state image list with two images using DrawFrameControl. State image 1 is the unchecked box, and state image 2 is the checked box. Setting the state image to zero removes the check box.
Version 6.00 Check boxes are visible and functional with all list view modes except the tile view mode introduced in ComCtl32.dll version 6. Clicking a checkbox in tile view mode only selects the item; the state does not change.
You can obtain the state of the check box for a given item with ListView_GetCheckState. To set the check state, use ListView_SetCheckState. If this style is set, the list-view control automatically toggles the check state when the user clicks the check box or presses the space bar.
Indicates that an overflow button should be displayed in icon/tile view if there is not enough client width to display the complete set of header items. The list-view control sends the LVN_COLUMNOVERFLOWCLICK notification when the overflow button is clicked. This flag is only valid when LVS_EX_HEADERINALLVIEWS is also specified.
Windows Vista. Snap to minimum column width when the user resizes a column.
Version 6.00. Paints via double-buffering, which reduces flicker. This extended style also enables alpha-blended marquee selection on systems where it is supported.
Version 4.71. Enables flat scroll bars in the list view. If you need more control over the appearance of the list view's scroll bars, you should manipulate the list view's scroll bars directly using the Flat Scroll Bar APIs. If the system metrics change, you are responsible for adjusting the scroll bar metrics with FlatSB_SetScrollProp. See Flat Scroll Bars for further details.
Version 4.70. When an item is selected, the item and all its subitems are highlighted. This style is available only in conjunction with the LVS_REPORT style.
Version 4.70. Displays gridlines around items and subitems. This style is available only in conjunction with the LVS_REPORT style.
Version 4.70. Enables drag-and-drop reordering of columns in a list-view control. This style is only available to list-view controls that use the LVS_REPORT style.
Windows Vista. Show column headers in all view modes.
Windows Vista. Hides the labels in icon and small icon view.
Version 4.71. When a list-view control uses the LVS_EX_INFOTIP style, the LVN_GETINFOTIP notification message is sent to the parent window before displaying an item's ToolTip.
Windows Vista. Icons are lined up in columns that use up the whole view.
Version 5.80. If a partially hidden label in any list view mode lacks ToolTip text, the list-view control will unfold the label. If this style is not set, the list-view control will unfold partly hidden labels only for the large icon mode.
Version 4.71. If the list-view control has the LVS_AUTOARRANGE style, the control will not autoarrange its icons until one or more work areas are defined (see LVM_SETWORKAREAS). To be effective, this style must be set before any work areas are defined and any items have been added to the control.
Version 4.70. The list-view control sends an LVN_ITEMACTIVATE notification message to the parent window when the user clicks an item. This style also enables hot tracking in the list-view control. Hot tracking means that when the cursor moves over an item, it is highlighted but not selected. See the Extended List-View Styles Remarks section for a discussion of item activation.
Version 4.71. Sets the list view window region to include only the item icons and text using SetWindowRgn. Any area that is not part of an item is excluded from the window region. This style is only available to list-view controls that use the LVS_ICON style.
Version 6.00. In icon view, moves the state image of the control to the top right of the large icon rendering. In views other than icon view there is no change. When the user changes the state by using the space bar, all selected items cycle over, not the item with the focus.
Windows Vista. Not used.
Windows Vista. In icon view, icons automatically snap into a grid.
Version 4.70. Allows images to be displayed for subitems. This style is available only in conjunction with the LVS_REPORT style.
Version 4.70. Enables hot-track selection in a list-view control. Hot track selection means that an item is automatically selected when the cursor remains over the item for a certain period of time. The delay can be changed from the default system setting with a LVM_SETHOVERTIME message. This style applies to all styles of list-view control. You can check whether hot-track selection is enabled by calling SystemParametersInfo.
Windows Vista. Background is painted by the parent via WM_PRINTCLIENT.
Windows Vista. Enable shadow text on transparent backgrounds only.
Version 4.70. The list-view control sends an LVN_ITEMACTIVATE notification message to the parent window when the user double-clicks an item. This style also enables hot tracking in the list-view control. Hot tracking means that when the cursor moves over an item, it is highlighted but not selected. See the Extended List-View Styles Remarks section for a discussion of item activation.
Version 4.71. Causes those non-hot items that may be activated to be displayed with underlined text. This style requires that LVS_EX_TWOCLICKACTIVATE be set also. See the Extended List-View Styles Remarks section for a discussion of item activation.
Version 4.71. Causes those hot items that may be activated to be displayed with underlined text. This style requires that LVS_EX_ONECLICKACTIVATE or LVS_EX_TWOCLICKACTIVATE also be set. See the Extended List-View Styles Remarks section for a discussion of item activation.
這就是可以賦給形參的值。怎么找到呢?
4、 我們先在MSDN中查找ClistCtrl::SetExtendedStyle,找到后,我們發現里面有介紹它的形參dwNewStyle的,並且介紹中有這么一句話:For a descriptive list of these styles, see the Extended List View Styles topic in the Windows SDK.其中Extended List View Styles是可以點擊的,我們點擊它就得到了我們想要的內容。
5、 這么多,看起來好像無從下手的樣子,那我們就挑幾個常用的開始吧:
LVS_EX_FULLROWSELECT 選中某行使整行高亮(只適用與report風格的listctrl)
LVS_EX_GRIDLINES 網格線(只適用與report風格的listctrl)
LVS_EX_CHECKBOXES item前生成checkbox控件
將參數放入剛才介紹的函數中,就可以達到對應的效果了,不信嗎?現在就動手試試吧,記住別敲錯了哦。
6、 如果想幾種風格都要怎么辦?有過WINDOWS編程經驗的人一定馬上就知道了,把它們用“|”連接起來。用上述3個做例子:
SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES)
7、 那還等什么,趕快加入到我們前面的代碼中去吧。別忘了,加入的代碼應該是:
m_ListCtrl.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT|LVS_EX_CHECKBOXES);
8、 插入位置在哪里呢?當然還是那個老地方了,和前面兩步我們放代碼的地方一樣。在ClistCtrlDlg::OnInitDialog(void)中。那是放在我們前兩步代碼前還是后呢?前后都可以,只是最好放在前面吧,這樣有利於提高代碼的可讀性。
9、 好了,那我們來一起看看最后的結果吧。
10、還想要其他的風格嗎?自己按照列表添加吧。