Feeds:
Posts
Comments

Archive for July, 2007

I was moving a WinForms app to WPF and I was perplexed for quite a while this morning when I found that setting the FontSize on a WPF TextBlock didn’t really give me the font-size I expected (the one that WinForms gave me). It was obvious pretty soon that WinForms was using point as the unit while the WPF default was to use device independent pixels. For a while I was confused as to how to set the font-size in points programmatically, since the FontSize setter only took a double and thus I couldn’t pass a unit-specific string as I could in Xaml. The solution was simple and it beats me why I didn’t think of it sooner, but for those others who occasionally hit dense brain periods like I do, the trick is to use FontSizeConverter.

double pointSize = (double)new FontSizeConverter().ConvertFrom("8pt");

Read Full Post »

I wrote a multi-column combobox class with support for data-binding at work and I have published an article on it here :-

It’s fairly easy to use and all you need to do is to set the DataSource property. The control will auto-populate the columns from the columns (if any) in the DataSource.

Read Full Post »

I started an installation of Visual studio Orcas beta 1 last night just before I went to sleep and also set the alarm for 3 AM so I could wake up and hit [Next] when it prompts me to install the documentation. Well guess what? I woke up and found that Vista had rebooted and there was a nice balloon tip on the lower right of my screen telling me some updates got installed and so it rebooted. Holy crap! There was a freaking installation going on, damnit! Has this crap happened to anyone else too?

The good thing is I restarted the installation and decided to stay up for 30 minutes to make sure it was all good. That turned out to be a good move because the setup paused and asked me to kill the machine debug manager (no idea why that was running) which I did. I went to sleep and woke up a while ago to find the installation complete. Apparently documentation got auto-installed and thus I didn’t have to click [Next] for that. But I still cannot believe that the OS was stupid enough to do a reboot while an installation was running – how screwed up an idea is that anyway! :grrr:

Read Full Post »

In my previous blog entry, I talked about extending marshal_as to support other type conversions in addition to the default ones. But the examples I used didn’t need to handle resource deallocation. When you have type conversions which require explicit resource deallocation, you have to handle it slightly differently. As an example I have written some code that’ll extend marshal_as to support conversions between .NET Font objects and native Windows HFONT structures

namespace msclr
{
    namespace interop
    {
        template<> ref class context_node<System::Drawing::Font^, HFONT>
        : public context_node_base
        {
        private:
            System::Drawing::Font^ _font;

        public:
            context_node(System::Drawing::Font^% to, HFONT from)
            {
                to = _font = System::Drawing::Font::FromHfont((IntPtr)from);
            }

            ~context_node()
            {
                this->!context_node();
            }

        protected:
            !context_node()
            {
                delete _font;
            }
        };

        template<> ref class context_node<HFONT, System::Drawing::Font^>
        : public context_node_base
        {
        private:
            HFONT _hFont;

        public:
            context_node(HFONT& to, System::Drawing::Font^ from)
            {
                to = _hFont = (HFONT)from->ToHfont().ToPointer();
            }

            ~context_node()
            {
                this->!context_node();
            }

        protected:
            !context_node()
            {
                DeleteObject(_hFont);
            }
        };
    }
}

I’ve added two specializations of the context_node template class which is used by the marshal_context class to handle conversions that need a context. In the constructors I create the object that’s requested and in the destructor/finalizer I free the resource. In the case of HFONT, I have called DeleteObject whereas for Font, I have called delete (which calls Dispose). I need not have done that for Font as the Font finalizer would have come into play eventually, but for GDI objects it’s preferable to free them as soon as they are not required as they tend to be rather heavy on the memory side.

Using these conversions is pretty similar to how marshal_as conversions are used for const char* or BSTR (where we use a context object too).

HFONT hFont = CreateSampleFont();

//...

marshal_context context;

System::Drawing::Font^ font =
    context.marshal_as<System::Drawing::Font^>(hFont);
HFONT hFontCopy = context.marshal_as<HFONT>(font);

//...

DeleteObject(hFont);

That’s it. Pretty straightforward to extend and to use.

Read Full Post »

A couple of blog entries ago, I had written about the marshal_as library that is introduced in Orcas and how it’s very useful for mixed-mode string conversions. While the built-in functionality only allows string conversions, it’s also possible to extend marshal_as functionality to support other type conversions. As an example of doing this, I have written a sample extension that supports converting between the Windows Forms Rectangle structure and the Win32 RECT structure. For the sake of completion I have also added specializations for the MFC CRect wrapper (which is a thin wrapper around the RECT structure). Here’s my extension (put into a separate header file) :-

namespace msclr
{
    namespace interop
    {
        template<> System::Drawing::Rectangle
            marshal_as<System::Drawing::Rectangle, RECT> (
            const RECT& from)
        {
            return System::Drawing::Rectangle(from.left, from.top,
                from.right - from.left, from.bottom - from.top);
        }

        template<> System::Drawing::Rectangle marshal_as<
            System::Drawing::Rectangle, CRect> (
            const CRect& from)
        {
            return System::Drawing::Rectangle(from.left, from.top,
                from.Width(), from.Height());
        }

        template<> RECT marshal_as<RECT, System::Drawing::Rectangle>(
            const System::Drawing::Rectangle& from)
        {
            System::Drawing::Rectangle rectangle = from; //remove const
            RECT rect = {rectangle.Left, rectangle.Top,
                rectangle.Right, rectangle.Bottom};
            return rect;
        }

        template<> CRect marshal_as<CRect, System::Drawing::Rectangle>(
            const System::Drawing::Rectangle& from)
        {
            System::Drawing::Rectangle rectangle = from; //remove const
            return CRect (rectangle.Left, rectangle.Top,
                rectangle.Right, rectangle.Bottom);
        }
    }
}

Effectively I’ve added four specializations for the four conversions that I need. Note how I have put the conversions into the msclr::interop namespace. This is to allow me (and anyone else) to use these additional conversions the same way I’d be using marshal_as for the regular conversions.

Now using these conversions would be quite trivial as show below.

//To Rectangle

RECT rect = {10, 10, 110, 110};
System::Drawing::Rectangle rectangle =
    marshal_as<System::Drawing::Rectangle>(rect);

CRect mfcRect(20, 20, 220, 220);
rectangle = marshal_as<System::Drawing::Rectangle>(mfcRect);

//From Rectangle

RECT rectBack = marshal_as<RECT>(rectangle);

CRect mfcRectBack = marshal_as<CRect>(rectangle);

One important thing to be aware of is that in all four conversions I’ve added there is no need to handle context as there is no explicit need for memory deallocation. This may not always be the case as we’ll see in the next blog entry (hopefully soon).

Read Full Post »

It’s been quite a while since I blogged – I am not sure why though. There are various topics I meant to blog about but never got around to doing it. Anyway this morning I updated my website (www.voidnish.com) to ASP.NET 2. Of course, there’s not going to be any speed difference or anything like that as it’s a very simple web site that might as well have used static pages. I’ve updated the articles section with my latest articles, and also made a few minor changes to the rest of the site. The one bad thing that happened during the upgrade was that the colorizer stopped working. I was using Mr .NET’s colorizer and while he has an ASP.NET 2 version available, I couldn’t get it to work so far. I’ll look into it later though and once I get it working as expected the website will once again support syntax colorizing. Please bear with that inconvenience until that happens. Also, I intend to resume blogging actively within a few days and also hope to write a few more articles (been procrastinating it for too long now).

Read Full Post »

Follow

Get every new post delivered to your Inbox.