繁体   English   中英

JNA-更改Windows光标

[英]JNA - Changing the Windows Cursor

我试图将Windows Cursor设置为一个名为cursorOptions的数组中的一个随机游标,该数组保存每种游标类型的值。

int[] cursorOptions = new int[]{32650, 32515, 32649, 32651, 32513, 32648, 32646, 32643, 32645, 32642, 32644, 32516, 32514, 32512};

我正在使用SetCursor(WinDef.LONG hcur)JNA方法(位于第1157行,位于: https : //github.com/br45entei/SWT-Win32-Extension/blob/master/src/org/sf/feeling/swt /win32/extension/jna/win32/User32.java ),但不知道我应该为hcur设置什么值。 我试图从数组中发送新的游标值,但计算结果为false。

WinDef.LONG u = new WinDef.LONG(cursorOptions[rand.nextInt(cursorOptions.length-1)]);
user32.SetCursor(u);
user32.ShowCursor(true);

接下来,我尝试分配不同游标值(IBar游标为65541)的hashCode值,该值评估为true,但没有更改游标值。

WinDef.LONG g = new WinDef.LONG(65541);
user32.SetCursor(g);
user32.ShowCursor(true);

预期什么值的hcur会改变光标? 为什么在SetCursor(WinDef.LONG hcur)中发送时,游标的hashCode值评估为true却没有更改游标?

完整代码:

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.W32APIOptions;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;

class MouseManipulation {

private User32 user32;

MouseManipulation() {
    user32 = User32.INSTANCE;
}
/**
* @return cursorInfo.hCursor Returns the HCURSOR of the cursor on screen
 */
WinDef.HCURSOR getCurrentCursor(){
    CursorInfo cursorInfo = new CursorInfo();
    this.user32.GetCursorInfo(cursorInfo);
    return cursorInfo.hCursor;
}

/*
 * Set the cursor to be a random cursor within the defined scope of cursorOptions
 */
void ChangeMouseCursor() {
    try {
        int[] cursorOptions = new int[]{32650, 32515, 32649, 32651, 32513, 32648, 32646, 32643, 32645, 32642, 32644, 32516, 32514, 32512};
        Random rand = new Random();

        WinDef.HCURSOR hc = getCurrentCursor();
        System.out.println("The HCURSOR: "+hc.toString());
        System.out.println("The HCURSOR hashCode: "+hc.hashCode());

        //I want this to randomly pick from the Array of cursor values and set that as the newest cursor icon 
        //but I have no clue what value for WinDef.LONG should be
        WinDef.LONG u = new WinDef.LONG(cursorOptions[rand.nextInt(cursorOptions.length-1)]);
        user32.SetCursor(u);
        user32.ShowCursor(true);
        System.out.println("When using the array: "+user32.SetCursor(u));

       //This evaluates to true yet doesn't change the cursor icon on screen; 
       //for instance 65541 is the hashCode for IBar and when cursor is NormalPointer it doesn't change.
        WinDef.LONG g = new WinDef.LONG(65541);
        user32.SetCursor(g);
        user32.ShowCursor(true);
        System.out.println("When using the hashCode: "+user32.SetCursor(g));

    }
    //Sometimes the cursor won't be found so we catch the NullPointerException
    catch (NullPointerException e){
        e.printStackTrace();
    }
}

/*
* Take advantage of the JNA 4.5.2 and the JNA 4.5.2 platform to interact with the Win32 API
*/
public interface User32 extends com.sun.jna.Library {
    MouseManipulation.User32 INSTANCE = (MouseManipulation.User32) Native.loadLibrary("user32", MouseManipulation.User32.class, W32APIOptions.DEFAULT_OPTIONS);
    boolean SetCursor(WinDef.LONG hcur);
    int GetCursorInfo(CursorInfo cursorInfo);
    int ShowCursor(boolean bShow);
}

/*
* This class was made to get the cursor's information such as its pointer and then later gets its hashCode value
Credits: deFreitas at: https://stackoverflow.com/questions/47634213/get-mouse-type-in-java-using-jna
*/
public static class CursorInfo extends Structure {

    public int cbSize;//Access must be public
    public int flags;
    public WinDef.HCURSOR hCursor;//Access must be public
    public WinDef.POINT ptScreenPos;

    CursorInfo() {
        this.cbSize = Native.getNativeSize(CursorInfo.class, null);
    }
    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList("cbSize", "flags", "hCursor", "ptScreenPos");
    }
}
}

二手图书馆:

compile group: 'net.java.dev.jna', name: 'jna', version: '4.5.2'
compile group: 'net.java.dev.jna', name: 'jna-platform', version: '4.5.2'

致电:

final MouseManipulation mouse = new MouseManipulation();
mouse.ChangeMouseCursor();

注意:我希望Windows光标即使不在Java应用程序上也可以更改,因此JFrame解决方案和其他类似的东西是不必要的。

我提前谢谢你!

SetCursor效果仅持续到下一次SetCursor调用为止。 在Windows程序中, SetCursor被调用,以响应Windows Manager发送的消息,以便在将鼠标移到Windows或更高版本的Windows(即使鼠标静止)时通过更改光标形状来提供用户反馈。

要永久更改光标,可以使用SetClassLongPtr( hwnd, GCLP_HCURSOR, cursor_handle ); 或子类窗口过程,并处理WM_SETCURSOR消息(不幸的是,这在C语言中):

case WM_SETCURSOR:
    // If you omit test below, you will change cursor also for scrollbars, frames, etc.
    if ( LOWORD( lparam ) == HTCLIENT )
        SetCursor( cursor_handle );
    else
        // This will also handle cursor for scrollbars and frames.
        return DefWindowProc( hwnd, msg, wparam, lparam );
    return TRUE;

cursor_handle必须是进程创建的窗口的有效句柄。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM