C#gdi +绘制线条失败

时间:2018-11-06 02:26:56

标签: c# gdi+

因此,我正在尝试为游戏创建由win 10语音识别引擎控制的智能十字准线。 表格1用于打开和关闭语音识别。如果听到我说“慢”,它将在另一透明的叠加表格上画一条线

 public partial class Form1 : Form
    {
        formoverlay overlayform = new formoverlay();
        SpeechRecognitionEngine dasSpeechEngine = new SpeechRecognitionEngine(); 
        public Form1()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dasSpeechEngine.RecognizeAsync(RecognizeMode.Multiple);
            btnDisable.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)//load event
        {
            Choices commands = new Choices();
            commands.Add(new string[] { "slow", "medium", "fast","ultra","x"});
            GrammarBuilder gbuilder = new GrammarBuilder();
            gbuilder.Append(commands);
            Grammar thegrammar = new Grammar(gbuilder);

            dasSpeechEngine.LoadGrammarAsync(thegrammar);
            dasSpeechEngine.SetInputToDefaultAudioDevice();
            dasSpeechEngine.SpeechRecognized += dasSpeechEngine_SpeechRecognized;
            overlayform.timestart = DateTime.Now.Second;
            //MessageBox.Show(overlayform.timestart.ToString());   



            //for (si=1,si)
        }
        private void dasSpeechEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {

            switch (e.Result.Text)
            {
                case "slow":
                    if (ModifierKeys.HasFlag(Keys.Control))//prevent accidentally sight change
                    {
                        //clear current canvas
                        //draw all the lines that has true bool flag
                        //draw all the marks
                        overlayform.theclearform();

                        overlayform.paintHorline(15, slowAimSet.lineStartY,slowAimSet.lineEndY,1);
                        break;
                    }
                    break;
            }
        }
    }

在叠加层窗体上,代码(不包括部分类声明):

Graphics g;
Pen myPen = new Pen(Color.Green);
Pen smallPen = new Pen(Color.GreenYellow);
public formoverlay()
{
    InitializeComponent();
}
private void formoverlay_Load(object sender, EventArgs e)
{
    this.BackColor = Color.Wheat;
    this.TransparencyKey = Color.Wheat;
    this.TopMost = true;
    this.FormBorderStyle = FormBorderStyle.None;

    int initialStyle = GetWindowLong (this.Handle, -20);
    SetWindowLong(this.Handle, -20, initialStyle | 0x80000 | 0x20);
    //enable click through


    //get wows window size;
    GetWindowRect(handle, out rect);

    this.Size = new Size(rect.right - rect.left, rect.bottom - rect.top);
    this.Top = rect.top;
    this.Left = rect.left;
    smallPen.Width = 2.0f;



}

public void paintHorline(int theTgtSpd,int tStartY, int tEndY, int thisRngMarkerDir)
{

    int rngMarkerOffset = 10;
    //aimlineForSpeed[] thisAimLine = new aimlineForSpeed [4];
    //g.DrawLine(myPen, 500,500, 600, 600);
   g.DrawLine(myPen, 0,tStartY, 1920, tEndY);   
}

public void theclearform()//everytime user say, re-draw everything
{
    Invalidate();
}   

当我运行程序时,当我说“慢”时,出现错误enter image description here 表示系统参数异常:{“参数无效。”} 如果我在绘图调用中写入常量值,例如g.DrawRectangle(myPen,100,100,200,200);

显然,该程序调用了“ painthorline”函数,但是它无法正确绘制。 如果我用painthorline方法创建新的图形,则不会导致此错误,但是根本不会绘制任何内容。

我哪里出错了? 我是否需要创建一个单独的事件才能使drawline / drawrectangle调用正常工作?

0 个答案:

没有答案