Set Window BackColor Gradient?

I want to be able to set a gradient for the window background color without using a picture. I couldn’t find a way on the forums without using a picture.

What I really want to do is set a gradient for the window BackColor property on window open so I won’t have to repaint the window when it gets resized to prevent all the flickering than can occur.

I wrote this today using the gradient code example from @Christoph De Vocht here: https://forum.xojo.com/20988-gradients/0

Soft Declare Function GetDesktopWindow Lib "User32" () As Int32
Dim HWND As Int32 = GetDesktopWindow

Soft Declare Function GetDC Lib "User32" (HWND As Integer) As Int32
Soft Declare Function DeleteDC Lib "GDI32" (hdc As Integer) As Int32
Soft Declare Function CreatePen Lib "GDI32" (fnPenStyle As Integer, nWidth As Integer, crColor As Color) As Int32
Soft Declare Function SelectObject Lib "GDI32" (HWND As Int32, DCDest As Int32) As Int32
Soft Declare Function DeleteObject Lib "GDI32" (HWND As Int32) As Boolean
Soft Declare Function Rectangle Lib "GDI32" (HWND As Int32, nLeftRect As Integer, nTopRect As Integer, nRightRect As Integer, _
nBottomRect As Integer) As Boolean
Const PS_SOLID = 0

dim i as integer
dim y,samt,eamt as double
dim intPen As Int32 
dim c As Color
dim intHandle As Int32 = GetDC(Handle)

for i = 0 to h
  samt = 1 - (i / h)
  eamt = i / h
  c = rgb(_
  (s.red * samt) + (e.red * eamt),_
  (s.green *samt) + (e.green * eamt),_
  (s.blue * samt) + (e.blue * eamt)_
  )
  intPen = CreatePen(PS_SOLID, 4, c)
  call SelectObject(intHandle, intPen)
  call Rectangle(intHandle, 0, 0 + i, w, 1 + i)
  call DeleteObject(intPen)
next

Call DeleteDC(HWND) 

It works, you can set a gradient for the background of a window/control like this without a picture:

SetGBackColor(Window1.Handle, &cff0000, &c0000ff, Window1.Width, Window1.Height)

Of course I still have to call the method each time the window resizes. :confused:

Does anyone know a way to add a gradient using only the BackColor property of a window on open?

Thanks for any advice. :slight_smile:

Backcolor is a property that does not support gradient anyway. Just one plain color.

The simplest way to go would be to use the backdrop property with a picture set to the maximum possible size of the window, so it will never need resizing.

Otherwise, it does make sense to paint again when the window is resized.

It flickers terribly when I use the backdrop property with my customized title bar window. The only way I can prevent it is to use the BackColor property. :frowning:

What do you mean ? A backdrop picture usually does not flicker. Only if you paint it.

I’m using GWL_STYLE to remove the title bar so I can use my own title bar for the window. This removes all of the built-in resizing in Windows so I am doing all of that myself using mouse events. It appears this must be forcing the backdrop property to paint I guess. I’m pretty sure I read in a Xojo flicker help post that the backdrop property shouldn’t be used unless the window size was going to remain static though.

[quote=330831:@Michel Bujardet]Backcolor is a property that does not support gradient anyway. Just one plain color.
[/quote]

I didn’t see your edit. I wasn’t planning on setting a size restriction. I was hoping maybe someone had figured out some other way to do this using BackColor. Thanks.

Where do you apply GWL_STYLE ? If you do that in the Open event, it should not provoke any issue. I do that myself, and never saw extra flicker.

BTW there are other ways to remove the window bar that preserve the resizing abilities. One such technique is to simply render the window bar transparent. And eventually the window frame as well. Incidentally, that is the way Windows 10 makes borderless windows. The 8 pixels frame still exists, it is simply transparent.

See /Example Projects/Platform-Specific/Windows/CustomWindowShape

I use regions to manage transparencies. Instead of a pixel per pixel analysis of the areas to make transparent, it is fairly easy to set the regions.

Then you have in fact a very standard window with no window bar but with all the trimmings.

[quote=330845:@Michel Bujardet]BTW there are other ways to remove the window bar that preserve the resizing abilities. One such technique is to simply render the window bar transparent. And eventually the window frame as well. Incidentally, that is the way Windows 10 makes borderless windows. The 8 pixels frame still exists, it is simply transparent.

See /Example Projects/Platform-Specific/Windows/CustomWindowShape

I use regions to manage transparencies. Instead of a pixel per pixel analysis of the areas to make transparent, it is fairly easy to set the regions.

Then you have in fact a very standard window with no window bar but with all the trimmings.[/quote]

Thanks I’ll definitely look into this. I am using it on open. It does seem to be coming from the custom sizing I am doing. If I turn off GWL_STYLE and restore the title bar and then resize the window the flicker is gone.

@Michel Bujardet So I worked out how to remove the title bar using the coding example from the Custom Window Shape project. Once I figured out how to get the remaining parts of the window combined using the GetWindowRect function I’m nearly there. Do you have any advice on how to get rid of the extra border like areas around the window? I’d like to remove that extra white area. Is there a windows style that removes it? The black area at the top is my desktop background image. Thanks for any advice! :slight_smile:

If you were under Windows 10 they would not appear. I would use the same technique of using regions to get rid of the border. Well, almost all. Under Windows 10, Microsoft keeps one pixel out of the 8 pixels border, so the windows have actually a one pixel border.

I would do just the same, to keep consistency between Windows versions.

[quote=330901:@Michel Bujardet]If you were under Windows 10 they would not appear. I would use the same technique of using regions to get rid of the border. Well, almost all. Under Windows 10, Microsoft keeps one pixel out of the 8 pixels border, so the windows have actually a one pixel border.

I would do just the same, to keep consistency between Windows versions.[/quote]

I am using Win 10 actually, I just updated from Anniversary to Creators. I kept trying to remove the 8 pixel border myself using SM_CYBORDER but it won’t go away. This must be because I was testing inside of this project that was created with an older version of Xojo. It even still has some older properties for the Window left over. I’m sure once I apply this to my actual project it will look correct.

Thanks for all your help. :slight_smile:

Post a link to your code or a sample project with the background colour code and your widow resizing/moving in and I’ll take a look.

Thank you very much for the kind offer to help. I have moved away from that layout and am doing it another way. Thanks again! :slight_smile: