Direct write or draw on desktop

Just starting out? Need help? Post your questions and find answers here.
nartex
New User
New User
Posts: 3
Joined: Wed Sep 03, 2014 6:10 am
Location: Slovakia

Direct write or draw on desktop

Post by nartex »

Hi guys,
what is the easiest way? I don’t need any window, only write or draw directly. Is it possible?
Last edited by nartex on Fri Mar 20, 2020 8:38 pm, edited 1 time in total.
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: Direct write or draw on desktop

Post by Mijikai »

nartex wrote:i guys,
what is the easiest way? I don’t need any window, only write or draw directly. Is it possible?
Do u have an example in another language? - if yes, its possible.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Direct write or draw on desktop

Post by RASHAD »

Hi nartex
You are asking in the wrong forum
But until some MOD will move it to Coding Questions the answer is yes

Code: Select all

  hdc = CreateDC_("DISPLAY", 0, 0, 0)
  If hdc  
    BeginPath_(hdc) 
      Rectangle_(hdc,100,100,210,210) 
      Arc_(hdc,220,100,320,210,0,90,0,90) 
    EndPath_(hdc) 
    
    OldPen = GetCurrentObject_(hdc,#OBJ_PEN) 
    NewPen = CreatePen_(#PS_SOLID,10,$0000FF) 
    SelectObject_(hdc,NewPen) 
    StrokePath_(hdc) 
  EndIf
Egypt my love
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Direct write or draw on desktop

Post by netmaestro »

I assume you'd like to use PureBasic drawing commands to draw to the desktop. This can be accomplished in a straightforward and easy way with just a couple of API calls. Here is the method:

1. Create a transparent image to serve as the drawing surface
2. Draw whatever you like to this surface
3. Open a borderless window to host the surface
4. Apply the 'Layered' extended style to the window
5. Blit the image to the window
5. If desired, provide the ability to move the image around on the desktop or close it

Code Example:

Code: Select all

; Another entry in an endless string of useless programs by netmaestro

CreateImage(0,320,320,32,#PB_Image_Transparent)
StartVectorDrawing(ImageVectorOutput(0))
  VectorSourceColor(RGBA(255,0,0,255))
  AddPathCircle(120,120, 70)
  StrokePath(6)
  VectorSourceColor(RGBA(0,0,255,255))
  AddPathBox(120,80,100,80)
  StrokePath(6)
  VectorSourceColor(RGBA(0,0,255,60))
  AddPathCircle(120,120,68,-38,38)
  AddPathBox(120,80,57,80)
  FillPath(#PB_Path_Winding)
StopVectorDrawing()

Procedure BlitWindow(window, image)
  hDC = StartDrawing(ImageOutput(image))
    With sz.SIZE
      \cx = ImageWidth(image)
      \cy = ImageHeight(image)
    EndWith
    With BlendMode.BLENDFUNCTION
      \SourceConstantAlpha = 255
      \AlphaFormat = 1
    EndWith
    UpdateLayeredWindow_(WindowID(window),0,0,@sz,hDC,@ContextOffset.POINT,0,@BlendMode,2)
  StopDrawing()
EndProcedure

OpenWindow(0,0,0,320,320,"Gdiplus Drawing",#PB_Window_ScreenCentered|#PB_Window_BorderLess)
SetWindowLongPtr_(WindowID(0), #GWL_EXSTYLE, #WS_EX_LAYERED)
CreatePopupMenu(0)
MenuItem(9, "Exit")
BlitWindow(0, 0)

Repeat
  EventID = WaitWindowEvent()
  Select EventID 
    Case #WM_LBUTTONDOWN
      SendMessage_(WindowID(0), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
    Case #PB_Event_Menu
      If EventMenu()=9 : End : EndIf
    Case #WM_RBUTTONDOWN
      DisplayPopupMenu(0, WindowID(0))
  EndSelect
Until EventID = #PB_Event_CloseWindow
Remember, everything in the Windows Operating System is a window. If you use a window for your output it will be persistent and well-behaved on your desktop. Trying to draw directly to the desktop without using a window is folly. It would be like running into a grocery store, grabbing an item off the checkout counter and running like hell. It just goes against all of the structure and order of things.
BERESHEIT
User avatar
oreopa
Enthusiast
Enthusiast
Posts: 283
Joined: Sat Jun 24, 2006 3:29 am
Location: Edinburgh, Scotland.

Re: Direct write or draw on desktop

Post by oreopa »

netmaestro wrote:It would be like running into a grocery store, grabbing an item off the checkout counter and running like hell.
:D

That's just normal shopping practice in some parts of Scotland.
Proud supporter of PB! * Musician * C64/6502 Freak
Everything
Enthusiast
Enthusiast
Posts: 225
Joined: Sat Jul 07, 2018 6:50 pm

Re: Direct write or draw on desktop

Post by Everything »

RASHAD wrote:

Code: Select all

  hdc = CreateDC_("DISPLAY", 0, 0, 0)
Btw, why not GetDC_(0)?
Tell me more about the advantages.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Direct write or draw on desktop

Post by RASHAD »

Hi Everything
You do not want to mess with the DeskTop device context
After all we should finish our work in a proper way
With CreateDC_(?) end the code with DeleteDC_(?) (Good practice)
With GetDC_(?) end the code with ReleaseDC_(hWnd,hDC)

Code: Select all

  hIcon  = ExtractIcon_(#Null,"shell32.dll",55)
  AddSysTrayIcon(0,GetDesktopWindow_(), hIcon)
  SysTrayIconToolTip(0,"Draw to DeskTop")
  hdc = CreateDC_("DISPLAY", 0, 0, 0)
  Repeat    
    If hdc
      Delay(100)  
      BeginPath_(hdc) 
        Rectangle_(hdc,100,100,210,210) 
        Arc_(hdc,220,100,320,210,0,90,0,90) 
      EndPath_(hdc) 
      
      OldPen = GetCurrentObject_(hdc,#OBJ_PEN) 
      NewPen = CreatePen_(#PS_SOLID,10,$0000FF) 
      SelectObject_(hdc,NewPen) 
      StrokePath_(hdc) 
    EndIf
    x + 1
  Until x > 100
  DeleteDC_(hdc)
  ;SHChangeNotify_($8000000, $1000, 0, 0)
SendMessageTimeout_(#HWND_BROADCAST,#WM_SETTINGCHANGE,0, 0,#SMTO_ABORTIFHUNG,10,dwResult)
Egypt my love
XCoder
User
User
Posts: 72
Joined: Tue Dec 31, 2013 9:18 pm

Re: Direct write or draw on desktop

Post by XCoder »

I am using the code posted by Rashad to draw on the desktop. I am using PureBasic 6.20 (x64) on Windows 11 with the compiler option "Enable DPI aware executable enabled".
Sometimes the area inside the shapes is transparent and at other times the area inside the shapes is black. Is there a way of ensuring that the area in the shape is always transparent?
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Direct write or draw on desktop

Post by Axolotl »

Sorry, I cannot help you with that.
BTW: I always take a transparent window, place it on the z-axis (e.g. behind the icons) and then draw in it.

OFF-TOPIC: Why do I see this "MR. GREEN" Smily on the picture (first column) in the overview list? Sorry again for off-topic. :oops: /OFF-TOPIC
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
BarryG
Addict
Addict
Posts: 4130
Joined: Thu Apr 18, 2019 8:17 am

Re: Direct write or draw on desktop

Post by BarryG »

Axolotl wrote: Thu Mar 06, 2025 3:01 pmWhy do I see this "MR. GREEN" Smily on the picture
I was wondering that, too. :?:
Cyllceaux
Enthusiast
Enthusiast
Posts: 510
Joined: Mon Jun 23, 2014 1:18 pm

Re: Direct write or draw on desktop

Post by Cyllceaux »

Its called "Topic Icon"
Normally it's disabled, but you can put one if you are use the phpBB API (HTML POST) directly or over another Program.

Maybe he had a browser Addon or use this while fred upgraded/configured the board. (the to many request problem)
User avatar
Mijikai
Addict
Addict
Posts: 1517
Joined: Sun Sep 11, 2016 2:17 pm

Re: Direct write or draw on desktop

Post by Mijikai »

There is source code for this on the forum but you need to search for it.
I made a lib for this a while back: https://www.purebasic.fr/english/viewtopic.php?t=77538
Post Reply