How to avoid mouse move for 10 sec - c#

I using User32.dll, running the application,pressing buttons getting information.
My problem is when i pull some data ,but when i move my mouse over spesific element it can stop this process ,sow i need to move my mouse to safe place and make it stay there for 2sec.
I found the way to move it to safe place
Cursor.Position = new System.Drawing.Point(3000, 0);
But how i make it stay there/stop moving for 2sec..

Well you could make a timer that loops this code:
Cursor.Position = new System.Drawing.Point(3000, 0);
But that would be inefficient.
So I suggest Making your form implement IMessageFilter.
Then add the following code to the form:
Rectangle BoundRect;
Rectangle OldRect = Rectangle.Empty;
private void EnableMouse()
{
Cursor.Clip = OldRect;
Cursor.Show();
Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x201 || m.Msg == 0x202 || m.Msg == 0x203) return true;
if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true;
return false;
}
private void DisableMouse()
{
OldRect = Cursor.Clip;
// Arbitrary location.
BoundRect = new Rectangle(50, 50, 1, 1);
Cursor.Clip = BoundRect;
Cursor.Hide();
Application.AddMessageFilter(this);
}
This will hide the cursor making it unable to move it and disable the right and left mousebuttons.

Related

How to resize a borderless MDI form?

I want this:
Pretty much, my form is resizeable if BorderStyle is set to None and isMDIContainer = false;
But, how do I get my form resizeable if BordeStyle is set to None and isMDICOntainer = true?
https://gyazo.com/6fe87f127a3b2768c152e64d372593c1
This is an example. You can see the form is resizeable just fine. But as soon as the MDI comes in play, it doesn't work anymore.
Here is the current code:
private const int cCaption = 62;
private const int cGrip = 16;
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
// e.Graphics.FillRectangle(Brushes.Blue, rc);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
{ // Trap WM_NCHITTEST
Point pos = new Point(m.LParam.ToInt32());
pos = this.PointToClient(pos);
if (pos.Y < cCaption)
{
m.Result = (IntPtr)2; // HTCAPTION
return;
}
if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17; // HTBOTTOMRIGHT
return;
}
}
base.WndProc(ref m);
}
The explanation from Jimi was right. The OnPaint event only draws your Rectangle on the MainForm, while what you see is the MdiClient control. This control covers the background of the MainForm (like you set a panel control and set Dock = fill), so you cannot see and click the rectangle at bottom right to resize.
One way for you to be able to see and click on the rectangle for resizing is set the padding for MainForm, like this:
protected override void OnClientSizeChanged(EventArgs e)
{
if (this.WindowState != lastState || lastState == FormWindowState.Normal)
{
lastState = this.WindowState;
OnWindowStateChange(e);
}
base.OnClientSizeChanged(e);
}
private void OnWindowStateChange(EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
Padding = new Padding(0);
}
else
{
Padding = new Padding(7);
}
}
So at normal window state (not fullscreen), the MdiClient will not cover all surface of mainform.
I find the color of this is not good. So you may want to change the background of the main form that fit the background of MdiClient, or use this approach to draw rectangles around your form for resizing like normal.
How to resize borderless form from all edges

Using sendmessage in C# to send keystrokes to a window only works when my preview is over source window

I am creating a "simulation" thumbnail that sends any input through to the window it is previewing, using wndproc and sendmessage. It works fine when the application I am sending to is displayed underneath my preview, but does not work when sending to a application that is not.
My cords seem fine, I don't think it is that.
It works just fine when the window I am sending to is displayed on the same monitor but is not active (behind stuff).
I tested it with multiple programs, same result.
UPDATE: After further testing, it is not a problem with monitors. It only works when the preview is over the source window (even with other windows in between). Thinking it is some event I didn't pass through. Something else that is interesting is that when holding mouse down on the part of the preview that works, I can't move to the part that doesn't; I assume it thinks it is the end of the window.
Update 2: Used Spy++, the messages are exactly the same when it works and when it doesn't.
Code:
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0204 || m.Msg == 0x0205 || m.Msg == 0x02004 || m.Msg == 0x0020 || m.Msg == 0x02A3)
{
m.Result = new IntPtr(WindowManagerNativeMethods.SendMessage(this._windowPtr, (uint)m.Msg, (int)m.WParam, (int)m.LParam));
} else if (mouseMsgsWithCoords.Contains(m.Msg)) { // mouse messegas where I need to chage the coords
IntPtr xy = m.LParam;
double x = unchecked((short)(long)xy);
double y = unchecked((short)((long)xy >> 16));
if (m.Msg == 0x0084 || m.Msg == 0x020E || m.Msg == 0x020A)
{
x -= this.Location.X;
y -= this.Location.Y;
}
x *= ((double)(this._sourceWindowRext.Right - this._sourceWindowRext.Left) / (double)this.Size.Width);
y *= ((double)(this._eveWindowRext.Bottom - this._sourceWindowRext.Top) / (double)this.Size.Height);
uint lParam = (((uint)(ushort)y << 16) | (uint)(ushort)x);
m.Result = new IntPtr(WindowManagerNativeMethods.SendMessage(this._windowPtr, (uint) m.Msg, (int)m.WParam, (int)lParam));
} else
{
base.WndProc(ref m);
}

Draggable borderless window in CefSharp

I want to implement borderless window with drag logic on some HTML element. I found some working examples (like frameless window for Chrome) and this is what I've tried:
.title-area
{
-webkit-app-region: drag;
}
<div class='title-area'>
A draggable area
</div>
Then, in C# code I've implemented IDragHandler class:
internal class PromtDragHandler : IDragHandler
{
bool IDragHandler.OnDragEnter(IWebBrowser browserControl, IBrowser browser, IDragData dragData, DragOperationsMask mask)
{
return false;
}
void IDragHandler.OnDraggableRegionsChanged(IWebBrowser browserControl, IBrowser browser, IList<DraggableRegion> regions)
{
}
}
Method OnDraggableRegionsChanged fires once at start, OnDragEnter fires when I'm dragging some text of the element "title-area". But I'm not sure what to do next to make my window move?
UPDATE. As mentioned in comments, CefTestApp support this drag feature. In the source code we have method OnSetDraggableRegions which is called from DragHandler:
void RootWindowWin::OnSetDraggableRegions(
const std::vector<CefDraggableRegion>& regions) {
REQUIRE_MAIN_THREAD();
// Reset draggable region.
::SetRectRgn(draggable_region_, 0, 0, 0, 0);
// Determine new draggable region.
std::vector<CefDraggableRegion>::const_iterator it = regions.begin();
for (;it != regions.end(); ++it) {
HRGN region = ::CreateRectRgn(
it->bounds.x, it->bounds.y,
it->bounds.x + it->bounds.width,
it->bounds.y + it->bounds.height);
::CombineRgn(
draggable_region_, draggable_region_, region,
it->draggable ? RGN_OR : RGN_DIFF);
::DeleteObject(region);
}
// Subclass child window procedures in order to do hit-testing.
// This will be a no-op, if it is already subclassed.
if (hwnd_) {
WNDENUMPROC proc = !regions.empty() ?
SubclassWindowsProc : UnSubclassWindowsProc;
::EnumChildWindows(
hwnd_, proc, reinterpret_cast<LPARAM>(draggable_region_));
}
}
I'm still not quite understanding, how exactly information about dragable regions (which fires only once at start) helps to move window? Can someone explain me this logic or provide C# equivalent of this code?
Take a look at: https://github.com/qwqcode/CefSharpDraggableRegion
This you can specify -webkit-app-region: drag in CSS to tell CefSharp which regions are draggable (like the OS's standard titlebar).
UPDATE2. I DID IT. This is what I've added to my form code:
IntPtr DragableRegionNative = Native.CreateRectRgn(0, 0, 0, 0);
void RegionsChangedCallback(DraggableRegion[] Regions)
{
Native.SetRectRgn(DragableRegionNative, 0, 0, 0, 0);
if (Regions == null)
return;
foreach (var Region in Regions)
{
var RegionNative = Native.CreateRectRgn(
Region.X, Region.Y,
Region.X + Region.Width,
Region.Y + Region.Height);
Native.CombineRgn(DragableRegionNative, DragableRegionNative, RegionNative,
Region.Draggable ? (int)Native.CombineRgnStyles.RGN_OR : (int)Native.CombineRgnStyles.RGN_DIFF);
Native.DeleteObject(RegionNative);
}
}
Point dragOffset = new Point();
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
dragOffset = this.PointToScreen(e.Location);
dragOffset.X -= Location.X;
dragOffset.Y -= Location.Y;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
Point newLocation = this.PointToScreen(e.Location);
newLocation.X -= dragOffset.X;
newLocation.Y -= dragOffset.Y;
Location = newLocation;
}
}
void chromewb_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
{
if (chromewb.IsBrowserInitialized)
{
ChromeWidgetMessageInterceptor.SetupLoop(chromewb, (m) =>
{
if (m.Msg == (int)Native.WM.WM_LBUTTONDOWN)
{
var point = Native.ParsePoint(m.LParam.ToInt32());
if (Native.PtInRegion(DragableRegionNative, point.X, point.Y))
this.InvokeEx(() => Native.PostMessage(this.Handle, (uint)m.Msg, m.WParam, m.LParam));
}
});
}
}
As you can see, it is enough to intercept WM_LBUTTONDOWN event from chrome browser, then check if mouse point belongs to a title region and, if so, send this message to main form. As soon as form will get WM_LBUTTONDOWN event, build-in form methods OnMouseDown and OnMouseMove do the other work.

Setting and using MousePosition

Hey, i have kinda silly problem, i need to set Point Pos as mouse current position in one if statement and in another statement move mouse cursor to setted position. First i need assign global Point variable but then Cursor moves to assigned variable and i don't want that happens.
Part of source:
protected override void WndProc(ref Message m)
{
Point Pos = new Point(0, 0);
if (m.Msg == 0x0312)
{
int id = m.WParam.ToInt32();
if (id == 0)
{
Pos.X = MousePosition.X;
Pos.Y = MousePosition.Y;
}
if (id == 1)
{
Cursor.Position = (Pos);
}
}
base.WndProc(ref m);
}
If you intend to capture the mouse position and restore it later then you have to make the Pos variable a field of your class instead of a local variable of the method. Like this:
private Point Pos;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312) // Trap WM_HOTKEY
{
switch (m.WParam.ToInt32()) {
case 0: Pos = Cursor.Position; break;
case 1: Cursor.Position = Pos; break;
}
}
base.WndProc(ref m);
}
Point pos = this.PointToClient(Cursor.Position);
Will return your actuall mouse position.

unselectable node in TreeView

I have TreeView control on winform. I desire to make several nodes unselectable. How can I achive this.
There is only one idea in my mind - custom drawn nodes, but may be more easier way exists? Please advice me
I have already try such code in BeforeSelect event handler:
private void treeViewServers_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Parent != null)
{
e.Cancel = true;
}
}
But effect it gained is not appropriate. Node temporary get selection when I am holding left mouse button on it.
Thanks in advance!
You could completely disable mouse events in case you click on a not-selectable node.
To do this, you have to override TreeView a shown in the following code
public class MyTreeView : TreeView
{
int WM_LBUTTONDOWN = 0x0201; //513
int WM_LBUTTONUP = 0x0202; //514
int WM_LBUTTONDBLCLK = 0x0203; //515
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN ||
m.Msg == WM_LBUTTONUP ||
m.Msg == WM_LBUTTONDBLCLK)
{
//Get cursor position(in client coordinates)
Int16 x = (Int16)m.LParam;
Int16 y = (Int16)((int)m.LParam >> 16);
// get infos about the location that will be clicked
var info = this.HitTest(x, y);
// if the location is a node
if (info.Node != null)
{
// if is not a root disable any click event
if(info.Node.Parent != null)
return;//Dont dispatch message
}
}
//Dispatch as usual
base.WndProc(ref m);
}
}

Categories

Resources