Is there any way to check if a drag and drop is in progress? Some method or win32 api which can be checked? I know I can set AllowDrop and use events but it doesn't work in this case. Basically i want to check, with code, if any drag&drop is in progress.
I had a similar question which I answered myself (after some hours messing about) See - How do I tell if a Drag Drop has ended in Winforms?.
Basically if you do as earwicker suggests you need to set the flag when the drag drop begins near the DoDragDrop call. You will need to unset the flag in both the DragDrop event and in the QueryContinueDrag if the QueryContinueDragEventArgs indicate a drop or a cancel.
The GetCapture API function might be a good start. Basically, when a drag operation starts, the source window "captures" the mouse, which means that it will still receive all mouse events even if the mouse leaves the window.
However, applications can also capture the mouse for other reasons, so this is not 100% reliable. You can try it and see how well it works for you. And with applications doing their own drag&drop handling, there's no way to be sure what is going on anyway.
What about QueryContinueDrag event handler http://msdn.microsoft.com/en-us/library/system.windows.forms.control.querycontinuedrag.aspx ?
You can hook a handler to any control and check if there is a ongoing drag&drop operation and then cancel it if you want to.
Ooops, sorry, I just saw that the guy before me already mentioned that. Me bad.
Assuming it's in the context of just your own code, you could identify all the places in your code where a drag/drop happens, and set a global boolean flag to true for the duration of the operation, then back to false after it finishes.
So the next question is, how are drag/drop operations being started in your application?
Related
I'm having a problem on controlling what touch event should trigger upon touching an object. The problem is, my background has a touch function and it is overlayed by a button, when I tap the button the background also detect a touch function even though I don't want it to happen. How can I make the button only respond when I tap it, or the background only respond when I actually tap on itself.
it's like in Corona SDK you put a "return true" at the bottom of your function to make the touch event only respond on the object rather than going all the way.
found the answer here, Unity docs is really hard to understand especially for beginners, unlike CoronaSDK docs it's very user friendly
here is the link on how to do this:
Credit to guy in here
Edit*
ok sorry for not giving the real scenario,
actually i have datagridview created programatically, i put 2 event, which is mouseclick and ColumnHeaderMouseClick.
currently whenever user click on column header it will trigger mouseclick first then followed by ColumnHeaderMouseClick
can i change the order of the trigger? or can i know when user click i can check whether he click on column header or other place in "mouseclick event"?
I don't know of any way to do that. The events occur according to the events that are actually taking place. In other words, that would be like saying can you walk through a door without opening it first. It doesn't really make sense in the context.
Could you possibly switch the code you are calling for down and click? Or just use click and execute the events in whatever order you like there?
You cannot do this, it does not make logical sense. The MouseDown event necessarily occurs prior to the MouseClick event because the mouse button had to go down in order to initiate a click. When the mouse button goes down, a MouseDown event is raised. The MouseClick event cannot be raised until some time after that.
The order of the mouse events is explicitly documented on MSDN and cannot be modified.
And you say that this was just an example, that you are working on other events. Unfortunately, the answer will be the same regardless. The order in which events are raised is something decided by the programmer who wrote the code that raises those events. There is no mechanism for you, the consumer of the library, to change that order.
Like the mouse events discussed above, the MSDN documentation lists the order of all significant events raised by the WinForms library.
Of course, if you are writing the code that raises the events, you can always modify it to raise them in whatever order you want. But I suspect that much is obvious and not why you are asking this question.
Most C# implementations will typically fire off the event handlers in the order that they are registered to each event. However this is not enforced!
It could change at any time and as such you should not create a dependency on the order of event handlers in your application.
I strongly suggest you take a step back and review your application design for ways to remove this dependency.
As many have suggested on their answers trying to change the event order is not recommended for multiple reasons.
In order to achieve what you want, you would have to re-implement the Windows Forms controls of the .NET Framework. You can also try to hijack the Windows message processing by overriding the WndProc procedure and process directly the messages provided by the OS, but you will learn that the event firing order follows the order in which the OS sends the input provided by the device (mouse) to the affected windows/controls.
i got the solution already, i get the index of the row, if -1 its mean it is a header so i can do the if else already, no need for different event, enough with cell-click event alone can handle both scenario.
sorry for asking in complicated way when the solution is so simple, thanks for helping.
Invoking the WinForms Control.Refresh method is described in MSDN as:
Forces the control to invalidate its client area and immediately redraw itself and any child controls.
I am debugging an intermittent issue that seems to occur when the attached display is switching scanout signals (effectively changing resolution), in which a Control.Refresh appears to not be generating the expected Control.OnPaint call. I am instrumenting the application to get more information, however I'm curious if this could actually be the runtime deciding not to invoke the OnPaint because it detects the display is momentarily blanked out.
This seems unlikely to me, and I expect I'll find some other smoking gun, but I'm posting on the off-chance somebody else has seen this in the wild, and has some recommendations for handling this.
No, it's not guaranteed. If the control does not have a screen to draw on, it won't fire the paint message.
But once the control is on the screen again, the paint message should fire again.
Are you trying to paint debug information? If the control isn't on the screen, what are you expecting to happen when you call refresh?
I need to be able to check if the mouse is within a certain area on the form continuously. I want to be able to do this without the use of a timer, though. How would I go about doing this?
I'm using C# btw.
Have you tried attaching a handler to the MouseMove event, and checking on each movement?
If the area is a screen control, you can use MouseEnter, MouseLeave, MouseHover and MouseMove events.
I think the mouse events suggested by others is the best solution, but as another alternative to timers, you could write a small function to check the mouse and then keep invoking it on your main window dispatcher with an "application idle" priority. This will continuously run your check without freezing the UI.
Again, hooking into the mousemove event is still a cleaner solution, IMO.
If hooking the MouseMove event triggers too often - or if you want to avoid hooking that event on every form, consider hooking the Application.Idle event instead.
This event fires every time the application is about to go idle - all pending messages (including repaints) have been processed and there's nothing left to do. In most WinForms applications, this happens several times a second, providing a good way to do "just in time" processing.
You need to define events over that area.
Use OnMouseEnter and OnMouseLeave together to decide if mouse is in this area or not !
OnMouseEnter until OnMouseLeave means that mouse is still in that area.
My goal is to make a floating toolbar (as its own C# application), and when the user uses the scrollwheel over me I want to change the buttons that are visible. Sounds easy enough, should just be a matter of this one-liner:
MouseWheel += new MouseEventHandler(Form1_MouseWheel);
The problem I am having is that the mouse wheel handler is only invoked when my application has focus. That means the user has to first click, and then mousewheel. That won't do for what I'm trying to do.
I can hook the MouseHover event handler and call form.Activate() then, to get focus. That's suboptimal because if the user uses the scrollwheel immediately after mousing over my application (instead of waiting a little), the focus will still be on the previous app and it'll get the mousewheel event.
A natural thing to do would be to hook the MouseEnter event and call Activate() there, but then instead of my application coming to the front, its icon starts to blink on the task bar. I'm using Win7, but this problem is probably older than this.
Ideally, what I'd like to do would be to detect the mousewheel events without having to worry about whether my application has focus. It would really be better for the previous application to keep input focus, so for example if the user's in Notepad they can type, mouse over to my app, use the scroll wheel, look at what they see and decide to just resume typing in Notepad. Ideally I don't want them to have to click even once in this scenario.
I'll settle for a solution that switches focus to my application, though, if there's no other way.
What I have so far uses C# and Windows Forms, but I'd be open to using something different if that can solve my problems.
So: how can I see those mousewheel events without the user having to click to focus my application first?
If you need to catch mouse events outside your application, you can use a global system hook. There's a good .NET implementation here