I play with small but hopefuly nice app for WM6.
I have noticed that there is no MouseUp and MouseDown triggered when playing with stylus (acting as a mouse) on the screen.
That's ok for me, I mean I can live without it.
But there is something else happening that I cannot live with for a change.
When paint on the screen using stylus and read coordinates of the mouse every MouseMove event I get something (lets say for X axis) like: 2,4,6,8,10,12 etc. (every second one)
This same happens for Y axis.
In other words no matter how slow I move stylus I never get coordinates like 2,3,4,5,6 etc.
Using this coordinates to paint I don't get nice continuous line but rather separate dots.
Hopefully I have made myself clear enough.
ps. I forgot about one thing.
To paint on the screen I use pictureBox and bitmap attached to this pictureBox.
When paint on the bitmap I cannot see the evect on the screen unless I refresh the pictureBox or do something like pictureBox.Image = bitmap;
What should I do to see painted dots straight away without refreshing whole pictureBox?
[edit]
List<Point> array = new List<Point>(); // in the header of the class
private void pictureBox_Screen_MouseMove(object sender, MouseEventArgs e) //event handler body
{
array.Add(new Point(e.X, e.Y));
}
As you can see, it is very simple routine. I have deleted all unnecessary noise to make it more clear.
If your only problem is that you need to draw a line by painting each pixel that you catch in mouse move, just connect the dots and you should be set. Even in a "standard" app you will never get each point in MouseMove if the user moves the mouse quickly, so the fact that you don't get every coordinate with the stylus should not be a problem.
Related
[please see update at the end]
I'm an old stack overflow user (as most developers on, you know, Earth), but that's my first question here.
I'm trying to use an "air mouse" for gaming (pointing it to the screen), but as the mouse sensor is a gyroscope, there's some problems with off-screen movement that I'd like to try to fix by software.
With this gyro mouse, when the user moves its arm pointing outside the screen, the cursor stops at the screen limit, which is no problem. However, when he moves his arm back, no matter the distance from the screen, the cursor immediately moves on-screen. This causes a giant difference between the air mouse real position and the cursor.
This could be fixed with a simple control over the number of pixels, and direction, travelled off-screen, in conjunction with some event handling. If I could sum the number of "offscreen" pixels traveled in -X, +X, -Y and +Y, it would be possible to prevent/cancel the mouse move event - or set the cursor to its previous position, at the edge of the screen - until the control tells me that the physical mouse is pointing back to the screen. Just then I'd allow the cursor to move freely.
Maybe this isn't that usefull, but it's an interesting problem, and see this working would be fun as hell!
Currently, based on this great project, I can just state when the mouse is off-screen, but cannot control how far and in which direction it is moving, to properly implement what I'm trying. It seems to me that such kind of information would be too low-level for my current Windows knowledge.
So, to be clear: how can I, in C# (other languages accepted, but I'd have to learn a lot ;), get any kind of "delta position" information or direction of movement, when the cursor is at the limit of screen?
With all due respect, I'm not interested on using different kinds of controllers, as well as "you shouldn't do this" answers. I have this problem to solve, with these elements, and it would be great to make this work!
UPDATE:
Ok, here's my progress up to now.
In order to get raw mouse data and know about the mouse movement 'offscreen', I had to register my application, using some windows API functions, to receive WM_INPUT messages.
Based on a very old code I found, I was able to get mouse raw data and implement exactly what I wanted. Except for the fact that this code is based on a WdnProc callback, and so it only works when my application has the focus. And I need it to also work when the focus is elsewhere - after all, I'm trying to improve the pointing provided by a gyro mouse, for third party games.
It seems that I should use a hook (a good example here), but I have no idea how to hook for input messages. Tried to merge the code of the two links above, but the first one needs the message.LParam that is passed to the WdnProc - which seems to be unavailable when merely hooking mouse events.
Now I'm way out of my league to make some real progress. Any ideas?
One of the simplest solution to get cursor position and then detect its movement regardless where the cursor is to use win32 API from the user32.dll.
Here is a simple code which gets the cursor position in every 10ms using the timer in the C# Windows Form application and displays it as the title of the window.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;
public partial class Form1 : Form
{
Timer timer = new Timer();
public Form1()
{
InitializeComponent();
timer.Interval = 10;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
// do here whatever you want to do
// just for testing...
GetCursorPos(out Point lpPoint);
this.Text = lpPoint.X + ", " + lpPoint.Y;
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point p);
}
I am trying to make a simple tile-based turn based strategy-game.
So I made a test-map with different things like trees, rivers etc.
My next step is to make classes that I call Unit, that holds the X-Y position and other data. Since Unit will be created/destroyed dynamically during the game (in a List>), I also want PictureBox containing the Unit tile-icon to be created/destroyed as needed.
I want to be able to click on a PictureBox, the box will make a mouse event, the event will check which class is connected to the PictureBoxand perform action as needed.
Dynamically Create and Destroy PictureBox in C#? Or should I make a grid of PictureBox?
I would go a different route. Create one single PictureBox and draw everything in it's Paint event. Do a nested loop through your map's x and y coords for the visible tiles and draw out those tiles using Graphics.Draw commands. After you paint the background tiles you can loop though the unit list and paint those right on top. You can do a simple hit-test on mouse down to see what is being clicked on.
This will be less resource intensive and also allow you to do better animations and graphics later (think sprites crossing tile boundaries or tiles larger than a single physical tile).
I have a Drag() method on form_MouseDown event. I also have a click event on the form. The problem is that if I click on the form, MouseDown event gets triggered and it never gets the chance to trigger click event.
What is the best way to solve this issue? I was thinking counting pixels if the form is actually dragged, but there has to be a better way. Any suggestions?
I was thinking counting pixels if the form is actually dragged, but there has to be a better way.
Nope, that's exactly how you have to do it.
This isn't just a software limitation; it's very much a practical one as well. If you think through the problem from a user's perspective, you'll immediately see the problem as well as the solution. Ask yourself, what is the difference between a click and a drag?
Both of them start with the mouse button going down over the object, but one of them ends with the mouse button going back up over the object in the same position and the other one ends with the mouse button going back up in a completely different position.
Since time machines haven't been perfected yet, you have no way of knowing this in advance.
So yes, you need to maintain some kind of a distance threshold, and if the pointer moves outside of that distance threshold while it is down over the object, then you consider it a drag. Otherwise, you consider it a click.
That distance threshold should not be 0. The user should not be required to hold the mouse completely still in order to initiate a click. A lot of users are sub-par mousers. They are very likely to twitch slightly when trying to click. If the threshold is 0, they'll end up doing a lot of inadvertent dragging when they try to click.
Of course, you don't actually have to worry about any of this or compute the drag threshold yourself. Instead, use the Windows default values, obtainable by calling the GetSystemMetrics function and specifying either SM_CXDRAG or SM_CYDRAG. (These might be exposed somewhere by the WinForms framework, but I don't think so. It's just as easy to P/Invoke them yourself.)
const int SM_CXDRAG = 68;
const int SM_CYDRAG = 69;
[DllImport("user32.dll")]
static extern int GetSystemMetrics(int index);
Point GetDragThreshold()
{
return new Point(GetSystemMetrics(SM_CXDRAG), GetSystemMetrics(SM_CYDRAG));
}
In the field of UX/UI, this sort of thing is called hysteresis or debouncing, by analogy to the use of these terms in physics and electronics.
I found this solution, although it is for a double-click and a mouse down events:
void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks ==1)
{
PictureBox pb = (PictureBox)sender;
DoDragDrop((ImageData)pb.Tag, DragDropEffects.Copy);
}
}
source: http://code.rawlinson.us/2007/04/c-dragdrop-and-doubleclick.html
Unfortunatelly, at the point of time when "button-is-pressed" you don't know yet if the desired action is just a click or a drag-drop. You will find it out it later.
For a click, the determinant is "no movement" and "button up".
For a drag, the determinant is "movement" and "button up".
Hence, to disambiguate those interactions, you have to track not only the buttons, but also the movement. You do not need to track the overall movement, only the movement between button-down and button-up is interesting.
Those events are therefore a good place to start/stop the Mouse.Capture mechanisms (to dynamically present drag adorners and drop location hints), or, in simplier form - to store the origin and target of movement vector and check if the distance is > D (even if movement occurred, there should be some safe minimal distance within which the DRAG is canceleed. The mouse is "jaggy" sometimes, and people would really don't like your app to start dragging when they double click at the end of fast mouse pointer movement :) )
I created simple app where the player moves the image to left and right. I am redrawing only the area of the player.
But if the player holds the key, the movement is too fast and you cannot see the image moving fast rather than flickering "something".
Any way to fix it? I am just beginning and am just 14 y/o so please bear with me.
What I am doing is on the key down event I recognize the arrow and then add or subtract to player class X and Y properties. Based on that I invalidate the area around the player.
If the main problem is flickering you want to be double-buffering (drawing to an area of memory - not the screen and then switching that area of memory to be drawn). Try setting these flags on your form (in its constructor):
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
I'm working with a WPF app, more specifically a Canvas with draggable elements.
Once an item is being dragged, I'd like to limit the scope of cursor movement to inside the canvas where the items are being dragged about.
The event which can start a drag is shown below
private void WidgetCanvas_PreviewHeaderLeftMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
... logic to figure out if this is a valid drag, blah blah blah ...
this.IsDragging = true;
// TODO: clip the available cursor movement to actual width of Canvas
}
On the Preview-MouseUp, I'd like to simply "unclip" the cursor movement back to it's normal state.
I'll be monitoring the movement of the mouse once I start dragging (PreviewMouseMove), so worst case, I could manually check the position of the mouse and keep it constrained to the canvas, but that seems a little ugly.
Anyone have a better way to limit the cursor boundaries?
There's no clean way to do this and the not-so-clean ways will make your mouse cursor "jitter" at the border of the clipping area.
Moreover, I'd question if this is really a good idea. The user should really own the mouse and he or she generally gets frustrated when you try to artificially limit things that he or she owns.
If you want to provide feedback when the mouse leaves your canvas, maybe you could leave the item being dragged stuck along the border while the mouse button is still down? This would tell the user that he or she has left the target area without trying to impose limitations on where the mouse can go.
Good luck!
You should be able to do it using the ClipCursor native API.