tags:

views:

81

answers:

3

I have a tab-based application for windows, which I am developing by myself.

I would like to add a subtle gradient to the background of my tab control. How would I go around doing this? What is the best method for me to use?

I think that implementing a custom control that takes up the space of the tab control would work, but how would I then draw a gradient using GDI?

Thanks in advanced.

+2  A: 

To use GDI you'll need the GradientFill function. You can also use GDI+ to get gradients. Here's a plain GDI example:

TRIVERTEX        vert[2] ;
GRADIENT_RECT    gRect;
vert [0] .x      = 0;
vert [0] .y      = 0;
vert [0] .Red    = 0x0000;
vert [0] .Green  = 0x0000;
vert [0] .Blue   = 0x0000;
vert [0] .Alpha  = 0x0000;

vert [1] .x      = 100;
vert [1] .y      = 32; 
vert [1] .Red    = 0x0000;
vert [1] .Green  = 0x0000;
vert [1] .Blue   = 0xff00;
vert [1] .Alpha  = 0x0000;

gRect.UpperLeft  = 0;
gRect.LowerRight = 1;
GradientFill(hdc,vert,2,&gRect,1,GRADIENT_FILL_RECT_H);

As for the tab control, you could sub-class the control and override its non-client and client drawing handlers to render the gradient.

To sub class a control, first create the control and then replace its WNDPROC function:

OldWndProc = (WNDPROC)SetWindowLongPtr (hControl, GWLP_WNDPROC, (LONG_PTR)NewWndProc);

then, in your new WNDPROC:

NewWndProc (usual args)
{
  switch message
  {
  case paint:
    draw gradient
    return result

  default:
    return CallWindowProc (OldWndProc, ..args..); <- important!
  }
}
Skizz
I think you should put the `return CallWindowProc` call outside the switch, or the compiler might give a warning that "all control paths not returning..". +1 though, very extensive answer. And I did not know about the sub classing :)
Default
+1 Thankyou for the detailed response. Looks very promising.
Alexander Rafferty
Could you point me to the right message(s) to handle. WM_PAINT?
Alexander Rafferty
I tried WM_PAINT, but I need something specific to the client area.
Alexander Rafferty
@Alexander: The painting of the client area is handled by the client, so you'll need to add the gradient code to the child windows of the tab control.
Skizz
A: 

You can use GDI+ LinearGradientBrush:

http://msdn.microsoft.com/en-us/library/ms533914(VS.85).aspx

GDI+ is available in WinXP and later Windows versions, may be installed also on previous Windows versions. GDI+ SDK is part of Windows SDK.

Alex Farber
+1  A: 

In the good old GDI, the GradientFill function does the gradient thing.

GSerg