Draw Gradient Background - Error with Tabs

Hi all,

I'm trying to draw a gradient (linear) background on my devices.

I've found some stuff using System.Drawing.Drawing2D.LinearGradientBrush but for some reason my application doesn't seem to be able to find it. I can get System.Drawing.Drawing2D but that's as far as it goes

I also can't find SystemGradientColors and ControlPaint which are things I need.

I'm using CF2.

Any help would be much appreciated.

Kind Regards,

Grant.



Answer this question

Draw Gradient Background - Error with Tabs

  • Shahab03

    Hi again,

    Actually - the last post i put up is incorrect as it seems it can get a handle to the device context, but just fails when it tries to fill the gradient.

    I just tried (in the Tab Pages Paint event)

    // Get handle to device context.

    IntPtr hdc = e.Graphics.GetHdc();

    // Create new graphics object using handle to device context.

    Graphics g = Graphics.FromHdc(hdc);

    It seems to get some sort of handle, so I'm not sure why it's failing.

    Cheers,

    Grant.


  • Tryin2Bgood

    Hi Michael,

    Thanks so much - that worked perfect - a very nice resulting gradient.

    I tried to mark your post as Answer, but keeps throwing an error - will keep trying as sure others will benefit from this.

    Cheers,

    Grant.


  • Richard Berg MSFT

    Hi Michael,

    The problem is with the Graphics object being passed into the GradientFill.Fill method. For some reason when I pass in the e.Graphics from the Tab Pages PaintEventArgs parameter it doesn't seem to be valid.

    I think the problem occurs in the Fill method when it tries to get the Handle to the Device Context from the graphics object passed it - for some reason it can't get it.

    // Get the hDC from the Graphics object.

    IntPtr hdc = gr.GetHdc();

    So when it tries to Fill the object it returns false

    // PInvoke to GradientFill.

    bool b;

    b = Win32Helper.GradientFill(

    hdc,

    tva,

    (uint)tva.Length,

    gra,

    (uint)gra.Length,

    (uint)fillDir);

    Any help would be much appreciated (again :)) - I've spent so many hours on this trying everything I can and searched for a solution or some insight to what might be going wrong, but am completely lost.

    Kind Regards,

    Grant.


  • smoon

    Hi Michael,

    I now have another problem with the gradient fill stuff...

    It works on the form backgound fine, but when I try it in a form that has Tabs it works for some forms and not others, or works when on one tab page but not the other.

    I have two forms that are almost identical and iI have gone through every bit of code, properties, the designer, etc on both forms and there is absolutely no difference, but one of them displays the gradient on both of the tab pages, while the other form only displays the gradient on the first tab page and not the other. In other forms it doesn't display the gradient on any of the tab pages.

    Any ideas why this would occur are there any known bugs with the stuff you linked me to in your previous reply

    Thanks in advance.

    Kind Regards,

    Grant.


  • Nikhil - MSFT

    Hi Grant

    The sample should be fine - except the remarks about the Windows Mobile 2003 Pocket PC in step 7. I've successully used this on Windows Mobile 5.0 (Smartphone and PocketPC) devices.

    It's hard to say why this occurs without knowing your code.
    One reason might be: When GradientFill gets called with invalid parameters, it may simply not draw anything.

    Michael



  • arse

    The .NET Compact Framework does not support the LinearGradientBrush class. Windows CE does support an API called GradientFill you can use instead.

    Here is a sample how to use this API: http://msdn2.microsoft.com/en-us/library/ms229655.aspx

    Michael



  • Draw Gradient Background - Error with Tabs