PIXNET Logo登入

羅羅亞的世界

跳到主文

我要成為世界第一的大劍豪

部落格全站分類:生活綜合

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 1月 14 週三 200914:03
  • C# : 模擬滑鼠鍵盤動作

用程式模擬人按下鍵盤或滑鼠的動作
一樣是用到 user32.dll 裡面的API,所以要先註冊
[DllImport("User32")] public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("User32")] public extern static void SetCursorPos(int x, int y);
[DllImport("User32")] public extern static bool GetCursorPos(out Point p);
[DllImport("User32")] public extern static int ShowCursor(bool bShow);
[DllImport("USER32.DLL")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd);
然後滑鼠有些動作的flag也是固定的,要先宣告好備用。
public enum MouseEventTFlags
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010
}
接下來要使用就簡單囉
滑鼠的部份就是
SetCursorPos(800, 640);  // 設定你要按的位置
mouse_event((int)(MouseEventTFlags.LEFTDOWN), 0, 0, 0, IntPtr.Zero);  // 按下 = =
mouse_event((int)MouseEventTFlags.LEFTUP, 0, 0, 0, IntPtr.Zero); // 起來 沒有起來就等於一直按著囉
鍵盤則是
IntPtr calculatorHandle = FindWindow(null, "小算盤"); 
// 先找到你要送 key message 到哪個視窗,第二個參數給視窗的名稱即可
if (calculatorHandle == IntPtr.Zero)  // 這裡檢查有沒有找到
{
MessageBox.Show("Calculator is not running.");
return;
}
SetForegroundWindow(calculatorHandle); // 然後 focus 在你要送的視窗是
SendKeys.SendWait("1");  // 開始送字元
SendKeys.SendWait("+");
SendKeys.SendWait("1");
SendKeys.SendWait("=");
相當實用!!
(繼續閱讀...)
文章標籤

Roronoa 發表在 痞客邦 留言(4) 人氣(9,923)

  • 個人分類:WinApp
▲top
  • 1月 14 週三 200913:54
  • C# : 螢幕抓圖

就如同一般的螢幕拍照軟體,把螢幕上的區塊存成圖片。

其實很簡單
Image myImage = new Bitmap(410, 410);      // 宣告 image 類別
Graphics g = Graphics.FromImage(myImage);
g.CopyFromScreen(new Point(340, 255), new Point(0, 0), new Size(410, 410)); 
// 取得螢幕上 x = 340 y = 255 為左上角,長寬410的區域
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
this.pictureBox1.Image = myImage;
myImage.Save("c:\\1.jpeg");       //把圖片存起來
(繼續閱讀...)
文章標籤

Roronoa 發表在 痞客邦 留言(0) 人氣(6,087)

  • 個人分類:WinApp
▲top
  • 1月 14 週三 200913:16
  • C# : 全域熱鍵

註冊一個全區域的熱鍵,即是應用程式沒有被 focus 也能收到熱鍵。
網路上找到的通用類別,可輕易註冊想使用的 hotkey 然後自己寫對應的 handler

using System;
using windowcatch.ChatClient;
using System.Runtime.InteropServices;

namespace ChatClient
{
public delegate void HotkeyEventHandler(int HotKeyID);
///
/// System wide hotkey wrapper.
///
/// Robert Jeppesen
/// Send bugs to robert@durius.com
///
public class Hotkey : System.Windows.Forms.IMessageFilter
{
System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
IntPtr hWnd;
///
/// Occurs when a hotkey has been pressed.
///
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
{
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id,
UInt32 fsModifiers, UInt32 vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
///
/// Constructor. Adds this instance to the MessageFilters so that this class can raise Hotkey events
///
/// A valid hWnd, i.e. form1.Handle
public Hotkey(IntPtr hWnd)
{
this.hWnd = hWnd;
System.Windows.Forms.Application.AddMessageFilter(this);
}
///
/// Register a system wide hotkey.
///
/// form1.Handle
/// Your hotkey
/// ID integer for your hotkey. Use this to know which hotkey was pressed.
public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
{
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
}
///
/// Unregister hotkeys and delete atoms.
///
public void UnregisterHotkeys()
{
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
{
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
}
}
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == 0x312) /*WM_HOTKEY*/
{
if (OnHotkey != null)
{
foreach (UInt32 key in keyIDs.Values)
{
if ((UInt32)m.WParam == key)
{
OnHotkey((int)m.WParam);
return true;
}
}
}
}
return false;
}
}
}
(繼續閱讀...)
文章標籤

Roronoa 發表在 痞客邦 留言(0) 人氣(3,218)

  • 個人分類:WinApp
▲top
1

新竹天氣

文章分類

toggle 資訊技能 (2)
  • WinApp (3)
  • Google (2)
  • 大小遊戲 (3)
  • 各式軟體 (1)
  • 美食天地 (2)
  • 永恆瞬間 (9)
  • 好文分享 (14)
  • 心語呢喃 (42)
  • 閒聊記事 (41)
  • 研究領域 (7)
  • 好物資訊 (3)
  • 熱血排球 (24)
  • 暗夜音籟 (122)
  • 精采電影 (14)
  • 未分類文章 (1)

文章搜尋

熱門文章

  • (9,923)C# : 模擬滑鼠鍵盤動作
  • (8,681)自製紅外線模組
  • (105)守城 - The Great Siege

最新留言

  • [16/01/17] timi9350092555 於文章「自製紅外線模組...」發表了一則私密留言
  • [13/12/06] 駿朋 黃 於文章「C# : 模擬滑鼠鍵盤動作...」留言:
    請問如果要同時輸入兩個按鍵該怎麼做呢? 例如:ctrl+C...
  • [11/07/27] chen 於文章「C# : 模擬滑鼠鍵盤動作...」留言:
    程式可正常運行,感謝分享 有個疑問 當我要傳按鍵給其他應用程...
  • [11/04/14] 鄭小冰 於文章「MV 失蹤...」留言:
    你好..可以把這個MV轉寄給我嗎? 謝謝你......
  • [11/03/06] kid 於文章「雲式唱腔...」發表了一則私密留言
  • [11/02/08] 珵珵 於文章「拈花惹草 - 迷你玫瑰...」發表了一則私密留言
  • [10/10/01] joeltra72 於文章「雲式唱腔...」留言:
    很熱鬧的地方,互訪一下,增進感情!...
  • [10/09/17] iAN 於文章「C# : 模擬滑鼠鍵盤動作...」留言:
    最近剛好在找這方面資料 謝謝提供!...
  • [10/06/24] MDLuffy 於文章「拈花惹草 - 迷你玫瑰...」留言:
    好酷~ 養水草,養綠藻都試過了,我還沒嘗試過種花說XD...
  • [10/03/14] 紫媗 於文章「Antbuster v1.2b...」留言:
    希望能常常看到你的更新...

記憶裂痕

訪客來源

參觀人氣

  • 本日人氣:
  • 累積人氣: