Re: Write on the Desktop, create\access functions, select font

[ DelphiLand FAQ ] [ Delphi Tutorials -- by DelphiLand ]

Posted by DelphiLand Team on December 17, 2004 at 15:04:58:

In Reply to: Write on the Desktop, create\access functions, select font posted by bob P12404 on December 12, 2004 at 21:42:34:

: I've created a little app that will write on the desktop what ever you put in the code. Need help in modifying it please. I'll post my questions first then the source that I'm using. Thanks for any pointers...

: Questions :
: 1) How can I create a modal form that will be used to alter\select the font type\size on the code below. After\before the text is sent to the desktop, I'd like to be able to modify the displayed font.

: 2) How can I create a modal form that will enable me to set the desktop screen cooridinates dynamically, instead of just using "DeskTopCanvas. TextOut(400,300,'Here is some text, it works, yippeeeee!!!');" in the code below. I'd like to use some kind of input box, don't know how to set it up.

: 3) As it is set up now, it prints out the text to the desktop, but not quite, really. Part of it prints out on the form, and when you move\drag the form, it moves whatever text that is on the form with it. So it seems like it's not quite printing to the desktop perse. Is there a Z-index something that I need to know about?

: 4) How can I make it transparent behind the text, ie let whatever that's on the desktop behind the text shine through. Just have the text, not the "white" background behind the text show.


1) At runtime, you can select a font with a FontDialog component. In the example below, for simplicity let's drop that component on the main form.
A. When the user clicks button btnGetFont, FontDialog1 is opened.
B. Next, if the user selects a font, the selected font is applied to label lblFont. Later on, it will be available to us when we write some text to the DeskTop.

procedure TTexTopMainForm.btnGetFontClick(Sender: TObject);
FontDialog1.Font := lblFont.Font;
if FontDialog1.Execute then begin
lblFont.Font := FontDialog1.Font;
lblFont.Caption := lblFont.Font.Name;

2) You can let the user type the coordinates in two Edit-boxes, for example: EditX for the X coordinate and EditY for the Y coordinate. Later on, we will convert these two strings to integer values and use them in the TextOut function.

procedure TTexTopMainForm.btnWriteClick(Sender: TObject);
X, Y: integer; // coordinates for text
hDesktop: hDC; // handle to Desktop
DeskTopCanvas: TCanvas;
X := StrToInt(EditX.Text);
Y := StrToInt(EditY.Text);
hDesktop := GetWindowDC(GetDesktopWindow);
DeskTopCanvas := TCanvas.Create;
DeskTopCanvas.Handle := hDesktop; // point to the DeskTop
DeskTopCanvas.Font := lblFont.Font; // same font as lblFont
DeskTopCanvas.Brush.Style := bsClear; // transparent background
DeskTopCanvas.TextOut(X, Y, EditTxt.Text);
ReleaseDC(GetDesktopWindow, hDesktop);

3) Let's look at some concepts of the Windows "DeskTop".

The Desktop is built up from several windows of different dimensions, that are "painted" on top of each other: the DeskTop background image, next all the icons (one small window per icon), next the TaskBar, and finally a window per application that is open.

If in your Delphi application you paint something on the DeskTop, it is painted directly over the *top* of all the stuff that is visible: over the Desktop background where it is visible, over the visible icons, and over the parts of the application windows that are visible. You can't paint "under" a window.

After you painted something on top of something, when that thing is moved or hidden, whatever was painted on top of it also moves or is hidden. For example, if you painted part of your text on top of a window of application, and next you move that window, then what you painted is moving along. Or when you hide a window and show it again, the Windows system will repaint that window, but without the part that you painted yourself.

4) The transparency is controlled by the property "Brush" of a Canvas. When you set Brush to bsClear, the background will shine through. See the example under (2):

DeskTopCanvas.Brush.Style := bsClear;

Related Articles and Replies

[ Delphi Forum -- by DelphiLand ] [ Delphi Tutorials -- by DelphiLand ]