[C#]使用WM_SYSCOMMAND訊息控制螢幕模式切換

要控制螢幕的開啟、關閉,可透SendMessage發送WM_SYSCOMMAND訊息,wParam參數傳入SC_MONITORPOWER,lParam參數則傳入螢幕的模式。

參數方面參閱WM_SYSCOMMAND Message,裡面清楚的帶出WM_SYSCOMMAND為0x0112、SC_MONITORPOWER為0xF170、與其對應的lParam。

image

image

使用上就像下面這樣:


[DllImport(“user32.dll”)]
private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

  const int SC_MONITORPOWER = 0xF170;
  const int WM_SYSCOMMAND = 0x0112;
  ...
  SendMessage(-1,  WM_SYSCOMMAND, SC_MONITORPOWER , -1);
  SendMessage(-1,  WM_SYSCOMMAND, SC_MONITORPOWER , 1);
  SendMessage(-1,  WM_SYSCOMMAND, SC_MONITORPOWER , 2);
</pre></div>  <p> </p>  <p>這邊為方便後續使用,將程式整理成類別,有需要的自行取用。</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c3cd5a55-1615-41d4-9ccd-45dd058acb8b" class="wlWriterSmartContent"><pre name="code" class="c#">

public static class MonitorControler
{
[DllImport(“user32.dll”)]
private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);

    const int SC_MONITORPOWER = 0xF170;
    const int WM_SYSCOMMAND = 0x0112;
    //const int SC_SCREENSAVE = 0xF140;

    public enum MonitorMode : int
    {
        MONITOR_ON = -1,
        MONITOR_STANBY = 1,
        MONITOR_OFF
    }

    public static void ChangeMonitorState(MonitorMode mode)
    {
        SendMessage(-1, WM_SYSCOMMAND, SC_MONITORPOWER, (int)mode);
    }

    public static void MonitorOff()
    {
        ChangeMonitorState(MonitorMode.MONITOR_OFF);
    }

    public static void MonitorOn()
    {
        ChangeMonitorState(MonitorMode.MONITOR_ON);
    }

    public static void MonitorStandBy()
    {
        ChangeMonitorState(MonitorMode.MONITOR_STANBY);
    }
}</pre></div>  <p> </p>  <h2>Link</h2>  <ul>   <li>WM_SYSCOMMAND Message</li> </ul>