C# Form2.ShowDialog() and Mouse Cursor - c#

(NET Compact Framework 3.5, Windows CE 6.0)
I want to hide mouse cursor.
So, I use Curosr.Hide()
I have two forms, Form1, Form2.
The size of Form2 is smaller than Form1.
PictureBox1 is in Form1.
When PictureBox1 is clicked, Form2 will be opened. (modal)
At this point, the mouse cursor suddenly appears outside area of Form2.
MouseDonw PictureBox1 -> Form2.ShowDialog -> Show MouseCursor
I have never done Cursor.Show()
Why does the mouse cursor appear?
Added the following
I moved Form2.ShowDialog() from 'MouseDown Event' to 'MouseUp Event'. then it is resolved. Why?

First, form show and other 'actions' usually are done with a mouse click event. That is fired after mouse down and mouse up.
If you break the normal sequence, ie show a form on a mouse down event, the GUI is in 'Mouse down/move' mode, for example to drag an element or draw a line.
As each element can show/hide the mouse cursor, the Windowing System recognizes youre Cursor Hide on the second form, but the first form still shows the mouse cursor as the Mouse Up event is not done.
If you would like to know more about the Basics, you should look at a native WndProc and how are Window Messages are handled. Programming Windows by Charles Petzold is still the bible for Windows programming.

Related

Hide mouse cursor when mouse cursor interacts with application

Problem
When the mouse cursor interacts with the application, the mouse cursor should be hidden. The problem is: I don't know how.
Question
How can I hide the mouse cursor when the mouse cursor interacts with the application?
You can subscribe for to something like Window.Activated event or similar one to know when user is interacting with the window. And call
Cursor.Hide();
to hide the cursor. Or
Cursor.Show();
to display it

Windows form with a transparent background that can be clicked through

INTRODUCTION
Using C# or VB.NET. I'm trying to make a form's background transparent; this form will be overlaped to other window, it will be a top-most window, so the transparent form (and its controls) must have the ability that they must not receive focus and they must can be clicked trough, this means if for example I perform a left-click on the transparent background, then the window on background of that (in the Z-order window) is the window that must receive the click instead.
Notes:
For avoiding the focus I'm overriding the CreateParams property as explained here.
For making my form transparent, I'm calling Win32 DwmExtendFrameIntoClientArea function and also using SharpDX library as explained here. But I think this really doesn't matter with the question itself.
PROBLEM
I'll show a demostration of what I mean using images. Here below is a image of a form (with no transparency, just to simplify understanding) overlapped to a window of a text editor program; note that my form doesn't receive focus. Well, the problem is when I do click on the form's background (or one of its controls) the window on background (the text editor window) still have focus but it can't receive the click.
Here is the same image of above but with a transparent form:
RESEARCH
I'm not really sure about what to investigate, so I'm going blind trying to find something useful in a trial-and-error stage by overriding the Window procedure (WndProc) of the transparent form to test related windows messages, like WM_NCHITEST or WM_MOUSEACTIVATE message as said here:
Windows form with a transparent background that cannot be clicked through
Make a form not focusable in C#
How do I create an "unfocusable" form in C#?
You can do this by sending click (mouse up & mouse down) messages to the window underneath the transparent window using WinAPI.
PostMessageA
You'll need to find the window underneath the point you require.
WindowFromPoint
You'll have to translate the position of the click events accordingly since messages are processed based on relative window position, not absolute screen position.
I actually did this quite successfully to automatically play a facebook game many years ago.
Check the RAD designer in Visual Studio.
Is the label docked to fill?
Where is the main form clickable?
The transparent color is click-though in the main parent, however, components will still retain clicks.

How to determine if user clicks out of ContextMenuStrip?

I am building winform application without any form shown (opacity of form is 0 and ShowInTaskbar property is false). It is accessible only from tray Notify icon. When users clicks with left mouse button on it, the contextMenustrip menu will be show. Because I want to detect LEFT mouse button click I can't use ContextMenu Property of NotifyIcon.
I would like, that if users clicks whenever out of the menu, it should hide. I don't have any idea how I can do that...
If I have shown form, I could detect Deactivate form event and then hide my menu, but in described situation it looks harder.
1) Instead of setting opacity to 0 better set WindowState = FormWindowState.Minimized
2) You cant detect mouse clicks outside of your program as they are controlled by other programs or your OS, but handling Leave or MouseLeave event should help you

Borderless windows form with Windows 7 dragging enhancements

In Windows 7, you can drag a typical window to the top of your screen and you'll see an outline of the window maximized. If you let go, Windows will maximize the form. Likewise, you can take an already maximized window and "pull" it down to restore it to the normal windowstate. I'd like to leverage these enhancements in a borderless form within my C# WinForms app.
My form is borderless so I can create a custom titlebar and minimize/maximize/close buttons. I can get my form to move by dragging the titlebar but I don't know how to plug into the Windows 7 enhancements that give me the maximize outline or the "pull down" feature.
Any suggestions? Thanks!
I did something like this in Delphi 7, years ago.
1) Form - OnCLick - capture mouse position on entire screen (maybe this link will help: http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx), save information about button pressed in private variable (example: buttonPressed = true)
2) Form - OnMouseMove - according to new mouse position, move your window
3) Form - OnMouseUp - buttonPressed = false. If cursor coordinates are in top of screen (x[0..screenW] y[0..10] ) --> maximize.
Just a hint, maybe it will help you.

Responding to keypress and mouse click on fully-transparent panel

I've created a form that's maximized over the entire screen.
Within that form, and sized to fill the entire form, I've placed a panel with background color set to Red. The form's TransparencyKey is set to Red.
Therefore, the Panel is like a "keyhole" - you can see the desktop that's directly underneath it.
When the user clicks on the panel, OR, presses a key on the keyboard, I want to take action.
But, because the panel is completely transparent, when it is clicked or a key is pressed, nothing happens. If I make the panel non-transparent (setting it's background color to Blue, for example), it does respond to clicks.
What's the best way to get the panel to respond to clicks and keypresses?
Do I have to hook all mouse an keyboard events on the entire system or is there a simpler way?
The panel does not take in text input, you can set your main form to handle the keypress event.
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_KeyPress);
http://screensnapr.com/v/VovWAi.png
As for getting the mouse location on your panel, you can use the mouseclick event
this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);
http://screensnapr.com/v/DB3kCQ.png
I have tested it, and it works on a fullsize transparent panel
I've discovered that I can use GetAsyncKeyState to respond to both keypresses and mouse clicks.
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);
Then, I can call GetAsyncKeyState(Keys.LButton) and GetAsyncKeyState(Keys.Escape), for example. Much simpler than hooking all keyboard and mouse events.

Categories

Resources