Você está na página 1de 4

Mastering Microsoft

Visual Basic Controls:


ListView indentation,
background shading,
custom sorting, and more

Table of Contents:
Give the ListView control a facelift
with indentation and background images ............................................................................4
Drag and drop items from one ListView to another ............................................................8
Use a callback function to perform custom sorting
in a ListView control ...............................................................................................................11
Solving item-retrieval problems when sorting a
ListView with API callbacks..................................................................................................15
Effectively using the ImageList and ListView
controls in .NET .......................................................................................................................16

Additional Information:
Readers Choice PDF Series, Mastering Microsoft Visual Basic Controls:
ListView indentation, background shading, custom sorting, and more
(ISBN-1-58205-150-x) is published by Element K Journals, a division of
Element K Press, 500 Canal View Blvd., Rochester, N.Y., 14623.

Customer Relations
U.S. toll-free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(800) 223-8720
Canada toll-free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .(877) 203-5248
Outside the U.S. and Canada . . . . . . . . . . . . . . . . . . . . . .(585) 240-7301
Customer Relations fax . . . . . . . . . . . . . . . . . . . . . . . . . . .(585) 292-4392

Editorial
Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sheila Davis
Associate Publisher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Michelle Rogers
Managing Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Mike Jones
Copy Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Jeff Hotchkiss
Leesa Israel
Contributing Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . G. H. Cloutier
Cover Design and Content Designer . . . . . . . . . . . . . . . Melissa Ribaudo
Graphic Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Alicia Natale

Price
Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . $14.95

Copyright
Please be aware of our copyright policy. Questions or requests for
permission to copy or republish any content may be directed to:
contentreuse@elementk.com.
2004 Element K Journals, a division of Element K Press LLC (Element
K). All rights reserved except for the right to view this PDF using a
Web browser and to make private, noncommercial use hereof. PDF
content may be printed by the purchaser, but may not be duplicated and
distributed. Element K and the Element K logo are trademarks of Element
K LLC. The content published in this PDF (Content) is the property of
Element K or its affiliates or third party licensors and is protected by
copyright law in the U.S. and elsewhere. This means that the right to copy
and publish the Content is reserved. Content may not be copied in whole
or in part or further distributed in any form or medium without the express
written permission of Element K.
All product names or services identified throughout this PDF are trademarks or registered trademarks of their respective companies.
Multi-user options are available
To request information about multi-user options please call Brian McKeon
at (585) 292-4363.

Our Canadian GST # is R140496720. Our CPM # is 40031405.


Our QST # is 1018491237.

Contact us at www.elementkjournals.com or 1.800.223.8720

2004 Element K Journals

A Note from our Publisher:


Dear Microsoft Visual Basic Enthusiast,
Thank you for choosing Element K Journals, the leading provider of computer technology journals and professional development products, as your comprehensive, innovative, and timely
information resource.
Since the companys inception more than 20 years ago, publishing best-selling software support
books has evolved into producing a complete line of digital and print products designed to complement every skill level and provide solutions to the challenges you face every day. Throughout
this period of growth, we have maintained our focus on providing you with the highest quality
content, in an easy-to-read format, through the best mediums available.
Thats why weve created the new Readers Choice PDF Series. This series of downloadable
reports contains a best-of selection of our most-requested articles covering hot topics in technology and professional development. You can have instant access to the information you need
right here, right now!
In addition to our Readers Choice PDF Series, Element K Journals produces nearly 40 monthly
how-to journals, a family of CD-ROM products, Quick Skills Kits, Quick Reference Guides, Quick
Reference Cards, special reports, tip booklets, and weekly email tips.
For more information regarding Element K Journals and a complete product listing, please visit
our Web site at www.elementkjournals.com.
Sincerely,

Doreen M. Bieryla
Publisher, Element K Journals

A Note from the Editor:


If you walk into any book store, youll find all kinds of manuals devoted to teaching you Visual Basic
fundamentals. But if you really want to create professional-grade applicationsif you really want to
satisfy the increasingly heavy demands from clients and employersyoure going to need to push
the standard Visual Basic controls to their limits, and beyond. In Inside Microsoft Visual Basic, I enjoy
showing readers how to get more out of Visual Basic than they ever thought possible. The ListView
is one control for which readers consistently ask for more advanced techniques. The techniques Ive
collected here are some of their ListView favoritesI think theyll become some of yours, too.

Editor Bio:
Sheila is the editor of Inside Microsoft Visual Basic and Active Server Developers Journal. She has
over 12 years programming experience starting with Fortran and VB 3. In addition to programming, shes also been a software trainer, developed user documentation and training manuals,
and was a technical editor for the Visual Basic 6 Bible from John Wiley & Sons. In her spare time,
shes a floor mat for her 9-year-old daughter.

Contact us at www.elementkjournals.com or 1.800.223.8720

2004 Element K Journals

Give the ListView control a facelift with indentation and


background images
by Jason Fisher
Application: Microsoft Visual Studio 6.0
Operating Systems: Microsoft Windows (All Versions)
Download: http://download.elementkjournals.com/vbasic/200309/ListViewControlFacelift.zip
The ListView and TreeView controls Microsoft has so thoughtfully provided for Visual Basic developers address a wide variety of design and user-interface needs, but sooner or later youre going
to run into a situation that isnt quite suitable to either one individually. Take Outlook Express, for
example. In its familiar message pane, you have what looks like both a TreeView, with expandable
nodes and indentation, and a ListView, with columns and column headers. Clearly, this control
would seem to be some kind of hybridand a very useful one. But can you tap into this sort of
functionality with the Microsoft Common Controls available to you in VB? And, if so, how? As
youve no doubt surmised from the title of this article, its not only possible, but its a matter of
customizing the ListView control. And as you might expect, doing so will require a sojourn into
the world of the Win32 API.

A new view of a common control

Well begin with a look at why and how the ListView control, as opposed to the TreeView or any
other control, is the best solution to this problem. Next, well develop a sample application that
goes a long way to replicating the user-interface in Outlook Express and other similar programs.
As you can see from Figure A, our sample will support indentation as well as ledger-style alternate-row highlighting through the judicious use of a background image.

Why a ListView?

The first question to answer is why wed want to customize a ListView control to serve our interface needs. Wouldnt a TreeView control work just as well? Or a custom hybrid we might write
ourselves? Though they might work as well, neither of the alternatives would be anywhere near
as easy to implement. Writing your own custom control would be an enormous and arduous task,
so thats out. In the end, the only viable alternative is a cosmetically altered ListView control.
The reason its easier to tackle modifying the ListView to act a little bit more like a TreeView than
vice versa comes down to the matter of columns. To give a TreeView control columns that users
can click, resize, and drag involves much more API manipulation than simply adding the ability to indent items in a
ListView. Thats because youd be dealing not only with
the ColumnHeaders collection and all of its constituent ColumnHeader objects, but youd also have to manage the relationship between these objects and a TreeView control entirely
through the API.

Figure A: By tapping the power of the Win32 API, we can give


a common ListView control a whole new look.

Contact us at www.elementkjournals.com or 1.800.223.8720

Even Outlook Express itself uses a ListView. We can prove


this by examining its window structure using the Spy++
utility that ships with Visual Studio. As you can see in
Figure B on the next page, the control is a SysListView32,
the underlying API equivalent to VBs ListView control.
So while customizing a TreeView in this way might be a
worthwhile exercise in taking VB off-road, we can sidestep
these difficulties by taking advantage of two characteristics
already available in the ListView control: indentation and
background images.

2004 Element K Journals

Você também pode gostar