C#WASD控件?

时间:2015-12-12 13:58:28

标签: c#

所以我有一个名为picbox1的图片框,里面有一个图片。如果按下W,我需要它向上移动,如果按下A则需要离开并且你明白了。最简单的方法是什么?你能解释一下代码吗?

这是我的代码(以防万一):

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "vfw32.lib")
#pragma comment( lib, "comctl32.lib" )
using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat edges;
    VideoCapture cap;
    try
    {
        cap.open(0); // open the default camera
    }
    catch (...)
    {
        cout << "error!!" << endl;
    }
    if (!cap.isOpened())  // check if we succeeded
        return -1;
    namedWindow("edges", 1);
    for (;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        edges = frame;
        GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
        imshow("edges", edges);
        if (waitKey(30) >= 0) break;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

在Program.cs代码中动态创建表单和图片框并不是一个好主意。您应该在另一个文件中声明Form1。放图片然后使用KeyPress事件:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    int offset = 10;
    if(e.KeyChar == 'a')
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X - offset, pictureBox1.Location.Y);
    }
    else if(e.KeyChar == 'w')
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - offset);
    }
    else if (e.KeyChar == 's')
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + offset);
    }
    else if (e.KeyChar == 'd')
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X + offset, pictureBox1.Location.Y);
    }
}