WM 5.0 projects on Vista

Fri, June 30, 2006, 04:27 PM under MobileAndEmbedded
It seems many NETCF developers are being bitten on Windows Vista after installing the WM 5.0 SDK for Visual Studio. The symptom is that although the SDK installation appears to go fine, the WM 5.0 project templates do not appear in VS2005.

The problem has to do with User Account Control (btw, I'll have a lot more to say about UAC in a future blog post). The way to work around it (on Vista Beta 2 at least) is to disable UAC and then install the WM 5.0 SDK thus getting the project templates to appear.

Specifically:
1. Launch "Control Panel"
2. Navigate to "User Accounts and Family Safety -> User Accounts"
3. Click on "Change Security Settings"
4. On the window that appears, uncheck the "Use User Account Control (UAC) to help protect your computer" checkbox (requires restart)
5. Now install the WM 5.0 SDK (or do a repair if you already had)
6. Launch VS2005 to verify you have the templates
7. Enable UAC again

The above combined with the WMDC update should answer most relevant queries...

.NET Compact Framework on Symbian

Tue, June 27, 2006, 03:18 PM under MobileAndEmbedded
Some people are asking about NETCF on Symbian. This makes me smile.

You see, every now and then, someone will bring out a Nokia phone and show me some end user feature and ask me how to achieve the same on a Windows Mobile device following it with a rant about how Symbian devices are dominant in the market compared to WM devices. My response is always the same. Yes, at the moment, Symbian is the dominant mobile OS when it comes to number of end user devices out there (not so sure in the enterprise/corporate world). However, from a developer perspective, that is completely irrelevant! Ask not how many devices are out there, ask how many of them are running an app that did not ship with the device. In other words, ask what percentage of Symbian devices compared to WM devices are running a 3rd party application that the user installed themselves. Windows Mobile *is* the dominant OS when you ask the right question and it is kind of expectant really: Every windows developer is a potential WM developer. The platform itself is built with running 3rd party apps in mind.

So going back to the original question, it shouldn't surprise us that it is being posed. An unfortunate developer has been asked by a decision maker to target a Symbian device and naturally the developer wants to use a modern, RAD and powerful platform.

The best development tool for all your needs is of course Visual Studio. To that end, there is an add-in for Visual Studio that allows targeting non-WM devices BUT note that this does not use the NETCF on the target - it is just a tool thing.

The best development runtime/framework of course is the .NET Compact Framework (ok, so I may be slightly biased :-)). When I was at MEDC a South African guy (Dusan Babich) approached me with some brochures and interesting ideas about porting the NETCF to a subset of Symbian devices. He didn't have a demo to show at the time but he said they were very close to releasing. I am not sure how far it got but this is their website should you wish to follow up.

I also found this excellent paper on porting the NETCF to Symbian phones. Just to keep the balance, here is an article on porting Symbian apps to Windows Mobile SmartPhones.

So there you have it. A slightly lengthier reply than what I had in mind originally but it is good to have context, right? To wrap it up, my advice is to target the Windows Mobile platform that gives you a versatile range of devices with a common API (and the story will get even better with future versions of SmartPhone and PPC form factors converging, I would imagine). If I am perfectly honest, I am not sure WM will win the consumer market tomorrow, but it is definitely slashing into the enterprise space right now and once it is done there, more of an end user focus should follow, in my opinion.

Vista: Glass in C#

Sat, June 17, 2006, 05:32 AM under Windows | Vista
We looked at glass in Vista previously so please read that to make sure we are on the same page and using the same terminology... I'll wait...

Great, now that you are back, let's see how to get glass in our C# applications. For starters, if glass is supported and enabled on the user's PC, then your non-client area will by default get the glass effect. In other words, the titlebar and borders will have glass (nothing more for you to do). The question is how to extend that into the client area like other OS applications do (as we saw last time).

There are 3+1 things we need to do:
1. Check that it is supported before proceeding further.
2. Extend the frame into the client area.
3. Paint the extended area black.

Optionally,
4. Allow the user to drag our window by pressing in the glass area as if they were pressing on the window's caption bar.

So let's go!

1. First, let's make sure we are not on a legacy OS:
    if (Environment.OSVersion.Version.Major < 6)
{
Debug.WriteLine("How about trying this on Vista?");
return false;
}
After that, we need to check that glass is enabled by using the DwmIsCompositionEnabled API which resides in Desktop Window Manager (dwmapi.dll)
    // [DllImport("dwmapi.dll")]static extern void DwmIsCompositionEnabled(ref bool pfEnabled);
bool isGlassSupported = false;
DwmIsCompositionEnabled(ref isGlassSupported);
return isGlassSupported;
2. Extending the frame requires setting up a margins structure and passing it to another DWM API: DwmExtendFrameIntoClientArea.

So if we want to extend a bit from the top of the window(i.e. we don't care about extending from the bottom, left or right sides) we can use the following code:
    // [DllImport("dwmapi.dll")]static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);
Margins marg;
marg.Top = 20; // extend 20 pixels from the top
marg.Left = 0;
marg.Right = 0;
marg.Bottom = 0;
DwmExtendFrameIntoClientArea(this.Handle, ref marg);
3. If you run your winforms app now, you'll find a black strip at the top. This is where we need the alpha blending and using a black brush does just that (tricking GDI that has no notion of an alpha channel).

So, in your form's paint event handler add the following code:
    SolidBrush blackBrush = new SolidBrush(Color.Black);
g.FillRectangle(blackBrush, 0, 0, this.ClientSize.Width, marg.Top);
4. Moving the form programmatically can easily be achieved by catching the mouse down/up/move events and some simple logic but instead we can use the following magic:
// make windows do the work for us by lieing to it about where the user clicked
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

if (m.Msg == 0x84 // if this is a click
&& m.Result.ToInt32() == 1 // ...and it is on the client
&& this.IsOnGlass(m.LParam.ToInt32())) // ...and specifically in the glass area
{
m.Result = new IntPtr(2); // lie and say they clicked on the title bar
}
}

private bool IsOnGlass(int lParam)
{
// get screen coordinates
int x = (lParam << 16) >> 16; // lo order word
int y = lParam >> 16; // hi order word

// translate screen coordinates to client area
Point p = this.PointToClient(new Point(x, y));

// work out if point clicked is on glass
if (y < marg.Top)
return true;

return false;
}
5. There is no 5 :-) Just as a gotcha, if you decide to extend the client frame after the form has been displayed (and hence its handle has been created), you should call
this.RecreateHandle();

Note that there are other simpler approaches that use winforms Transparency but they seem to suffer from a very undesirable effect: When the user clicks on the glass area they are actually clicking on whatever is behind the form (true transparency is not what we want here).

Download a complete sample here.

Vista: ActiveSync replacement

Fri, June 16, 2006, 08:57 AM under MobileAndEmbedded
There was a pleasant surprise on Windows Update this morning, a Beta update of the Windows Mobile Device Center that I talked about last month. After downloading it I captured some screenshots below (some are behind the hyperlinks and others inline with the body).

First connect a WM device or you'll get different screenshots since the UI options are visible only when applicable.

First screen offers us two options. The first one sets up the partnership and the second looks like this:


Hovering over any of the 4 areas, exposes further options: Programs and Services, Pictures Music and Video, File Management and Mobile Device Settings.

The Programs and Services option has two sub-options: One takes you to the windowsmobile home page on microsoft.com; the other option allows you to add/remove programs via this dialog:


The Pictures Music and Video option has 3 sub-options: one shows you how many "new pictures/video clips are available for import"; the last one launches a wizard from Media Player so you can "add media to your device from windows media player"; the middle option offers the following dialog "picture video import settings":


The File Management option doesn't seem to have changed from the one I captured last time.

The Mobile Device Settings option shows connection settings and of course the one we've all been waiting for: Setting up partnership (which I'll have more on later :-)

Vista: Glass

Fri, June 16, 2006, 03:00 AM under Windows | Vista
One of the new "features" of Windows Vista is: glass. Title bars and borders on windows (i.e. the non-client area) now have a transparent glass effect. Assuming your graphics card is powerful enough to support it you must choose the "Windows Vista Aero" colour scheme under Appearance Settings and you've got glass ability:


If you don't have the aero option, go to "Control Panel-> System and Maintenance -> Performance Rating and Tools" and Refresh your rating [if even that doesn't do it for you, buy a better graphics card or lower your resolution or search the net but please do not email me for help in this particular area as this is all I know :-)]

You can control whether the glass effect is transparent or not (it looks good even when not transparent), and further control the intensity of it and the colour that the glass is based on. The control panel for these options looks like this:


While aesthetics is one of the goals that the glass effect scores, it achieves a usability goal that, admittedly, you only appreciate after living with Vista for a while: it makes you naturally focus on the content of the window, the main area, rather than anything else; it draws your attention to the solid, useful bit in the middle of the window.

Some applications have taken the aforementioned benefit further, by extending the glass effect into the client area of their windows. An example of that is the Control Panel, Document Explorer and Internet explorer (they have extended the glass area from the top of the window):


Another example is Windows Media Player (it has extended the glass from the bottom of the window):


And finally, the Sidebar Gadget Gallery has gone all the way as it has extended the glass from top to bottom (or bottom to top if you prefer):


Note that in all the previous applications/windows, you can move the window not only by holding down and moving the mouse on the title bar but also in any glass area. I don't know if this is a guideline but it should be (if it is, Windows Photo Gallery doesn't follow it). A few applications offer the ability to move a window by clicking on areas other than the title bar and it would be good to standardise on that area being glass. It would work the other way too by implicitly letting people know that "any glass area of a window can be used to drag the window".

So what is all this information doing on a developer blog? It simply provides background for when I show you how to use glass in your managed applications, next.

Windows Vista in Newcastle

Thu, June 15, 2006, 08:17 AM under Events
Those of you in the north of UK and in particular the Newcastle vicinity are invited to my session at VBUG.

Go register now. Even if you are attended our roadshow, in this session we'll have twice the time to explore the developer story on Vista *beyond* .NET Framework v3.0 (in other words, native Vista-only APIs via managed code :-)

Details here.

Register for my webcast

Thu, June 15, 2006, 08:10 AM under Events
Many of you have caught my session ".NET Compact Framework for Desktop developers" in Horsham, London and Athens. You will have also caught my variation of that in Dublin and yet another variation in Las Vegas. Well, now is your chance to attend it online :-)

If you are a .NET dev with ZERO previous knowledge on .NET Compact Framework or mobility, go register now!

Wednesday 5th July
17:00 GMT
(09:00 AM Pacific Time)

Details here.

.NET Framework v3.0

Thu, June 15, 2006, 07:59 AM under dotNET | dotNET
Ever since the announcement about the renaming of WinFX to .NET Framework v3.0 there have been many blog entries on the topic. Some we can ignore as they are of the "me too" variety e.g. "Here is the announcement. End of Message" or "This is good. End of Message" or "This is bad. End of Message". There have also been some posts about how "This is good. And the reason is X". Where X is usually something sales/marketing orientated; since we are developers, let's ignore those too.

So what are we left with? We are left with a subtle message that many seem to have missed (more on that in a minute) and also with some posts that are simply missing the facts so let's first get the facts right:

1. This is just a name change. Nothing more, nothing less. No schedules have changed, no content has changed, no direction has changed, and no relevant follow up announcements are planned. The technology formerly know as WinFX is now NETFx v3.0

2. It is not named ".NET 3.0". It is named ".NET Framework v3.0". This is more important than what you might think.

3. I have always described .NET as the CLR/engine, the framework/libraries, the compilers/languages and the tools/VS
a) .NET Framework v3.0 doesn't *change* any of that.
b) .NET Framework v3.0 simply *adds* WPF, WCF, WF, WCS
c) It does not add LINQ, Orcas or anything else

4. After .NET Framework v3.0 is released (with Windows Vista as has always been the plan) when you try to install it on XP SP2 it will bring all the v2.0 bits with it. If you have those bits on the machine already, it will simply add the 4 that you are missing.

If you grasp all of the above facts but still want to have discussions about the choice of name then I don’t see the point. Microsoft's marketing department has never picked the right name (as far as developers are concerned) for any of the released technologies. If we look at this, then it either means that developers are never happy _or_ it means that Microsoft marketing will never get the names right so, again, why bother? Just focus on the technology!

Now on to the more interesting IMO observation:

This effectively signifies the beginning of the end of bundled versioning. I look forward to the day where I get updates to the tools without having to wait for a new version of the CLR or getting some library bits without having to wait for the final RTM tools... Decoupling of release schedules brings us the bits when they are ready without having to wait for various groups to align with each other (which inevitably results in delays). The challenge now is to see how well this works going forward. Ask me again in a year…

Vista: TaskDialogIndirect

Mon, June 12, 2006, 06:38 AM under Windows | Vista
You know about my simple managed wrapper of the simple version of the new TaskDialog API. I was going to go ahead and implement a wrapper for the advanced version (namely TaskDialogIndirect) but it seems that VistaBridge does that already).

I knocked up a new winforms app with a button that references the wrapper library, and I then setup as many options as I could on the TaskDialog. The results are ugly but do demonstrate some of the power of TaskDialog:


The code responsible for that follows:


Enjoy!

VistaBridge

Mon, June 12, 2006, 06:35 AM under Windows | Vista
You are a managed developer, with Vista Beta 2 and you also have the SDK (which you know from my Vista Links).

Now run (don't walk) to your Vista machine and do the following now:
Unszip [your drive letter]:\Program Files\Microsoft SDKs\Windows\v1.0\samples\CrossTechnologySamples.zip and browse to the VistaBridge folder and open the solution. If you have two projects in the solution then great, one of them is the Windows Presentation Foundation demo app. If one of the projects failed to load for you then you don't have the WPF Beta 2 bits installed. Not to worry though as the project that did load is the managed class library that you are interested in and it seems ok without the WPF bits. It wraps a few cool native APIs!

I found this by accident while searching my hard drive for one of my TaskDialog files but more on that next...

TextBox prompt

Sun, June 11, 2006, 06:44 PM under Windows | Vista
I just discovered "prompt" for textboxes (no medal required, thank you :-)).
The inspiration to dig into it was the mention in the Vista guidelines (scroll down to "Prompts").

So, you have empty textboxes and you need to prompt the user to do something and in some cases it makes sense to provide that prompt in the textbox while not making it part of the textbox text/content itself; when the user clicks on the textbox (gives it focus) or enters text into it, the prompt has to dissappear of course.

So if you have a windows form with a button and a textbox that has no text in it, put this code in the button_click event handler method:
Win32Api.SendMessage(
new HandleRef(textBox1, textBox1.Handle),
Win32Api.EM_SETCUEBANNER,
IntPtr.Zero,
"some prompt. click in the textbox to make me go away"
);
...and the constants you need are:
internal const uint ECM_FIRST = 0x1500;
internal const uint EM_SETCUEBANNER = ECM_FIRST + 1;
The good news seems to be that this is also available on XP, if we are to believe this msdn link.

It would be nice to have this feature on Windows CE so we could use it in our Windows Mobile applications where screen real estate is at a premium and having additional labels to describe the textbox is sometimes wasteful.

Recent and upcoming Events (plus helping yourself)

Wed, June 7, 2006, 04:43 PM under Events

MSDN Roadshow: 3 down, 3 to go.

In addition to presenting at events, I also attend many in a support/helper capacity. As such, I mingle with the crowd and get to hear what they think about our products. Recent such events were the DevDays event on security in London and the DDD III event in Reading (also look out for me at the Office DevCon in a couple of weeks).

Whether attending, speaking or helping, one of the unwritten "rules" is that I wear a Microsoft shirt. This means that I get many questions, some that I can answer on the spot and some that I take with me for follow up. While I respond to every query I get and am more than happy to listen, help me help you by trying the advice below :-)

1. Search MSDN. Write your question down as if you were going to email it to someone. Then pick out the words that your mom wouldn't understand and enter them in the search box. You'll be amazed at how useful the results can be. Sometimes we forget that we don't call it the Microsoft Developer Network for nothing.

2. Search the newgroups. If you don't find an answer, post your question to the newsgroups.

3. Use the forums.

My bet is that there are no .NET developer questions on our current technologies that the above route cannot answer... none!

Remember that ultimately, Product Support Services is the place where you can get help in an official capacity. Finally, your feedback is always appreciated on ladybug.

I wrote a similar blog entry in my MVP days (due to the high volume of directed email I received) with a particular focus on NETCF: Before you email me with a question


Vista: TaskDialog

Thu, June 1, 2006, 05:07 PM under Windows | Vista
One of the many new things in 'comctl32.dll version 6' is the TaskDialog.
A task dialog is similar to, while much more flexible than, a basic message box.

There are two variants of the TaskDialog the second of which is the one that gives you enormous flexibility and power. We will look at the first variant which, while being simpler, still offers advantages over the old MessageBox API :-)

When I say "look", what I mean is I will share my managed wrapper since TaskDialog is a native API only at this point.

First let's look at what the managed wrapper looks like in a Class Diagram (IWin32Window is part of the .NET Framework, of course):


Here is a VS2005 screenshot of intellisense when using our class:


My test harness looks like this:


And finally, here are a couple of screenshots of the end product:



You may download the sample project here (remember, this only works on Vista!).