Esempio n. 1
0
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="Ctrl"></param>
        /// <param name="CancelDefault"></param>
        void _RightBtn_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
        {
            if (clickNumber == 0)
            {  
                //Point currentPos = GetPositionForShowing(this.Application.Selection);
                LineSelectAnswerForm answerForm = new LineSelectAnswerForm(lineQuestion) { StartPosition = FormStartPosition.CenterParent };
                answerForm.AnswerEvent += (str) => { answer = str; }; // 获得答案
                //answerForm.Location = currentPos;
                answerForm.ShowDialog();

                Word.Document document = this.Application.ActiveDocument;
                string originalTxt = document.Paragraphs[lineQuestion.currentParagraph].Range.Text.ToString();
                originalTxt = originalTxt.Substring(0, originalTxt.IndexOf("]", 5) + 1);
                string currentTxt = originalTxt + answer + "\n";
                answer = null;
                document.Paragraphs[lineQuestion.currentParagraph].Range.Text = currentTxt; // 替换原段落的内容

                clickNumber++;
            }
        }
Esempio n. 2
0
        void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
        {
            Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
            lineQuestion = new LineQuestion();
            lineQuestion.leftAnswerList = new List<LineLeftAnswer>();
            lineQuestion.rightAnswerList = new List<LineRightAnswer>();
            Word.Selection Sel = this.Application.Selection;
            object charUnit = Word.WdUnits.wdCharacter; //字符移动
            object lineUnit = Word.WdUnits.wdLine; //行移动
            object count = 5;  // 移动次数
            object extend = Word.WdMovementType.wdExtend; //extend对光标移动区域进行扩展选择
            Sel.MoveEnd(charUnit, count);
            Sel.HomeKey(lineUnit, extend);
            if (!string.IsNullOrWhiteSpace(Sel.Range.Text) && Sel.Range.Text.Trim().ToString().Contains("连线"))
            {
                Sel.HomeKey(lineUnit);
                Sel.EndKey(lineUnit, extend);
                string currentParagraphText = Sel.Range.Text; // 获取该行文本内容
                lineQuestion.currentParagraph = 0;
                for (int i = 1; i <= vstoDoc.Paragraphs.Count; i++)
                {
                    if (currentParagraphText.Trim().Equals(vstoDoc.Paragraphs[i].Range.Text.Trim().ToString()))
                    {
                        // 获取该行在文档中的段落数
                        lineQuestion.currentParagraph = i;
                        break;
                    }
                }
                if (lineQuestion.currentParagraph != 0)
                {
                    // 获取每一道连线题的连线行总数
                    lineQuestion.answerCount = validParagraphs(lineQuestion.currentParagraph);
                }
                if (lineQuestion.answerCount != 0)
                {

                    for (int i = 1; i <= lineQuestion.answerCount; i++)
                    {
                        // 获取连线行内容
                        string paragraphText = vstoDoc.Paragraphs[lineQuestion.currentParagraph + i].Range.Text.ToString();

                        // 操作连线行左侧
                        string leftAnswerStr = paragraphText.Substring(0, paragraphText.IndexOf(" "));
                        LineLeftAnswer lla = new LineLeftAnswer();
                        lla.leftIndex = leftAnswerStr.Substring(0, leftAnswerStr.IndexOf("."));
                        lla.leftAnswer = leftAnswerStr.Substring(leftAnswerStr.IndexOf(".") + 1);
                        lineQuestion.leftAnswerList.Add(lla);

                        // 操作连线行左侧
                        string rightAnswerStr = paragraphText.Substring(paragraphText.LastIndexOf(" ") + 1);
                        LineRightAnswer lra = new LineRightAnswer();
                        lra.rightIndex = rightAnswerStr.Substring(0, rightAnswerStr.IndexOf("."));
                        lra.rightAnswer = rightAnswerStr.Substring(rightAnswerStr.IndexOf(".") + 1);
                        lineQuestion.rightAnswerList.Add(lra);
                    }
                }

                if(lineQuestion != null)
                {
                    LineSelectAnswerForm answerForm = new LineSelectAnswerForm(lineQuestion) { StartPosition = FormStartPosition.CenterParent };
                    answerForm.AnswerEvent += (str) => { answer = str; }; // 获得答案
                    answerForm.ShowDialog();

                    Word.Document document = this.Application.ActiveDocument;
                    string originalTxt = document.Paragraphs[lineQuestion.currentParagraph].Range.Text.ToString();
                    originalTxt = originalTxt.Substring(0, originalTxt.IndexOf("]", 5) + 1);
                    string currentTxt = originalTxt + answer + "\n";
                    answer = null;
                    document.Paragraphs[lineQuestion.currentParagraph].Range.Text = currentTxt; // 替换原段落的内容
                }

            }
        }