所以这就是问题所在。我正在使用keyDown事件将bool设置为true - 只要它为true,mouseMove事件就会在picturebox上绘制。基本上是一个绘图程序。问题是,只要我按住绘图按钮,bool似乎打开和关闭,我得到一个奇怪的条纹结果(见示例图片中的蓝线)。 bool在代码中没有关闭,所以我不知道是什么导致它(好吧,它被关闭,但我禁用了那部分代码,我仍然遇到问题)。
有什么想法吗?
编辑:这是一些代码。我省略了keyUp事件,因为我可以禁用它,但仍然会遇到同样的问题。更有趣的是,如果我放开键盘按钮,我会得到一条实线,但只要我按下按钮,它就可以正常工作大约一秒钟,然后开始做条纹的东西(类似于你持有的时候)文本中的按钮 - 它稍等一下,然后开始重复该字符)。
private void Main_KeyDown(object sender, KeyEventArgs e)
{
string clicked = e.KeyData.ToString().ToLower(); // I'm comparing the clicked key to make sure the right button is clicked (there's a controls changing system)
cursorLocation = pictureBox1.PointToClient(System.Windows.Forms.Cursor.Position);
if (clicked == ctkey && MapLoaded)
{
readyForDraw();
pen = new Pen(Color.DodgerBlue, penSize);
}
else if (clicked == tkey && MapLoaded)
{
readyForDraw();
pen = new Pen(Color.Red, penSize);
}
else if (clicked == neutralkey && MapLoaded)
{
readyForDraw();
pen = new Pen(neutralColour, penSize);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawing && MapLoaded && addPlayerMode == false)
{
g.DrawLine(pen, cursorLocation, e.Location);
pictureBox1.Invalidate();
pictureBox1.Image = mainBitmap;
cursorLocation = pictureBox1.PointToClient(System.Windows.Forms.Cursor.Position);
}
}
private void readyForDraw()
{
g = Graphics.FromImage(mainBitmap);
isDrawing = true;
saveBackup();
}
示例图片。使用MouseDown事件绘制红色 - 工作正常。蓝色是keyDown事件,坏了。
答案 0 :(得分:0)
使用WPF中的Keyboard.GetKeyStates,如下所示:
public static bool IsDown(Key key) {
return Keyboard.GetKeyStates(key) & KeyStates.Down != 0;
}
即使你的应用程序是WinForms而不是WPF,它应该没有问题。 (参考项目中的PresentationCore.dll)