顯示具有 WinAPI 標籤的文章。 顯示所有文章
顯示具有 WinAPI 標籤的文章。 顯示所有文章

2015/10/22

C# 透過Win32控制滑鼠 mouse_event

這個方法除了滑鼠移動之外其他是都可以用的
不管你的x/y輸入正確數值也無法移動到正確的地方

Code:
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GetCurrentPoint
{
    public partial class Form1 : Form
    {
        [DllImport("User32")]
        public static extern void mouse_event(
            int dwFlags,
            int dx,
            int dy,
            int dwData,
            int dwExtraInfo
        );

        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        const int MOUSEEVENTF_MOVE = 0x0001;
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        const int MOUSEEVENTF_WHEEL = 0x0800;
        const int MOUSEEVENTF_XDOWN = 0x0080;
        const int MOUSEEVENTF_XUP = 0x1000;
        const int MOUSEEVENTF_HWHEEL = 0x01000;


        public Form1()
        {
            InitializeComponent();
            int x = 768, y = 1024;
            mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);
        }


    }
}

C# 透過Win32取得滑鼠位置 GetCursorPos

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GetCurrentPoint
{
    public partial class Form1 : Form
    {
        [DllImport("User32")]
        internal extern static bool GetCursorPos(out MousePoint point);

        internal struct MousePoint {
            public int x;
            public int y;
        };

        public Form1()
        {
            InitializeComponent();
            MousePoint point;
            GetCursorPos(out point);
            Console.WriteLine(point.x + "," + point.y);
        }
    }
}

2013/05/22

C#.Net 用WinAPI控制音量

利用WinAPI去控制音量大小,用SendMessage函式控制系統音量大小
測試環境是W7 32bit企業版本,可以成功使用
如果你要用Unicode編碼就用SendMessageW,用ANSI就用SendMessageA