July 2007 - Posts

Microsoft Patterns and Practices April 2007 DVD

Microsoft Patterns and Practices April 2007 DVD

http://www.microsoft.com/downloads/details.aspx?FamilyID=6724E09B-CA2E-425A-8D71-8FEBAD3BA203&displaylang=en

Full DVD download of all the articles, videos, libraries, and tools posted by Microsofts Patterns and Practises team. Well worth the download wait.

Posted by dsmyth
Filed under:

Open Packaging Convention (OPC)

Was reading MSDN Magazine through the week and there is an article in it on OPC. In short OPC is a new standard for saving complex data, perhaps stored in multiple files, to a single file (a package) using a combination of two standard technologies zip and xml.

Here is the article...

OPC: A New Standard For Packaging Your Data
http://msdn.microsoft.com/msdnmag/issues/07/08/OPC/default.aspx

The idea is simple you store your data files seperately and use xml to create relationships between them. All the seperate files are then combined into a single zip file. Office 2007 uses these new file formats, calling them Open XML Formats, but there is nothing stopping you creating your own.

I'll give you an example a Word document is made up of text, styles, maybe a macro and perhaps some images. With the new format instead of saving this in binary the following happens, the text is stored as a WordML file, the styles are saved in an xml file, the macros are stored in a seperate binary file and each image is saved to a jpeg file. Relationships are then used to knit the document together and essentially describe how the document is built from each of the seperate files. All the files are then zipped into an single archive and the normal zip extenstion is changed to associate the single file package with Word.

Here is a noob friendly article that describes the file format, a good place to start

Ecma Office Open XML Formats architecture guide
http://office.microsoft.com/en-us/products/HA102057841033.aspx

and here is another thats not so noob.... but more complete

Introducing the Office (2007) Open XML File Formats
http://msdn2.microsoft.com/en-us/library/ms406049.aspx

You also don't need to wait until your financially paranoid company decides to upgrade to Office 2007. There are both software development kits and Office 2003 Compatibility Packs to get you up and running (although I haven't had time to give them a proper once over).

2007 Office System: Microsoft SDK for Open XML Formats
http://www.microsoft.com/downloads/details.aspx?familyid=ad0b72fb-4a1d-4c52-bdb5-7dd7e816d046&displaylang=en

Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats
http://www.microsoft.com/downloads/details.aspx?familyid=941B3470-3AE9-4AEE-8F43-C6BB74CD1466&displaylang=en

If any of the links don't work then do a search for the title and you'll get the pages.

It has always been a nightmare trying to extract data from Office files and if the company you work for is anything like the one I work for where the report is more important than the data held within the report, or you get many requests to extract data from Word or Excel, or even if your fed up telling people that the 36Mb spreadsheet that they cannot produce a report from should have been a database, then you'll be happy, very happy, as sometime soon things are going to get easier.

Posted by dsmyth

Embedding images into Mp3's

I'm a huge fan of electronic music and being the type of frood who knows where his towel is at I've got rather a nice selection of Mp3s and a lovely IRiver H340 to play them on. The H340 comes with it own built in firmware which although it's very colourful it's still crap. So some sexy developers have got together and produced Rockbox, a free alternative firmware for certain Mp3 players.

One of the custom themes of RockBox, iCatcher, appears to extract from the Id3v2 tag of the mp3 the embedded image stored as the album art (front cover, back cover, etc). That got me looking into software tools that let you embed images into mp3s and what I found wasn't good enough, so I created my own; it's great being a developer.

First off I had a look for some .NET libraries that let you get or create mp3 Id3v2 tags and low and behold....

TagLib#... http://www.taglib-sharp.com/Main_Page

An excellent API for altering mp3 (and many other formats) tags. So here is some code using this library that allows you to embed an image. This code only works if the image is correctly sized, 75 pixels by 75 pixels is suitable.

'get the mp3 file
Dim mp3 As TagLib.File = TagLib.File.Create("D:\Towers Of Dub.mp3")
'create the picture for the album cover
Dim picture As TagLib.Picture = TagLib.Picture.CreateFromPath("D:\UfOrb.jpg")
'create Id3v2 Picture Frame
Dim albumCoverPictFrame As New TagLib.Id3v2.AttachedPictureFrame(picture)
albumCoverPictFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
'set the type of picture (front cover)
albumCoverPictFrame.Type = TagLib.PictureType.FrontCover

'Id3v2 allows more than one type of image, just one needed
Dim pictFrames() As TagLib.IPicture = {albumCoverPictFrame}
mp3.Tag.Pictures = pictFrames 'set the pictures in the tag
mp3.Save()

Unfortunately!!

Microsofts Media Player only supports Id3 v2.3, which has been superceded, so you have to use the following lines of code to tell the TibLib library to use v2.3, otherwise it uses a default of v2.4.

TagLib.Id3v2.Tag.DefaultVersion = 3
TagLib.Id3v2.Tag.ForceDefaultVersion = True

If you have any need to work with mp3 tags TagLib# is the API to use!

Posted by dsmyth
Filed under:

Looping over resources

Here is a small code snippet that demonstrates looping over the resources stored in the My.Resources namespace. It was a response to a MSDN forum question where all the resources stored were images. If you want have more types of resources then you'll want to refine this a bit. Treat the code below as a starter for 10 and take it from there...

Dim res As Resources.ResourceManager = My.Resources.ResourceManager
Dim resSet As Resources.ResourceSet = _
res.GetResourceSet(Globalization.CultureInfo.CurrentUICulture, True, 
True)
Dim en As IDictionaryEnumerator = resSet.GetEnumerator()

While en.MoveNext()
'en.Key - name of the resourse
'en.Value - the resource as an object
images.Add(en.Value)
End While
Posted by dsmyth

The ListView and its ListViewGroups.

The ListView control is maybe the most commonly used control used by Windows. The reason being its used by the file explorer to let you view your files in a variety of ways, you can view directories as icons, you can view thumbnails, or you can view all the files details. One option of the file explorer that you might not make use of is the ability to group the icons. Here's an example...

 

It's not that useful in Windows to be honest, or at least I haven't been in a position when I needed to use it. However..... It's actually pretty good if you use it in your own applications to group certain common options, for example, you have a place where the user can print reports, rather than listing of all reports and letting the user scan through them you can instead group your reports into categories i.e. financial or informational. Another examle might be a list of products that the user can select from you can now list the products by category or price. It works really well and I've had some fairly positive feedback from users, which is surprising, users tend not to give feedback unless something doesn't work.

So here's how. I'll cover how to do it through code, once you understand that adding groups and items though Visual Studio IDE will be straightforward.

The concept first, simple really, the listview can be made up of groups which contains a collection of items (sub items are not supported), any item not in a group is placed in a default group. So the first thing to do is you need to add the groups to the ListView, I'll use the product example given above....

Step 1: Create your groups and add them to the ListView's Group property

Dim audio As New ListViewGroup("Audio CD")
Dim dvd As New ListViewGroup("DVD")
Dim vhs As New ListViewGroup("VHS")

Dim groups As New List(Of ListViewGroup)
groups.Add(audio)
groups.Add(dvd)
groups.Add(vhs)

Me.ListView1.Groups.AddRange(groups.ToArray())

This is done by creating instances of ListViewGroup objects, it's easier to create instance rather than to add the groups directly as when it comes to adding items you need to use the group instances as your about to see.

Step 2: Add the items to the ListView and specify the group it's to appear in. It's important to note that there is a one to many relationship between groups and items. One group can have many items but one item can only belong to one group. Here's some example code....

Dim viewItem As ListViewItem
viewItem = Me.ListView1.Items.Add("Biosphere - Substrata")
viewItem.Group = audio

viewItem = Me.ListView1.Items.Add("Night of the Living Dead")
viewItem.Group = dvd

viewItem = Me.ListView1.Items.Add("Chopper Chicks in Zombie Town")
viewItem.Group = vhs

When the ListViewItem is added to the ListView control you need to set it's Group property to an instance of a ListViewGroup object.

Step 3: When you use the ListView in this way the layout doesn't follow the usual grid like approach so you have to sort the layout of the items, which you do using a couple of properties. This will completely depend on what your control should look like and how it should act. Just make sure ListView.ShowGroups = True

You may also notice that the labels of your items aren't quite right. If you find this happening set ListView.View = Title and set ListView.LabelWrap = False and hopefully that will fix any problems you have.

Click here to download the code example... it's simple but demonstrates the idea.

Changing the display name and sorting properties of the Property Grid

This post assumes a bit of background knowledge on how to use and control the property grid when displaying the properties of an object.

The property grid is a great control, I like it a lot, but it does have some problems. One of these is the use of attributes to specify and control categories, display names and descriptions of each property it displays. The attributes, which are specified at design time, allow only string literals to be set. These are compiled into assembies metadata and because of this they cannot be changed at runtime. So, for example, you cannot look up the name or description of a property from a database, or a file, or even change it depending on a condition.

Another problem is there are no attributes to specify in which order the property grid should displays the object properties, so even though in code the properties of your object are defined in the order you want, the property grid won't display them in that order.

There are two classes that control how the property grid displays an objects properties that help fix these two problems, one class is used to look up the display name, category and description of a property while the other class indentifies which properties of the object, and more importantly in what order, these properties should be displayed.

The class System.ComponentModel.PropertyDescriptor is a class that tells the property grid how to describe a property. There are many methods and properties that need to be overriden but the three most useful are the read only DisplayName, Category and the Description properties. This descriptor class could be thought of as a class that describes how a single property is displayed in the property grid.

The second class allows you to select and sort the properties displayed on the property grid. The System.Component.CustomTypeDescriptor class allows to control how the properties of a type are displayed. It's main task is to build a collection of PropertyDescriptors.

If the PropertyDescriptor describes how each individual property of a type is displayed then the CustomTypeDescriptor completes the picture by allowing a type to describe how it's properties are displayed. As you can imagine the CustomTypeDescriptor creates instances of the PropertyDescriptor. Generally the object whos properties you want to display is inherited from the CustomTypeDescriptor and this allows the object to describe itself to the property grid.

Granted this description, without code samples, isn't particularly intuitive so here is a small sample application that demonstrates how to use the above two classes. The example displays a simple Employee class in the property grid. The employee class has no attributes describing how each of it's properties are displayed, that information is extracted from a comma delimited file by a EmployeePropertyDescriptor class, which doesn't do a very efficient job of the lookup, but it is only a demo. The employee class also inherits from the CustomTypeDescriptor and in the GetProperties() method specifies the order of the properties and using that order builds a collection of EmployeePropertyDescriptor objects which is used by the property grid. Very simple example.

DisplayName And Sorting PropertyGrid.zip

Very sorry !! Missed out a very small but important detail! You have to set the Property Grids PropertySort property (eh?) to Categorized. Otherwise you'll still get the list in alphabetic order. Sorry for missing that.

Posted by dsmyth | 1 comment(s)
Filed under:

Catching up....

After a fairly intense period of full on study I finally sat the MCPD Windows Developer upgrade exam (70-552) and was pretty shocked to see that I failed. wtf? can I say wtf on the Internet? of course I can because I failed the exam, wtf? To be honest I don't like the new format they have introduced in the upgrade exams.

Basically you sit three one hour exams (70-526, 70-536, and 70-548), which in itself is fine but if you finish two exams in 30 minutes, then the hour isn't transferred into the last exam, which is a bit unfair. Basically I passed the two code based TS exams (70-526, and 70-536) taking about 30 minutes for each and messed up on the (70-548) questions to do with debugging performance and troubleshooting network problems, which you really need experience of, not something you can read in a book, if infact you can find a book on it. What was so annoying was the outstanding hour wasn't transferred and with 30 questions in 1 hour, 2 minutes a question, thats a bit harsh Microsoft.

So anyway I have been neglecting my blog and to a greater extent DotNetSlackers and everyone here, and for that I appologise. I have so much to write about as well. Lots of nice little juicy code snippets but just not getting around to posting them for some reason. Need to start posting from work more I think and recent changes in my position might make that more of a possibility. I'll make it happen.

Need to write an article as well, I haven't written one this year and that's rubbish. Think I'll write an article on the internals of the DES and 3DES encryption algorthims, which is a subject I've been reading up on, not DES specifically just crytography and security in general. I believe that, with the way the industry is going, the two most important subjects for a developer to know is security and threading.

But I do need to contribute more!!

Posted by dsmyth