Главная страница | назад





Article #20689: How do I set the desktop icon color in code, or make the text behind them transparent?

Question: How do I set the desktop icon color in code, or make the text behind them transparent?

Answer: To do this, you will have find the "SysListView32" window (which is the desktop icon list view window). First, you will have to find the main parent window called "Progman". It has a child window called "SHELLDLL_DefView" that has the "SysListView32". You can use the API call FindWindow to find the "Progman" window, and then FindWindowEx to find the child window. When you finally have the right handle to the "SysListView32" window, you can use the ListView macro's ListView_SetTextBkColor and ListView_SetTextColor to set the color's to whatever you want.

Below is a sample unit that does this for you. Simply use the procedure SetDesktopIconColor to set the desktop icon color. If the parameter Trans is true, it makes the background transparent, otherwise it used the given Background color.

Download the DeskIcons.pas file

DeskIcons.pas Unit

unit DeskIcons;
interface
uses Graphics; // Definition of TColor
procedure SetDesktopIconColor(Forground, Background: TColor; Trans: Boolean);
procedure SetDefaultIconColors;
implementation
uses Windows, CommCtrl; // Definition of HWND and ListView_XXXXX
procedure SetDesktopIconColor(Forground, Background: TColor; Trans: Boolean);
{
This procedure set's the desktop icon text color
to a given color with the option to add a transparent background.
}
var
Window: HWND;
begin
// Find the right window with 3 calls
Window := FindWindow('Progman', 'Program Manager');
// FindWindowEx is used to find a child window
Window := FindWindowEx(Window, HWND(nil), 'SHELLDLL_DefView', '');
// SysListView32 is the desktop icon list view
Window := FindWindowEx(Window, HWND(nil), 'SysListView32', '');
// Use the macro to set the background color to clear
if Trans then
ListView_SetTextBkColor(Window, $ffffffff) // back color
else
ListView_SetTextBkColor(Window, Background); // back color
ListView_SetTextColor(Window, Forground); // foreground color
// now send a redraw to the icons to redraw the new color
ListView_RedrawItems(Window, 0, ListView_GetItemCount(Window) - 1);
UpdateWindow(Window); // force the redraw to take effect immediatly
end;
procedure SetDefaultIconColors;
{ This set's the colors to be whatever is currently
stored by windows }
var
Kind: Integer;
Color: TColor;
begin
Kind := COLOR_DESKTOP;
Color := GetSysColor(COLOR_DESKTOP);
SetSysColors(1, Kind, Color);
end;
end.

Last Modified: 23-FEB-00