Jump to content


Photo

[Bug] -Background doesn't work when creating TreeView


  • Please log in to reply
7 replies to this topic

#1 jballi

jballi
  • Members
  • 940 posts

Posted 05 July 2012 - 08:23 AM

Preface: This bug only occurs on the Treeview control. All other GUI objects work as expected.

When using a default control color, i.e. "gui Color", the "-Background" option does not work correctly when creating a TreeView control. The workaround is to apply the option via GUIControl after the control has been created.

Example of the bug and the workaround:

#NoEnv
#SingleInstance Force
ListLines Off

;-- GUI Options
gui Color,,FFFF80  ;-- Initial background color

;-- Buttons
gui Add,Button,       gBG1,      %A_Space%  Background 1  %A_Space%
gui Add,Button,x+0 wp gBG2,      Background 2
gui Add,Button,x+0 wp gBG3,      Background 3
gui Add,Button,x+0 wp gBGRandom, Random
gui Add,Button,x+0 wp gBGDefault,Default

;-- Start section
gui Add,Text,xm w250 h1 Section,%A_Space%  ;-- Spacer

gui Add,Text,w250,TreeView 1`nControl uses and retains the initial background color (if any).
gui Add,TreeView,w250 r3 vTV1
Loop 3
    TV_Add("Parent " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w250,TreeView 2`nCustom background color set with the "+Background" option.
gui Add,TreeView,w250 r3 +BackgroundF0F0F0 vTV2
Loop 3
    TV_Add("Parent " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w250,TreeView 3`nAttempt to remove background color with "-Background" option.  The text background color is removed but the control background color remains.  Click on any of the background color buttons above to see that "-Background" option has not been applied to the control's background color.
gui Add,TreeView,w250 r3 -Background vTV3
Loop 3
    TV_Add("Parent " . A_Index)

gui Add,Button,xm gRemoveBGTV3,Permanently remove`n%A_Space% background color of TV3 %A_Space%
gui Show
return


GUIClose:
GUIEscape:
Exitapp


BG1:
gui Color,,FFCECE
return


BG2:
gui Color,,CEFFFF
return


BG3:
gui Color,,CEE7FF
return


BGRandom:
SetFormat Integer,Hex
Random RandomColor,0x000000,0xFFFFFF
gui Color,,%RandomColor%
SetFormat Integer,D
return


BGDefault:
gui Color,,Default
return


RemoveBGTV3:
GUIControl -Background,TV3
return
Thank you for your consideration.

#2 just me

just me
  • Members
  • 1175 posts

Posted 06 July 2012 - 05:04 AM

Can't confirm this for Vista32 and AHK 1.1.07.03 (U32), -Background option works as expected here.

But there is an issue when you click one of the "Background" buttons (i.e. processing Gui, Color,, ...). The text of the selected items in treeview #1 doesn't have any background color afterwards.

#3 jballi

jballi
  • Members
  • 940 posts

Posted 06 July 2012 - 05:56 AM

Interesting. I'm using XP SP3 and the bug occurs on Basic and L Unicode. The condition you described does not occur on my PC.

I'm wondering what happens with the other standard controls. If you have time, try this script. Everything works perfectly for me except the TreeView control. It would be nice if someone with Windows 7 could test it as well.

#NoEnv
#SingleInstance Force
ListLines Off

;-- GUI Options
gui Margin,8,1
gui Color,,FFFF80  ;-- Initial background color

;-- Buttons
gui Add,Button,       gBG1,      %A_Space%  Background 1  %A_Space%
gui Add,Button,x+0 wp gBG2,      Background 2
gui Add,Button,x+0 wp gBG3,      Background 3
gui Add,Button,x+0 wp gBGRandom, Random
gui Add,Button,x+0 wp gBGDefault,Default
;;;;;gui Add,Button,x+20 wp gReload,Reload...

;-- Start section
gui Add,Text,xm w190 h1 Section,%A_Space%  ;-- Spacer

;-- Column 1
gui Add,Text,w190 Center,-- Column 1 --`nControls Not Affected By`nBackground Color

gui Add,Text,,Button
gui Add,Button,,%A_Space%  Button  %A_Space% 
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Checkbox
gui Add,Checkbox,,Option 23
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,ComboBox
gui Add,Combobox,r5,Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,DateTime
gui Add,DateTime,w190
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Hotkey
gui Add,Hotkey
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Progress
gui Add,Progress,w190,80
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Slider
gui Add,Slider,w190,40
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Radio
gui Add,Radio,,Option 72
gui Add,Radio,,Option 73
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Text
gui Add,Text,w190 cBlue,Text not affected by the background color.
gui Add,Text,h1  ;-- Spacer




;-- Column 2
gui Add,Text,ys w190 h1 Section,%A_Space%  ;-- New column
gui Add,Text,w190 Center,-- Column 2 --`nVarious

gui Add,Text,,DropDownList 1
gui Add,DropDownList,,Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,DropDownList 2`nBackground color removed with "-Background" option.
gui Add,DropDownList,-Background,Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,Edit 1
gui Add,Edit,w190 r3,Just some pre-loaded text.  Add your own.
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,Edit 2`nBackground color removed with "-Background" option.
gui Add,Edit,w190 -Background
gui Add,Text,h1  ;-- Spacer

gui Add,Text,,ListBox 1
gui Add,ListBox,w190 r3,Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,ListBox 2`nBackground color removed with "-Background" option.
gui Add,ListBox,w190 r3 -Background,Item 1|Item 2|Item 3|Item 4|Item 5|Item 6|Item 7|Item 8|Item 9
gui Add,Text,h1  ;-- Spacer




;-- Column 3
gui Add,Text,ys w190 h1 Section,%A_Space%  ;-- New column
gui Add,Text,w190 Center,-- Column 3 --`nListView

gui Add,Text,w190,ListView 1`nControl uses and retains the initial background color (if any).  
gui Add,ListView,w190 r3 Checked,Column 1|Column 2|Column 3
Loop 3
    LV_Add("","Row " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,ListView 2`nBackground color removed with "-Background" option.  Note: The "+BackgroundDefault" option can also be used.
gui Add,ListView,w190 r3 Checked +BackgroundDefault,Column 1|Column 2|Column 3
Loop 3
    LV_Add("","Row " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,ListView 3`nCustom background color set with the "+Background" option.
gui Add,ListView,w190 r3 Checked +BackgroundFFE0FF,Column 1|Column 2|Column 3
Loop 3
    LV_Add("","Row " . A_Index)
gui Add,Text,h1  ;-- Spacer




;-- Column 4
gui Add,Text,ys w190 h1 Section,%A_Space%  ;-- New column
gui Add,Text,w190 Center,-- Column 4 --`nTreeView

gui Add,Text,w190,TreeView 1`nControl uses and retains the initial background color (if any).
gui Add,TreeView,w190 r3
Loop 3
    TV_Add("Parent " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,TreeView 2`nBackground color removed with "-Background" option.  Note: The "+BackgroundDefault" option can also be used.
gui Add,TreeView,w190 r3 -Background
Loop 3
    TV_Add("Parent " . A_Index)
gui Add,Text,h1  ;-- Spacer

gui Add,Text,w190,TreeView 3`nCustom background color set with the "+Background" option.
gui Add,TreeView,w190 r3 +BackgroundF0F0F0
Loop 3
    TV_Add("Parent " . A_Index)
gui Add,Text,h1  ;-- Spacer




;-- Column 5
gui Add,Text,ys w190 h1 Section,%A_Space%  ;-- New column
gui Add,Text,w190 Center,-- Column 5 --`nTabs

gui Add,Text,w190,Tab 1`nBackground color does not affect the control but controls within the tabs are affected.
gui Add,Tab2,w190 h100,Tab 1|Tab 2|Tab 3
gui Add,Text,,Edit control within the tab.
gui Add,Edit,w170 r3,What the heck?
gui Tab
gui Add,Text,h1  ;-- Spacer

gui Add,Text,xs y+10 w190,Tab 2 with +Theme and -Background options.  Background color does not affect the control but controls within the tabs are affected.
gui Add,Tab2,w190 h100 +Theme -Background,Tab 1|Tab 2|Tab 3
gui Add,Text,,Edit control within the tab.
gui Add,Edit,w170 r3,What the heck, part 2.
gui Tab
gui Add,Text,h1  ;-- Spacer

gui Show
return


GUIClose:
GUIEscape:
Exitapp


BG1:
gui Color,,FFCECE
return


BG2:
gui Color,,CEFFFF
return


BG3:
gui Color,,CEE7FF
return


BGRandom:
SetFormat Integer,Hex
Random RandomColor,0x000000,0xFFFFFF
gui Color,,%RandomColor%
SetFormat Integer,D
return


BGDefault:
gui Color,,Default
return


Reload:
Reload
return
Thank you for your consideration.

#4 just me

just me
  • Members
  • 1175 posts

Posted 06 July 2012 - 09:04 AM

Right now I only can test on XPSP3 and confirm the issue for AHK 1.1.07.01 (U32). I'll test your new example in the evening or tomorrow on Vista and Win7 if still needed.

#5 Lexikos

Lexikos
  • Administrators
  • 8844 posts

Posted 06 July 2012 - 10:39 AM

I have tested on Windows 2000, XP, Vista, 7 and 8. Only XP and 2000 exhibited the problem.

GuiType::AddControl assumes that if you specify -Background or +BackgroundDefault, no change in background colour is needed for the new control, because by default, it uses the system colours. By contrast, when you use GuiControl, it sends the control a TVM_SETBKCOLOR message with lParam set to GetSysColor(COLOR_WINDOW).

The problem appears to be that if TVM_SETBKCOLOR hasn't been sent to the TreeView control, it sends a WM_CTLCOLOREDIT message to the GUI whenever the background is painted. The GUI responds the same way it would to an Edit control, to implement the custom background colour. Note that this message is not supposed to be sent by TreeView controls, so this behaviour seems incorrect. It was apparently fixed in Vista.

The following can be found in the AutoHotkey source code:
[color=green]//case GUI_CONTROL_LISTVIEW:     Can't reach this point because[/color][color=red] WM_CTLCOLORxxx is never received for it[/color][color=green].
//case GUI_CONTROL_TREEVIEW:     Same (verified).[/color]
However,

Like any other control, you can handle the WM_CTLCOLOR message for the tree view control too. You can set the background color and it works - kind of. When the control draws the tree items, it ignores the color set by the WM_CTLCOLOR handler and uses the system color. The net effect is that portions of the control not covered by any tree item gets the custom color but the tree items have the system color as the background.
Source: Setting a background color

Although this article is very old (even older than XP), the behaviour it describes matches what I see on XP.

In any case, for now the problem can be worked around by explicitly setting the background colour. You can retrieve the system-defined colour as follows:
SetFormat IntegerFast, H  [color=green]; Required for +Background and similar usages.[/color]
WindowColor := DllCall("GetSysColor", "int", 5) . ""

The text of the selected items in treeview #1 doesn't have any background color afterwards.

Do you mean the selection background turns grey? That's supposed to happen when the control loses the keyboard focus. Edit controls have the same sort of behaviour.

Everything works perfectly for me except the TreeView control. It would be nice if someone with Windows 7 could test it as well.

I saw no problems on any OS, except for what was described above.

#6 jballi

jballi
  • Members
  • 940 posts

Posted 07 July 2012 - 05:37 AM

In any case, for now the problem can be worked around by explicitly setting the background colour. You can retrieve the system-defined colour as follows:

SetFormat IntegerFast, H  [color=green]; Required for +Background and similar usages.[/color]
WindowColor := DllCall("GetSysColor", "int", 5) . ""

This works but setting the background color to CLR_DEFAULT also works. For example:
CLR_DEFAULT:=0xFF000000
gui Add,TreeView,w250 r3 +Background%CLR_DEFAULT%
...which is strange because that is what I thought the "+BackgroundDefault" option is supposed to do.

Thank you for your attention/feedback.

#7 Lexikos

Lexikos
  • Administrators
  • 8844 posts

Posted 07 July 2012 - 07:27 AM

0xFF000000 is not a valid colour value, and will have the same effect as 0x7FFFFFFF (or FFFFFF, i.e. WHITE) due to internal use of _tcstol. If it weren't for this, +Background0xFF000000 would be indistinguishable from -Background or +BackgroundDefault, since all of three would set opt.color_bk to the same value.

#8 jballi

jballi
  • Members
  • 940 posts

Posted 07 July 2012 - 10:15 AM

You're right. CLR_DEFAULT (i.e. 0xFF000000) only appeared to work because white was my default background color. When I changed the background color to test it again, it didn't work.

FYI, I also discovered that the GetSysColor API returns the color in BGR format. It must be converted to RGB before it can be used by the +Background option.

Thank you for your feedback.


Edit: Final workaround. Just in case anyone needs it.
;-- The "-Background" and "+BackgroundDefault" options don't work when creating
;   a TreeView control on Windows 2000 or Windows XP.  The following is a
;   workaround (provided by Lexikos).  Should work on all versions
;   AutoHotkey.
SetFormat IntegerFast,H
COLOR_WINDOW:=5                                                ;-- Window background
Color:=DllCall("GetSysColor","Int",COLOR_WINDOW)               ;-- Returns BGR
Color:=((Color&0xFF)<<16)+(Color&0xFF00)+((Color>>16)&0xFF)    ;-- Convert to RGB
gui Add,TreeView,w190 r3 vTV3 +Background%Color%
SetFormat IntegerFast,D
...or you can apply the "-Background" option via GUIControl after the control has been created. For example:
gui Add,TreeView,w190 r3 vTV3
GUIControl -Background,TV3