10th
Ноя

Как вызывать WIN API в C#?

Posted by Chas under Пост-обзор

Ну вот пример работы с WinAPI:

class MonitorPower
    {
        private const int MONITOR_ON = -1;
        private const int MONITOR_OFF = 2;
        private const int WM_SYSCOMMAND = 0×0112;
        private const int SC_MONITORPOWER = 0xF170;
        private const int HWND_BROADCAST = 0xffff;

        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd,
                                                 uint Msg,
                                                 IntPtr wParam,
                                                 IntPtr lParam);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        public static void MonitorOff()
        {
            IntPtr foregroundWindow = GetForegroundWindow();
            if (foregroundWindow == IntPtr.Zero)
                foregroundWindow = (IntPtr)HWND_BROADCAST;

            SendMessage(foregroundWindow, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MONITOR_OFF);
        }

        public static void MonitorOn()
        {
            IntPtr foregroundWindow = GetForegroundWindow();
            if (foregroundWindow == IntPtr.Zero)
                foregroundWindow = (IntPtr)HWND_BROADCAST;

            SendMessage(foregroundWindow, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)MONITOR_ON);
        }
    }

Тема на форуме

Похожие статьи