A little pit of Setcursorpos and GetCursorPos under Windows

Source: Internet
Author: User

These two days in a special format to edit the mesh gadget, you need to check the mouse movement, generally can be solved with wm_mousemove event, but I used the time to save the timing query + set up the way, that is:

1 voidFrame (void)2 {3     //Do something else ...4     5 Point cursorpos;6GetCursorPos (&cursorpos);7Curmovex = cursorpos.x-Cursorbasex;8Curmovey = Cursorpos.y-Cursorbasey;9Cursorpos.x = cursorbasex, Cursorpos.y =Cursorbasey;TenSetcursorpos (&cursorpos); One      A     //Do something else -}

The simple logic is to periodically set the cursor to the specified position, and then later to check where the cursor is running, to see how much the cursor has moved over time, and to repeat the process to learn how the mouse moves (just a gadget so it's done). But ran up to encounter a strange problem: in the case I did not move the mouse, the cursor position change is not actually 0 (unplug the mouse or there is this phenomenon, not the problem of the mouse), the internet did not find this problem, I was not familiar with Windows programming and could not read the document carefully (at least GetCursorPos and Setcursorpos's entries did not mention this), so I put it down.

Later thought, the problem may be in my setcursorpos time. So I get it once after the set, and then I get the value compared to set and get, then I find that sometimes the two values are different (in the case of not moving the mouse), the difference is small, but enough to interfere with the program to run. Knowing this, there are several solutions immediately: one is to avoid the problem by honestly using Wm_mousemove in the Window event function, which is to ignore the move when the detected cursor moves too slowly, so that it works but not so beautifully. And then the amount of get and set is stored separately, each set immediately after the get once, the next time the offset is calculated using this time get value. Finally, I used a third method, which solves the problem.

The problem itself is not difficult, but it is a small hole, so record it. On the cause of the problem, I am not clear now, in the course of learning will be noted. The preliminary speculation was that when I setcursorpos, the system did not strictly move the cursor to the position I specified, but because of the difference in unit or accuracy the cursor was set to another position near the position I specified (for example, offset by a pixel), and the result was that I did not move the mouse , we find that the values of get and set are sometimes different.

A little pit of Setcursorpos and GetCursorPos under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.