我試圖改變窗口的光標(默認爲視窗定製計劃)到我的自定義光標(它起名割繩子):以編程方式在Windows中更改自定義鼠標光標?
有任何想法來改變所有的遊標(箭頭,忙,幫助選擇,鏈接選擇,...)給我切繩子? 請幫忙,謝謝!
我試圖改變窗口的光標(默認爲視窗定製計劃)到我的自定義光標(它起名割繩子):以編程方式在Windows中更改自定義鼠標光標?
有任何想法來改變所有的遊標(箭頭,忙,幫助選擇,鏈接選擇,...)給我切繩子? 請幫忙,謝謝!
如果要更改默認的鼠標指針主題:
你可以改變它在註冊表中:
有跡象表明,發揮作用的三個主要註冊表項。
註冊表項HKEY_CURRENT_USER \控制面板\遊標含有活性用戶光標
1a)中的值的下方這是不同類型的遊標
1b)中的方案來源指定光標方案的類型目前正在使用。
不同的值是:
「0」 - Windows默認
「1」 - 用戶計劃
「2」 - 系統方案
註冊表項HKEY_CURRENT_USER \控制面板\遊標包含用戶定義的遊標方案(即方案來源= 1)
註冊表項HKEY_LOCAL_MACHINE \ SOFTWA RE \ Microsoft \ Windows \ CurrentVersion \ Control Panel \ Schemes包含系統光標方案(即,計劃源= 2)
如果你已經改變了路徑在HKCU \控制面板\遊標遊標類型之一,並意識到它沒有做任何事情。你是對的,只是更新一個鍵 - 例如HKCU \ Control Panel \ Cursors \ Arrow是不夠的。你必須告訴窗口加載新的光標。
這就是SystemParametersInfo這個呼叫的來源。試試這個,讓我們繼續並將HKCU \ Control Panel \ Cursors \ Arrow更改爲C:\ WINDOWS \ Cursors \ appstar3.ani(假設你有這個圖標),然後調用SystemParametersInfo。
在AutoHotkey的腳本:
SPI_SETCURSORS := 0x57
result := DllCall("SystemParametersInfo", "UInt", SPI_SETCURSORS, "UInt", 0, "UInt", 0, "UInt", '0')
MsgBox Error Level: %ErrorLevel% `nLast error: %A_LastError%`nresult: %result%
翻譯成C#:
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
const int SPI_SETCURSORS = 0x0057;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDCHANGE = 0x02;
SystemParametersInfo(SPI_SETCURSORS, 0, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
更改爲默認的Windows光標
現在棘手的問題。如果你看看HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Control Panel \ Schemes,你會注意到「Windows Default」被定義爲「,,,,,,,,,,,,,」或換句話說沒有指針到實際的遊標!
現在該做什麼?別擔心。您所要做的就是將不同的遊標類型設置爲空字符串,然後照常進行SystemParametersInfo調用。實際上,您可以在任何方案中將任何遊標類型設置爲空字符串,並且Windows將默認爲「Windows默認」方案中的遊標類型。
REF:
https://thebitguru.com/articles/programmatically-changing-windows-mouse-cursors/3
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Bitmap bmp = Properties.Resources.Image1;
bmp.MakeTransparent(Color.White);
IntPtr hIcon = bmp.GetHicon();
Icon ico = Icon.FromHandle(hIcon);
Cursor cur = new Cursor(hIcon);
using (FileStream fs = new FileStream(@"c:\temp\test.cur", FileMode.Create, FileAccess.Write))
ico.Save(fs);
cur.Dispose();
ico.Dispose();
DestroyIcon(hIcon);
// Test it
cur = new Cursor(@"c:\temp\test.cur");
this.Cursor = cur;
}
[DllImport("user32.dll")]
extern static bool DestroyIcon(IntPtr handle);
}
}
感謝您的回覆,但我想改變這一切的鼠標光標(手,箭,忙,幫助選擇...)不僅爲當前光標 –
你可以這樣做。獲取Cursor.cur
文件以加載自定義光標。在MouseLeave
上設置窗體的默認光標。
public static Cursor ActuallyLoadCursor(String path)
{
return new Cursor(LoadCursorFromFile(path));
}
[DllImport("user32.dll")]
private static extern IntPtr LoadCursorFromFile(string fileName);
Button btn = new Button();
btn.MouseLeave += Btn_MouseLeave;
btn.Cursor = ActuallyLoadCursor("Cursor.cur");
private static void Btn_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
}
我想改變所有的窗口遊標不僅爲窗體。謝謝你的幫助! –
我試圖改變這個值,但沒有任何反應,它仍然是默認光標 –
看到更新 - 你需要調用SystemParametersInfo –
我在這裏得到了一些錯誤:http://i.imgur.com/Agns2vV.png。 pvParam不接受空值。我嘗試將pvParam值更改爲「0」,它工作正常。非常感謝! –