Archive for the ‘Win32’ Category

Virtual ListViews - Name for Speed

ListView.  Huh?  Come on, you all know it - the right pane of Windows Explorer.  That is, where contents of folders are displayed.  Whenever you see large icons, or small items listed in columns, or a list of items with details in other columns, it is always one and the same control - ListView.  Kind of a Listbox, but much, much better and powerful.  ListView control came into this world with Windows Explorer back in WIndows 95, and got numerous enhancements eversince.  ListView is very convenient when it comes to display a list of items.  It can even substitute grid controls for read-only data in many cases, as ListView can display multiple columns per data item.  Add support for icons, check boxes, grouping and different layouts, and you have a real beauty.  How to use ListViews is out of the scope of this post.  If you want to learn it - just google for it.  There are tons of tutorials out there.

Great as they are, ListView contols can be very slow with large number of items (like tens of thousands), especially while populating with data.  Furthermore, by default ListView stores all its items internally.  In case you populate the List View from an in-memory collection, you end up with doubled memory usage.  Another thing to be worried about when working with tons of data.

The solution to both the performance and memory-usage issues is to use Virtual ListViews.

(more…)

Posted by Nik on April 10th, 2009 1 Comment

Multiple Threads in the UI - Please, Don’t Do This at Home!

I was having a nice chat with a friend of mine - a .NET Framework and Windows Forms programmer, and he said:

“It is not possible to access and modify the contents of a window from a thread other than the main one of the application.  And this is how Windows is designed”.

Well, he was wrong.  His mistake comes obviously from the fact that Windows Forms in .NET Framework does not allow you to do so.  If you try to access a control (i.e. a window in Win32 terminology) from another thread, you get a slap on your hands with a nice exception.

In Win32 you definitely can modify from another thread the contents of a window, created in another one.  But believe me, you should not do it.

(more…)

Posted by Nik on August 5th, 2008 2 Comments

The Look of Windows API Controls

You can tell that a standard push button in Windows XP looks different than a standard push button in WIndows 2000, and such one in Windows Vista.  On top of it, since XP Windows supports visual themes.  The interesting thing is that sometimes one and the same application keeps its Windows 2000 look, but sometimes standard push buttons follow the way of XP, or the Vista, depending on where you run it. 

In this article I explain why.

(more…)

Posted by Nik on July 31st, 2008 1 Comment

Reading VARIANT from Stream - NET’s way

In a recent post I described how a VARIANT variable can be serialized into a binary stream.  That really did the trick back then, and the team lived happily…  Ever after?  Well, no.  A day came when we had to provide a NET-based interface to our application server.

I will not go into details now about the actual design and implementation of the system.  I will only describe an algorithm in NET Framework, which deserializes a binary stream into an appropriate type.

(more…)

Posted by Nik on July 21st, 2008 No Comments

Reading VARIANT from Stream - Delphi’s Way

In this post I described an efficient way to write a VARIANT to a stream in a binary format.  Doing so will be of no use whatsoever if you cannot bring back a fully-fledged VARIANT from the stream.

Reading a VARIANT is analoguous to writing it, but there is an interesting points.  You have to know how to set properly the type of the output variant.  If the VARIANT is a single value, the right way to do it is to use VarCast function.  If the VARIANT is an array, however, you should create it by using VarArrayCreate.  Only after you set the type, you can assign the data. 

Here comes the code:

(more…)

Posted by Nik on July 10th, 2008 11 Comments

Writing a Variant to Stream

Recently I came to a case when I had to write an OLE Automation VARIANT type variable to a stream.  And, of course, read it back as a fully-fledged VARIANT.  The solution had to fully support arrays as well.  A VARIANT can be a byte, an integer, a floating point number, a date.  A VARIANT can be a string.  A VARIANT can be a safe array of VARIANTs, which can in turn be safe arrays, and so on.  Start to feel the catch?

(more…)

Posted by Nik on July 9th, 2008 No Comments

Taming the Clipboard, part 2

Putting Data into the Clipboard

Putting data into the clipboard involves these few steps:

  1. Open the clipboard.
  2. Empty the clipboard - except for very rare cases, you would better delete what any previous application has put into the clipboard, and replace it with your content.
  3. Register a custom clipboard format, if standard ones do not suit you well.
  4. Put data into the clipboard.  Repeat steps 3 and 4 for every format you support.
  5. Close the clipboard.

Steps 1 and 2 are really straightforward.  You call two API functions - OpenClipboard, and EmptyClipboard.  Registering a custom clipboard format is also a piece of cake.  Simply call RegisterClipboardFormat, pass it a name for it, and store the returned value for later use with SetClipboardData.  If you register a custom format with the same name more than once, the function will return the same value as previous calls - this enables applications to exchange data, as long as they use the same format names.

Putting data into the clipboard is easy if you use NET’s class Clipboard.  Anyway, lets see how it goes the hard way - useful for C++ and Delphi developers out there.

(more…)

Posted by Nik on May 6th, 2008 No Comments

Taming the Clipboard, Part 1

Harnessing the full power of Windows clipboard can bring nice user-friendly advantages to your applications.  Face it, users are accustomed to Ctrl + C and Ctrl + V.  Most programmers content themselves with transferring only text data and sometimes images to and from the clipboard, and they do it through the available helper classes and methods of the framework they use - MFC, VCL, NET Framework.  Both Borland VCL and Microsoft NET Framework provide classes, which make it easy to manipulate plain text, images, and sometimes rich text format.  MFC, on the other hand, does little more than providing access to the OLE interface to the clipboard, which is a bit of overkill in my opinion.   Working with standard data is not enough to make a good application.  With little effort the clipboard can be used to integrate you application nicely with other well-written software.

In this article I will explain the basics of the clipboard, demonstrate how it can be used to exchange data with Windows Explorer and Microsoft Word and Excel.  I will deliberately stay away from the dark corners of Windows, where OLE and COM lurk.

(more…)

Posted by Nik on April 23rd, 2008 3 Comments