实现效果:
知识运用:
API函数SendMessage向窗口发送消息
private static extern bool SendMessage(IntPtr hWnd, int wMsg,int wParam,int IParam)
//hWnd: 窗口句柄 wMsg: 将要发送的消息 wParam,IParam :消息的参数 依据发送的消息而定
实现代码:
private const int WM_SYSCOMMAND = 0x0112; private const int SC_SCREENSAVE = 0xf140; [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hWnd,int wMsg,int wParam,int IParam); private void button1_Click(object sender, EventArgs e) { SendMessage(this.Handle, WM_SYSCOMMAND, SC_SCREENSAVE, 0); }