Write contents directly to desktop window

Hi all,

How would I, using VB, write output (text mainly) directly to the desktop without having to modify the desktop background image?  Is that possible?

Thanks

dds
LVL 8
dds110Asked:
Who is Participating?
 
List244Commented:
DDs110, try this:

Add a timer (Timer1) and a picturebox (Picture1) to your form.  Paste the following code:

Option Explicit
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Sub Form_Load()
Timer1.Interval = 100
Picture1.AutoRedraw = True
Picture1.Visible = False
Picture1.ForeColor = vbRed
End Sub
Private Sub Timer1_Timer()
Picture1.Cls
Dim DW As Long, DC As Long
DW = GetDesktopWindow()
DC = GetWindowDC(DW)
BitBlt Picture1.hdc, 0, 0, 100, 100, DC, 0, 0, vbSrcCopy
Picture1.Print "Test"
BitBlt DC, 0, 0, 100, 100, Picture1.hdc, 0, 0, vbSrcCopy
ReleaseDC DW, DC
End Sub

This should do what you want.
0
 
Mark_FreeSoftwareCommented:

you could use active desktop, and then set a webpage as background and alter that
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not the prettiest solution...

But here is how one way to output text directly onto the desktop at a specified location.

Option Explicit

Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Sub Timer1_Timer()
    Dim deskWnd As Long
    Dim desktopDC As Long
    Dim curTime As String
   
    deskWnd = GetDesktopWindow()
    desktopDC = GetWindowDC(deskWnd)
    curTime = Format(Now(), "hh:nn:ss")
    TextOut desktopDC, Screen.Width / Screen.TwipsPerPixelX / 2, Screen.Height / Screen.TwipsPerPixelY / 2, curTime, Len(curTime)
    ReleaseDC deskWnd, desktopDC
End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
dds110Author Commented:
Interesting code Idle_Mind.  Unfortunately, the area behind the text is white.  Not exactly what I was looking for but still interesting.

Mark_FreeSoftware,

Active desktop was my first consideration but I need this to be as seemless as possible.  In other words, I need the user to still be able to choose whatever background image they want.  

Win 98 had a feature where you could display the version of windows on the desktop.  The text was black, the background was transparent, and it didn't mess with the users preferred background image.  That's what I'm after.

Thanks for the help, any other suggestions?
0
 
sakuya_suCommented:
you could get the current desktop image, make ur own copy of it, tell your program to put whatever text you want and then set the desktop background to your program' copy of thta image?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That's exactly what List244s code is doing.  He is using the BitBlt() API to copy a small section of the desktop to a PictureBox.  Then he draws text on top of that and BitBlts the changed image back to the desktop.
0
 
dds110Author Commented:
Just read the recent posts.  I'm not sure if a picture box would do the trick as it would need to be "Click-Through".  I've done this type of thing before with a form and set it's background to transparent using API's and it works.  I was hoping to do something simialar except straight to the desktop.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well...the PictureBox in this case is just used to "hold" the image of the desktop so you can draw on top of it and copy it back.  You aren't actually putting the PictureBox over the spot where the text is drawn on the desktop.  This would not interfere with clicks in that region.

The problem with using a "transparent" form would be that the form itself would capture the clicks and not the desktop.
0
 
List244Commented:
DDS, try the code I gave you, all you need to do is put a timer, and a picturebox, then paste the code.
I have written the code to set up all the properties for you.  Try it out and see if it works for you.  It is
just like Idle_Mind's code, except that it will not have a white background.
0
 
dds110Author Commented:
Ok List244.  I'll have to try it out tonight since I don't have VB available to me at work.

Thanks
0
 
List244Commented:
You're welcome, it should be what you want, if there is a problem with it, let us know, I am sure we can help.
0
 
dds110Author Commented:
I haven't forgotten about this Q, i've been a bit busy.  I should get around to trying out List244's suggestion sometime tonight or tomorrow night.

dds
0
 
List244Commented:
Thanks for the update.
0
 
dds110Author Commented:
Not exactly what I was looking for.  Then again, I'm not sure exactly what I'm looking for.  I'll definitley be diving more into this code though.

Thanks for the assist and the patience.

dds
0
 
Mark_FreeSoftwareCommented:

if you just want to paint text on the desktop, try the code from this page:

http://www.vbaccelerator.com/home/VB/Tips/Get_The_Desktop_Device_Context/article.asp
0
 
List244Commented:
Mark, that is the solution that Idle_mind had already given.

dds110, if you can perhaps say where it is lacking I can try to help improve it.  Otherwise good luck to you with that.
0
 
Mark_FreeSoftwareCommented:
>>Mark, that is the solution that Idle_mind had already given.

with one little difference


the solution Idle_Mind gave, is creating a picture, putting text on the picture and putting that on the desktop

the solution i provided puts the text directly on the desktop
0
 
List244Commented:
No, the solution I gave uses a picture. Idle Mind's is just like what you posted.  The difference between mine and that however,
is that mine does not have a white background nor any background for that matter.
0
 
Mark_FreeSoftwareCommented:
Idle_Mind's solution:

BitBlt Picture1.hdc, 0, 0, 100, 100, DC, 0, 0, vbSrcCopy
'put the desktop (a part of it) in picturebox 1

Picture1.Print "Test"
'print the text "Test" in the upper left corner

BitBlt DC, 0, 0, 100, 100, Picture1.hdc, 0, 0, vbSrcCopy
'put the picture back at the desktop


vbAccelerator example:

DrawText hdc, "vbAccelerator", Len("vbAccelerator"), tR, 0
'draw the text directly on the desktop at a specified position


and it's not about the points i provide this example, just so he has some more reference,
i don't want points for this because it is indeed almost the same as Idle_Mind's solution
0
 
List244Commented:
Again , that was my solution, not Idle Mind's.  And of course you wont get points, it is a closed question.

Idle Mind:

Option Explicit

Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Private Sub Timer1_Timer()
    Dim deskWnd As Long
    Dim desktopDC As Long
    Dim curTime As String
   
    deskWnd = GetDesktopWindow()
    desktopDC = GetWindowDC(deskWnd)
    curTime = Format(Now(), "hh:nn:ss")
    TextOut desktopDC, Screen.Width / Screen.TwipsPerPixelX / 2, Screen.Height / Screen.TwipsPerPixelY / 2, curTime, Len(curTime)
    ReleaseDC deskWnd, desktopDC
End Sub

Which has the same affect as your solution.  I just don't like when experts add input on an already solved problem, especially input
which does not help any further.  Too many times that is the cause of unfair splits, and certification for people that do not deserve it.
Even worse, most of the time they are too out for points that they do not even review what has already been offered.  In my opinion this
really brings down EE.
0
 
Mark_FreeSoftwareCommented:
>>Which has the same affect as your solution.

ugh now i see it, i'm really tired i think...

sorry





>>Even worse, most of the time they are too out for points that they do not even review what has already been offered.
i don't care about the points, i tried to help....
0
 
dds110Author Commented:
Regardless, thanks to all of you.  I'm designing a desktop calendar (simply because I don't want to shell out big bucks for a commercial one).  I'm not sure If I want to go through the headache of writing text or going through the effort of a see through form with buttons.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.