2017-07-21 50 views
4

我一直在努力這樣做,不能做的,這是我到:C#獲取當前光標圖標

[DllImport("user32.dll")] 
static extern bool SetSystemCursor(IntPtr hcur, uint id); 

[DllImport("user32.dll", EntryPoint = "GetCursorInfo")] 
public static extern bool GetCursorInfo(out CURSORINFO pci); 

[DllImport("user32.dll", EntryPoint = "CopyIcon")] 
public static extern IntPtr CopyIcon(IntPtr hIcon); 

[StructLayout(LayoutKind.Sequential)] 
public struct CURSORINFO 
{ 
    public Int32 cbSize;  // Specifies the size, in bytes, of the structure. 
    public Int32 flags;   // Specifies the cursor state. This parameter can be one of the following values: 
    public IntPtr hCursor;   // Handle to the cursor. 
    public POINT ptScreenPos;  // A POINT structure that receives the screen coordinates of the cursor. 
} 

public void etc() 
{ 
    IntPtr hwndic = new IntPtr(); 
    CURSORINFO curin = new CURSORINFO(); 
    curin.cbSize = Marshal.SizeOf(curin); 
    if (GetCursorInfo(out curin)) 
    { 
     if (curin.flags == CURSOR_SHOWING) 
     { 
      hwndic = CopyIcon(curin.hCursor); 
      SetSystemCursor(hwndic, OCR_NORMAL); 
     } 
    } 
} 

的問題是,複製的圖標有時是不同於默認鼠標圖標,如果它在等待位置捕捉到它,例如它可能會給我鼠標等待圖標。

我希望獲得正在運行的系統的默認光標圖標(空閒時的圖標),我該怎麼做?

在此先感謝。

+0

看看LoadCursor和IDC_ARROW常量 – Pascalz

+0

@Pascalz不幫我,它沒有檢測到系統的光標是什麼。 –

+0

您可以在複製之前設置Cursor.Current = Cursors.Default嗎? – obl

回答

0

我已經解決了我的問題,經過一些研究後,我得到了一些有趣的東西,可能會一直工作。

而不是試圖保存遊標的圖標並加載它,我想到了一個不同的想法。

我注意到,每當我改變一個光標圖標(讓我們說使用一個代碼,一些隨機圖標),然後每當我去的窗口光標設置圖標不會改變那裏,但沒有應用按鈕來申請之前的設置,因爲Windows認爲我正在使用這些設置。

因此,我將設置更改爲某些隨機的其他設置,但未應用並返回到之前應用的設置,這樣做會在更改之前將光標重置爲原始光標。因此,我剛剛做的是「刷新」鼠標,所以我這樣做,也許如果我可以強制刷新一切都將是完美的,所以我找到了一種方法來使用這個功能:

[DllImport("user32.dll", CharSet = CharSet.Auto)] 
private static extern Int32 SystemParametersInfo(UInt32 uiAction,UInt32 uiParam, String pvParam, UInt32 fWinIni); 

正如我看過MSDN,我發現一些有趣的事情在它的參數,參數「SPI_SETCURSORS(0×57)」,我引用:

「重新加載系統光標設置uiParam參數。零並且pvParam參數爲NULL「。

所以,我已經試過了,和它的工作,例如過程:

[DllImport("User32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)] 
private static extern IntPtr LoadCursorFromFile(String str); 

uint SPI_SETCURSORS = 0x57; 
var NewArrow = LoadCursorFromFile("C:\\Users\\mtnju\\Downloads\\invisible.cur"); // loads some new cursor icon using the LoadCursorFromFile function 
SetSystemCursor(NewArrow, OCR_NORMAL); // sets the new cursor icon using the SetSystemCursor function 
SystemParametersInfo(SPI_SETCURSORS, 0, null, 0);// reloads all of the system cursors 

我認爲它會帶我做5分鐘這樣的事情...我希望它會幫助你們,我真的很感謝那些試圖幫助我的意見。