
BOOL GetWindowRect(HWND hWnd,LPRECT lpRect);
返回指定窗口的边框矩形的尺寸。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出。
hWnd hOK = GetDlgItem(hDlg,IDOK)RECT rt
GetWindowRect(hOK,&rt)
Point m_Pntpublic Form1()
{
InitializeComponent()
m_Pnt = new Point( -1, -1 )
pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick)
pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint)
}
void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
// 这里模仿文本框修改坐标点,同时刷新下picturebox,鼠标点哪里,哪里就会画一个红点
m_Pnt = e.Location
pictureBox1.Invalidate()
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse( Brushes.Red, m_Pnt.X - 2, m_Pnt.Y - 2, 5, 5 )
}
想要获取鼠标坐标并不容易,还是一样需要调用两个API函数GetCursorPos() 获取鼠标指针位置(屏幕坐标)然后采用ScreenToClient() 将鼠标指针位置转换为窗口坐标输出即可。上例子。// 程序名称:通过 API 获取鼠标状态
//
#include <graphics.h>
#include <stdio.h>
void main()
{
initgraph(640, 480) // 初始化绘图窗口
HWND hwnd = GetHWnd() // 获取绘图窗口句柄
POINT point
TCHAR s[10]
while(true)
{
GetCursorPos(&point) // 获取鼠标指针位置(屏幕坐标)
ScreenToClient(hwnd, &point)// 将鼠标指针位置转换为窗口坐标
// 获取鼠标按键状态可以用 GetAsyncKeyState 函数,这里不再详述。
// 输出鼠标坐标
sprintf(s, _T("%05d"), point.x)
outtextxy(0, 0, s)
sprintf(s, _T("%05d"), point.y)
outtextxy(0, 20, s)
// 适当延时
Sleep(10)
}
}
同样运行环境需要安装EasyX。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)