java鼠标坐标是像素值
3个回答
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener;
class myMouseListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
String s="当前鼠标坐标:"+x+','+y;
MouseMove.lab.setText(s);
}
public void mouseDragged(MouseEvent e){};
}
public class MouseMove extends JFrame{
public static JLabel lab=new JLabel();
public MouseMove() {
}
public static void main(String [] args)
{
// MouseMove fm=new MouseMove("鼠标坐标测试");
JFrame fm=new JFrame("鼠标坐标测试");
JPanel fp=new JPanel();
fp.addMouseMotionListener(new myMouseListener());//对在面板上的鼠标移动进行监听。
Container con=fm.getContentPane();
fp.add(lab);
con.add(fp);
fm.setSize(500,400);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener;
class myMouseListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
String s="当前鼠标坐标:"+x+','+y;
MouseMove.lab.setText(s);
}
public void mouseDragged(MouseEvent e){};
}
public class MouseMove extends JFrame{
public static JLabel lab=new JLabel();
public MouseMove() {
}
public static void main(String [] args)
{
// MouseMove fm=new MouseMove("鼠标坐标测试");
JFrame fm=new JFrame("鼠标坐标测试");
JPanel fp=new JPanel();
fp.addMouseMotionListener(new myMouseListener());//对在面板上的鼠标移动进行监听。
Container con=fm.getContentPane();
fp.add(lab);
con.add(fp);
fm.setSize(500,400);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
展开全部
是像素值
当分辨率变化时,其实如果能够得到相对屏幕的鼠标坐标,相对窗口的也就出来了。
我试了一下百分比的情况,不行。
代码如下:
HDC hdc=GetDC(NULL); //获得屏幕设备描述表句柄
int i=GetDeviceCaps(hdc,HORZRES); //查询屏幕水平分辨率,并返回在变量i中
int j=GetDeviceCaps(hdc,VERTRES); //查询屏幕垂直分辨率,并返回在变量j中
ReleaseDC(NULL,hdc); //释放屏幕设备描述表
sprintf(str,"水平分辨率:%d,垂直分辨率:%d\n",i,j);
//MessageBox(NULL,str,"屏幕分辨率",0);
Sleep(3000);
POINT lpPoint = {0};
::GetCursorPos(&lpPoint);
ScreenToClient(hwind, &lpPoint);
lpPoint.x = 553 * (float)i/1152;
lpPoint.y = 166 * (float)j/864;
SendMessage(hwind,WM_LBUTTONDOWN,MK_LBUTTON,MAKELONG(lpPoint.x, lpPoint.y));
SendMessage(hwind,WM_LBUTTONUP,MK_LBUTTON,MAKELONG(lpPoint.x, lpPoint.y));
sprintf(str,"水平坐标:%d,垂直坐标:%d\n",lpPoint.x,lpPoint.y);
MessageBox(NULL,str,"鼠标位置",0);
当分辨率变化时,其实如果能够得到相对屏幕的鼠标坐标,相对窗口的也就出来了。
我试了一下百分比的情况,不行。
代码如下:
HDC hdc=GetDC(NULL); //获得屏幕设备描述表句柄
int i=GetDeviceCaps(hdc,HORZRES); //查询屏幕水平分辨率,并返回在变量i中
int j=GetDeviceCaps(hdc,VERTRES); //查询屏幕垂直分辨率,并返回在变量j中
ReleaseDC(NULL,hdc); //释放屏幕设备描述表
sprintf(str,"水平分辨率:%d,垂直分辨率:%d\n",i,j);
//MessageBox(NULL,str,"屏幕分辨率",0);
Sleep(3000);
POINT lpPoint = {0};
::GetCursorPos(&lpPoint);
ScreenToClient(hwind, &lpPoint);
lpPoint.x = 553 * (float)i/1152;
lpPoint.y = 166 * (float)j/864;
SendMessage(hwind,WM_LBUTTONDOWN,MK_LBUTTON,MAKELONG(lpPoint.x, lpPoint.y));
SendMessage(hwind,WM_LBUTTONUP,MK_LBUTTON,MAKELONG(lpPoint.x, lpPoint.y));
sprintf(str,"水平坐标:%d,垂直坐标:%d\n",lpPoint.x,lpPoint.y);
MessageBox(NULL,str,"鼠标位置",0);
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是的 是像素值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询