shut off monitor will flash display,by use SendMessage command.

microsoft 1 Reputation point
2022-06-20T07:16:44.397+00:00

SendMessage function Win32 API Windows and Messages

Similar problems monitor-will-flash-before-shut-off-or-monitor-will.html

screen recording onedrive

using System;  
using System.Runtime.InteropServices;  
  
namespace ConsoleApp  
{  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
            const uint WM_SYSCOMMAND = 0x0112;  
  
            const uint SC_MONITORPOWER = 0xF170;  
  
            IntPtr HWND_BROADCAST = new IntPtr(0xffff);  
  
            // Shut down  
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);  
  
            // Open the  
            //SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);  
        }  
  
        [DllImport("user32.dll")]  
        public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int lParam);  
    }  
}  

SendMessage function (winuser.h)Win32 API Windows and Messages

Similar problems monitor-will-flash-before-shut-off-or-monitor-will.html

screen recording onedrive

using System;  
using System.Runtime.InteropServices;  
  
namespace ConsoleApp  
{  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
            const uint WM_SYSCOMMAND = 0x0112;  
  
            const uint SC_MONITORPOWER = 0xF170;  
  
            IntPtr HWND_BROADCAST = new IntPtr(0xffff);  
  
            // Shut down  
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);  
  
            // Open the  
            //SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);  
        }  
  
        [DllImport("user32.dll")]  
        public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int lParam);  
    }  
}  
 
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,652 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,006 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 85,806 Reputation points
    2022-06-20T08:57:01.457+00:00

    I still cannot reproduce this...
    Maybe you can test by sending the message only to the Desktop :

        DefWindowProc(GetDesktopWindow(), WM_SYSCOMMAND, (int)SC_MONITORPOWER, (IntPtr)2);  
    

    with :

        [DllImport("User32.dll", SetLastError = false)]  
        public static extern IntPtr GetDesktopWindow();  
          
        [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]  
        public static extern int DefWindowProc(IntPtr hWnd, uint uMsg, int wParam, IntPtr lParam);
    
    1 person found this answer helpful.

  2. 佳朋 洪 0 Reputation points
    2023-04-23T07:58:43.5566667+00:00

    I also encountered this problem. The solution is not to use HWND_BROADCAST, but to create another window, send the message and then destroy the window.

    
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
    [DllImport("user32.dll")]
    static extern int DestroyWindow(IntPtr hWnd);
    
    [DllImport("user32.dll", SetLastError=true)]
    public static extern IntPtr CreateWindowEx(uint dwExStyle, string lpClassName, IntPtr cap, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
    
    const int SC_MONITORPOWER = 0xF170;
    const int WM_SYSCOMMAND = 0x0112;
    const int MONITOR_OFF = 2;
    
    static void Main(string[] args)
    {
        IntPtr w = CreateWindowEx(0, "Button", IntPtr.Zero, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
        if (w != IntPtr.Zero)
        {
            SendMessage(w, WM_SYSCOMMAND, (IntPtr) SC_MONITORPOWER, (IntPtr) MONITOR_OFF);
            DestroyWindow(w);
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.