Drag n Drop Operations across MDI Child Forms - drag-and-drop

I am trying to perform a simple drag-n-drop operation starting from one button in one MDI child form to another button in a different MDI child form. For some reason however the DragDrop event never gets fired when I attempt to drag one button to the other. It may be worth noting that when I drag the button my cursor becomes the black-cancel icon.
My code:
#region ActivatesDragDropControl
[DllImport ("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private const int WM_NCACTIVATE = 0x0086;
#endregion
private void button1_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(LocationNode, DragDropEffects.Link);
// to deactivate
SendMessage(Handle, WM_NCACTIVATE, 0, 0);
}
private void button1_DragDrop(object sender, DragEventArgs e)
{
//never gets here...
}
private void button1_DragEnter(object sender, DragEventArgs e)
{
// to activate
SendMessage(Handle, WM_NCACTIVATE, 1, 0);
}

OK so I played around a bit more and using DragEnter isn't enough; I had to set the DragEventArgs' Event value. In my case:
e.Effect = DragDropEffects.Link;

Related

Calling C# click event without actually clicking the button not working

I have below code written inside C# Form application where I am trying to get x,y and z co-ordinates from Leap Motion device.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//controller.EventContext = WindowsFormsSynchronizationContext.Current;
button1.Click += new EventHandler(button1_Click);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
// Initialize the Controller object which connects to the Leap motion service
// and captures the hand tracking data
Controller controller = new Controller();
//Get the most recent tracking data using the Frame object
Frame frame = controller.Frame();
for (int h = 0; h < frame.Hands.Count; h++)
{
// Initialize the Hand in the given frame
Hand leapHand = frame.Hands[h];
// Get the "Pointer" finger of current hand which refers to where a person is pointing
Finger leapFinger = leapHand.Fingers[1];
// Prepare a vector which will store the co-ordinate values of the tip of the pointer
Vector currentPosition = leapFinger.StabilizedTipPosition;
textBox1.Text = Convert.ToString(currentPosition.x);
textBox2.Text = Convert.ToString(currentPosition.y);
textBox3.Text = Convert.ToString(currentPosition.z);
}
}
}
However, I need to explicitly click the button1 to display.
Any idea what's wrong ?
Here is an overly simple example of one way to do it. I'm not saying it's the best way, but you should be able to modify this sample code with your code.
In this example, there is a simple "HelloWorld" method that is called when the form is initialized as well as whenever you press the button. Let me know if this gets you close to what you're after.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
HelloWorldTest();
}
private void button1_Click(object sender, EventArgs e)
{
HelloWorldTest();
}
private void HelloWorldTest()
{
MessageBox.Show("Hello World!");
}
}
}
For what it's worth, I didn't use your code because I don't have the Leap libraries installed and if there was a typo, I wouldn't be much help to resolve it. Nonetheless, hopefully it gets you going down the right path.

Open NotePad in mys form on top

Can you hel me please to open an application (notepad) on top my form ?
private void button1_Click(object sender, EventArgs e)
{
Process N = new Process.Start(#"notepad.exe");
N.ShowDialog(this);
}
I try this but is not correctly.
So, how can i open notepad.exe or word.exe on top my form as ShowDialog please ?
Cordialy
Use SetForegroundWindow function.
Declare using System.Runtime.InteropServices; and use
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void SetNotepadAsForegroundApp()
{
Process N = new Process.Start(#"notepad.exe");
SetForegroundWindow(N.MainWindowHandle);
}

Capture keypress in Unity when Unity doesn't have the input focus

I need Unity to capture ALL key presses, even if Unity doesn't have the focus.
I've tried to use:
Input.KeyPress()
But this seems to only work if Unity has the focus of the user's input. I need it to work when it doesn't have the focus, such as when I'm looking at / working with another Windows program.
PS: I already turned on the "Run in Background" option in player preferences.
This is totally possible! Although, there is no way to do it by using only the tools in-built into Unity3D. You will have to use native libraries to do that.
The example below hooks the hook chain with a hook type of WH_KEYBOARD, which corresponds to a message-level keyboard hook. You can read more on SetWindowsHookEx and different types [ here][1].
You can check the parameters that are being received on hooking such a message type (WH_KEYBOARD) [here][2]
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class KBHooks : MonoBehaviour
{
[DllImport("user32")]
protected static extern IntPtr SetWindowsHookEx(
HookType code, HookProc func, IntPtr hInstance, int threadID);
[DllImport("user32")]
protected static extern int UnhookWindowsHookEx(
IntPtr hhook);
[DllImport("user32")]
protected static extern int CallNextHookEx(
IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
// Hook types. To hook the keyboard we only need WH_KEYBOARD
protected enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
protected IntPtr m_hhook = IntPtr.Zero;
protected HookType m_hookType = HookType.WH_KEYBOARD;
protected delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
//We install the hook and hold on to the hook handle.
//The handle will be need to unhook.
protected bool Install(HookProc cbFunc)
{
if (m_hhook == IntPtr.Zero)
m_hhook = SetWindowsHookEx(
m_hookType,
cbFunc,
IntPtr.Zero,
(int)AppDomain.GetCurrentThreadId());
if (m_hhook == IntPtr.Zero)
return false;
return true;
}
protected void Uninstall()
{
if (m_hhook != IntPtr.Zero)
{
UnhookWindowsHookEx(m_hhook);
m_hhook = IntPtr.Zero;
}
}
protected int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0)
return CallNextHookEx(m_hhook, code, wParam, lParam);
Debug.Log(
"hook code =" + code.ToString() +
" lparam=" + lParam.ToString() +
" wparam=" + wParam.ToString());
// Yield to the next hook in the chain
return CallNextHookEx(m_hhook, code, wParam, lParam);
}
// Use this for initialization
void Start()
{
Debug.Log("install hook");
Install(CoreHookProc);
}
void OnDisable()
{
Debug.Log("Uninstall hook");
Uninstall();
}
}
This example comes from [this blog][3].
Such a way of hooking will only work on Windows systems. If you need to make a separate hook on OS X or Linux you would need to do it in a native way in that operating system.
I cannot post more than 1 link because I lack reputation on SO. I hope one of the mods will edit my post accordingly.
[1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx
[2]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644984(v=vs.85).aspx
[3]: http://phardera.blogspot.com.es/2010/12/windows-hooks-in-unity3d.html
I've assembled a Unity package based on the #boris-makogonyuk answer with some usability improvements.
The package is available on GitHub (MIT license): https://github.com/Elringus/UnityRawInput
You can use it as follows:
Include package namespace.
using UnityRawInput;
Initialize the input service to start processing native input messages.
RawKeyInput.Start();
Optinally, you can specify whether input messages should be handled when the application is not in focus (disabled by default).
var workInBackground = true;
RawKeyInput.Start(workInBackground);
Add listeners for the input events.
RawKeyInput.OnKeyUp += HandleKeyUp;
RawKeyInput.OnKeyDown += HandleKeyDown;
private void HandleKeyUp (RawKey key) { ... }
private void HandleKeyDown (RawKey key) { ... }
You can also check whether specific key is currently pressed.
if (RawKeyInput.IsKeyDown(key)) { ... }
You can stop the service at any time.
RawKeyInput.Stop();
Don't forget to remove listeners when you no longer need them.
private void OnDisable ()
{
RawKeyInput.OnKeyUp -= HandleKeyUp;
RawKeyInput.OnKeyDown -= HandleKeyDown;
}

Silverlight 5 + AutoCompleteBox = Bug

Just installed SL5 and the toolkit, that were released few days ago.
The bug happens when you set the Text property of the AutoCompleteBox to string.Empty. It causes the AutoCompleteBox to be in a buggy state. To reproduce the bug:
add an AutoCompleteBox and a Button to the main page. Register to the TextChanged and Click events. This is the code-behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
auto.Text = string.Empty;
}
private void auto_TextChanged(object sender, RoutedEventArgs e)
{
// Put a break point here.
}
}
In runtime:
1) type "aa" into the autobox.
2) click the button.
3) type "q". ( TextChanged is still invoked).
4) erase the "q" - TextChanged is not invoked.
5) type "q" again - TextChanged is not invoked.
6) and so on, until you pick a new letter. And then it's starts over.
I found a workaround for this strange behavior. You need a control derived from AutoCompleteBox and overrride OnApplyTemplate method to find inner TextBox of AutoCompleteBox.
When inner TextBox TextChanged event fires you need to fire TextChanged event of AutoCompleteBox control manually.
public class CustomAutoComplete : AutoCompleteBox
{
TextBox mytext;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
mytext = GetTemplateChild("Text") as TextBox;
mytext.TextChanged += new System.Windows.Controls.TextChangedEventHandler(mytext_TextChanged);
}
void mytext_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
this.Text = mytext.Text;
OnTextChanged(new RoutedEventArgs());
}
}

DragDrop event not raised

This is kind of a stupid question... I'm trying to drag and drop a picturebox onto a panel. I followed some exemples, but it doesn't work. The DragDrop event of the panel is never raised. I searched thi site for a solution andfound two topics over a year old, but their solutions did not work. I created a new project, with only this code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
panel1.DragDrop +=new DragEventHandler(panel1_DragDrop);
panel1.DragOver +=new DragEventHandler(panel1_DragOver);
}
private void panel1_DragOver(object sender, DragEventArgs e)
{
Console.WriteLine("DragOver");
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("DragDrop");
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse");
pictureBox1.DoDragDrop(pictureBox1.Text, DragDropEffects.All);
}
}
I also set the AllowDrop of the panel and the form to true. DragOver and MouseDown are raised. Also, when I drag the picturebox, the cursor become a barred circled, like it was an operation that wasn't allowed. Is there a way that the cursor becomes the image in the picture box? I don't want the picturebox to move, only to add an item to the panel.
The problem is easy to solve.
You must just set in DragEnter appropriate Effect:
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
After that DragDrop event is fired correctly.
Richard, the problem is that drag and drop is not as simple operation as you have coded here. Here you haven't started the drag movement which should start with code and you can read more about it here... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=VS.90).aspx
If you just want to move the PictureBox... dragging picturebox inside winform on runtime
And finally Drag and Drop between Instances of the same Windows Forms Application
Hope this helps.