No tak to je nesmysl, picturebox zavolá event ve formu a zpracovává ho také vlákno formu. Pokud, ale nutně chcete řešení v události MouseMove tak to také lze. Na http://stackoverflow.com/questions/11002... je jedno z řešení, ale pro začátečníka je trochu složitější. Třída KeyMessageFilter
public class KeyMessageFilter : IMessageFilter
{
private Dictionary<Keys, bool> m_keyTable = new Dictionary<Keys, bool>();
public Dictionary<Keys, bool> KeyTable
{
get { return m_keyTable; }
private set { m_keyTable = value; }
}
public bool IsKeyPressed()
{
return m_keyPressed;
}
public bool IsKeyPressed(Keys k)
{
bool pressed = false;
if (KeyTable.TryGetValue(k, out pressed))
{
return pressed;
}
return false;
}
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private bool m_keyPressed = false;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN)
{
KeyTable[(Keys)m.WParam] = true;
m_keyPressed = true;
}
if (m.Msg == WM_KEYUP)
{
KeyTable[(Keys)m.WParam] = false;
m_keyPressed = false;
}
return false;
}
}
Hlavní formulář:
public Form1()
{
InitializeComponent();
pictureBox1.MouseMove += OnMouseMove;
m_filter = new KeyMessageFilter();
Application.AddMessageFilter(m_filter);
}
private KeyMessageFilter m_filter;
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (!m_filter.IsKeyPressed()) return;
if (m_filter.KeyTable.Count == 0) return;
var k = (from i in m_filter.KeyTable
where (i.Value)
select i).First();
this.Text = k.Key.ToString();
}
|