Tag Archives: owner-draw

Owner-draw (II)

Earlier, An owner-draw gridwhich is, almost a year ago, I was comparing owner-draw programming in Delphi and C# and concluded that it’s quite similar: basically you set any visual component in a particular mode (‘I’m going to take care of drawing the content’) and then you basically override the component’s OnPaint/OnDraw event. Drawing the actual stuff is the hardest part, but there’s pretty good API help for both Win32 and .Net platforms. If you have programmed graphics in Win32 before, this is going to be a piece of pie.

So, yeah: a couple of years ago, I did some fancy stuff in a Delphi Drawgrid component. The .Net framework has a similar ‘grid’ like component, which is called a ‘Datagridview’ and you probably guessed it: it has basically the same functionality and offers the (almost) the same features as Delphi’s DrawGrid component. This brings me to that earlier mentioned project I was working on: a Sudoku puzzle generator, which obviously needed to use a custom grid-like component with a lot of colours (Warning: if you’re not interested in programming, you probably don’t want to continue reading).

Continue reading

Owner-draw

One point with which I want to close off for today: Ownerdraw stuff in C#. Yesterday, I was comparing code to create owner-draw listboxes and comboboxes in both Delphi and C# and noticed how remarkably easy it’s to switch from Delphi to C#: if you did this in Delphi before, the procedure is almost the same in C# (once again this is not surprising if you know who designed C#, earlier at xsamplex)

In Delphi for example, most VCL components have an ‘ownerdraw’ property: switching this one to either true (or to a specific enumerated value like ‘Ownerdrawfixed’), you generally capture two events to draw (and colour) individual items. The same is true for C#: you set the DrawMode property either to ‘OwnerDrawFixed’ or ‘OwnerDrawVariable’. Which one you choose depends on what you plan to do with the listbox.

As mentioned earlier, code to actually draw an item is done in an event: for this C# has the DrawItem event. I’m not going to explain the code, because it should speak for itself.

private void cboDataSources_DrawItem(object sender, DrawItemEventArgs e)
{

    if (e.Index > -1)
    {
        TDataSourceType nt = 
           (cboDataSources.Items[e.Index] as TDataSource).SourceType;

        // Background first!
        e.DrawBackground();

        if (nt == TDataSourceType.System)
        {
            // Draw image from image list...
            imlSources.Draw(e.Graphics, e.Bounds.X, e.Bounds.Y + 1, 0);
            e.Graphics.DrawString(cboDataSources.Items[e.Index].ToString(),
                e.Font, Brushes.Black, e.Bounds.X + 18, e.Bounds.Y + 2);
        }

        // ... other code removed...
        //

        // and then draw the focus rectangle...
        e.DrawFocusRectangle();
    }
}

What do you mean, it smells like Delphi?

Owner draw

While doing sm_Ellipses.jpg some ownerdraw stuff in regular Windows stuff (stuff is my favourite word, as you can see), I noticed something weird: I was forced to ‘refresh’ regions by code, instead of relying on the tools of the specific programming language. For a single moment I was thinking I was seeing ghosts, but apparently, I wasn’t hence the use of a refresh in a specific piece of code that allowed me to merge a couple of cells in grid.

Now for the gifted among us, how do I make that ellipse look 3D (and don’t tell me to shift the ellipse a couple of pixels up or down, drawn in a different colour).