Page 1 of 1

Direct write or draw on desktop

Posted: Fri Mar 20, 2020 6:48 pm
by nartex
Hi guys,
what is the easiest way? I don’t need any window, only write or draw directly. Is it possible?

Re: Direct write or draw on desktop

Posted: Fri Mar 20, 2020 8:16 pm
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.

Re: Direct write or draw on desktop

Posted: Fri Mar 20, 2020 9:44 pm
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

Re: Direct write or draw on desktop

Posted: Sat Mar 21, 2020 12:17 am
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.

Re: Direct write or draw on desktop

Posted: Sat Mar 21, 2020 12:50 am
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.

Re: Direct write or draw on desktop

Posted: Sat Mar 21, 2020 11:48 am
by Everything
RASHAD wrote:

Code: Select all

  hdc = CreateDC_("DISPLAY", 0, 0, 0)
Btw, why not GetDC_(0)?
Tell me more about the advantages.

Re: Direct write or draw on desktop

Posted: Sat Mar 21, 2020 1:08 pm
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)

Re: Direct write or draw on desktop

Posted: Wed Mar 05, 2025 12:27 pm
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?

Re: Direct write or draw on desktop

Posted: Thu Mar 06, 2025 3:01 pm
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

Re: Direct write or draw on desktop

Posted: Fri Mar 07, 2025 5:06 am
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. :?:

Re: Direct write or draw on desktop

Posted: Sat Mar 08, 2025 8:13 am
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)

Re: Direct write or draw on desktop

Posted: Sat Mar 08, 2025 6:23 pm
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