Tiny tweak to old tech article

Started by kdmoyers, January 04, 2023, 01:05:35 PM

Previous topic - Next topic

kdmoyers

The code to turn off the monitor in the article below works, but hangs.
change "SendMessageA" to "PostMessageA" fixes it.

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DllCall~Information+Turn~Off~Monitor.txt

It might seem like an insignificant thing, but it took me a couple hours to figure out, so...

-Kirby
The mind is everything; What you think, you become.

td

The reason it hangs is that you are sending a broadcast message to all processes with a GUI interface. One of those processes is however not listening. Since SendMessage is synchronous, it hangs waiting for the process to respond. I would recommend finding a better way to hibernate a monitor as this method is not always reliable even using PostMessage.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Gotcha.  interesting.

I also saw this similar code, which is for autohotkey:
Quote; Turn Monitor Off:  0x0112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
PostMessage, 0x0112, 0xF170, 2,, Program Manager

I see that it sends the message directly to wintitle "Program Manager", maybe that is more correct?
So maybe the winbatch code could be like this?

Code (winbatch) Select
AddExtender("wwctl44i.dll",0,"wwctl64i.dll")
pmwnd=DllHwnd("Program Manager")
;0x0112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER, 2 is OFF
cPostMessage(pmwnd,274,61808,2)


This seems to work well, at least in initial testing.

The mind is everything; What you think, you become.

kdmoyers

And just to beat this obscure horse a little more, I did find another way.
There is a utility called ControlMyMonitor from Nirsoft.com
https://www.nirsoft.net/utils/control_my_monitor.html
It has a command /TurnOff and a bunch more.
The mind is everything; What you think, you become.

td

Neither of your posted scripts using PostMessage works on my Windows 11 system and neither works nor hangs using SendMessage either. I suspect but do not know that the not working part has more to do with the installed device drivers than the OS version. That said there are multiple reports of the SC_MONITORPOWER not working on Windows 8.1 and newer systems on various Websites.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Wow, OK, so this is definitely in the "well, if it works for you, great, but it probably won't work generally" category.
And also it might fail for me when I go to windows 11, so not even a guaranteed solution for me going forward.

(
    subtext: I just got a Elgato StreamDeck+ for Christmas and wanted to configure one of the knobs to
        clockwise: increase monitor brightness
        counterclockwise: decrease monitor brightness
        push: turn off monitor
    and I think I have all that working!  It's nerdy fun!
)
The mind is everything; What you think, you become.

td

Quote from: kdmoyers on January 05, 2023, 08:25:11 AM
(
    subtext: I just got a Elgato StreamDeck+ for Christmas and wanted to configure one of the knobs to
        clockwise: increase monitor brightness
        counterclockwise: decrease monitor brightness
        push: turn off monitor
    and I think I have all that working!  It's nerdy fun!
)

I am completely unfamiliar with the device so forgive me for asking a possibly stupid question; you are talking about using the device to control a PC monitor correct?
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Yes, among many other things.  I also made buttons for programs, websites, switching genres of music, volume mixing of various apps, etc.

It's basically a box of programmable buttons and knobs, and you program them to do all sorts of things.  There's a secondary market of plug-ins for domain-specific stuff like Photoshop or Discord, and built in functions for running programs, media control and hot keys.

The intended use-case is that you can make a simple physical button action that works outside of the context of what's on screen -- great for situations where you are task-saturated, like video streaming or dungeon mastering.  When your head is maxed-out on what you are saying to your audience, you want a quick one-button way to play the dragon attack music.

But for every-day use, I like it because it feels like I'm punching buttons in Apollo Mission Control!   Start Reentry Burn Now! 

No, there probably isn't a serious business case for it, but it's cheaper than a Corvette!
The mind is everything; What you think, you become.

kdmoyers

Yet another way to do the blank screen effect:

%systemroot%\system32\scrnsave.scr /s
The mind is everything; What you think, you become.

td

I had completely forgotten about that one. This more or less works on my system:
Code (winbatch) Select
cmd = Environment("comspec")
run(cmd, "/c scrnsave.scr /s")
exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

I gues this will work but it might need a couple of TimeDelays on either side depending on how the script is called.

Code (winbatch) Select
run("scrnsave.scr", "/s")
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

kdmoyers

Interestingly, the scrnsave.scr trick is not exactly the same effect.  It produces a picture of black pixels.

The cPostMessage(pmwnd,274,61808,2) thing actually turns off the monitor.

I didn't notice the difference until I looked at it in a dark room.
The mind is everything; What you think, you become.

td

You are quite correct. In fact, It was a slow day so I amused myself starting up each of the half-dozen or so Windows 11 screen savers that MSFT supplies with the system...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade