2016.1.1 VS中宏的使用技巧点滴

Dim selection As TextSelection = DTE.ActiveDocument.Selection‘定义 TextSelection 对象

selection.StartOfLine()‘移动到当前光标所在行的起始位置

Dim keyword = selection.Text.Trim()‘取得整行文本

‘获取当前选择首点所在行

Dim endLine As Integer

endLine = selection.TopPoint.Line

selection.NewLine()‘插入一行空白行

selection.LineUp()‘将光标移回到新空白行

selection.GotoLine(startLine) ‘将光标位置跳至某行的起始位置

selection.MoveToLineAndOffset(startLine,10 )‘将光标跳至某行的起始位置+10个字符处

selection.LineDown(True, 18) ‘将选择位置向下移18行,true表示将选择范围进行扩展,选择的起始位置不变,终止位置下移18行

selection.CharRight(True,5) ’与上面类似,将选择范围向右扩展5个字符

‘VB语法输入多段文本只能用" & Chr(13) & _ 连接的方法

‘在选择范围中进行文本替换

DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection

DTE.Find.FindWhat = "dvxxxdv"

DTE.Find.ReplaceWith = keyword

DTE.Find.Execute()

DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument ‘定义搜索范围,注意:默认是vsFindTargetCurrentDocumentSelection,只在当前选中文本中查找,vsFindTargetCurrentDocument表示全文查找

DTE.Find.Backwards = True ‘向上还是向下,默认居然是向上

DTE.Find.Action = vsFindAction.vsFindActionFind

DTE.Find.FindWhat = "{"

DTE.Find.Execute()

MsgBox(selection.Text.Trim())‘弹出提示框

实例:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports EnvDTE90a

Imports EnvDTE100

Imports System.Diagnostics

Public Module SkModule

‘sk 2014.8.27 快速输入string.Format

Sub SQLFormat()

ActiveDocument.Selection.Text = "string.Format(@""select "",AisLogical.curUser);"

ActiveDocument.Selection.EndOfLine()

ActiveDocument.Selection.CharLeft(False, 23)

End Sub

‘sk 2016.1.8将DataGridview控件加上两个事件替代selectionchanged事件

Sub DvAddFunc()

Dim selection As TextSelection = DTE.ActiveDocument.Selection

‘选择整行,注意此时光标焦点落在下一行开始处

selection.SelectLine()

‘取得整行文本

Dim keyword = selection.Text.Trim()

selection.Text = ""

‘插入一行空白行

selection.NewLine()

‘将光标移回到新空白行

selection.LineUp()

Dim startLine As Integer

‘获取selection末端行号

startLine = selection.BottomPoint.Line

‘VB语法输入多段文本只能用" & Chr(13) & _连接的方法

‘注意在输入过程中VS环境可能会自动将某些看似语法错误的表达式替换成貌似正确的表达式,可能跟插入文本原文不一致

selection.Text = "//将下面两行剪切到该Dv控件所在窗体的Designer.cs文件中的InitializeComponent()函数末尾" & Chr(13) & _

"this.dvxxxdv.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dvxxxdv_CellClick);" & Chr(13) & _

"this.dvxxxdv.KeyUp += new System.Windows.Forms.KeyEventHandler(this.dvxxxdv_KeyUp);" & Chr(13) & _

"public void zMySelectionChanged_dv()" & Chr(13) & _

"{" & Chr(13) & _

"if (dvxxxdv.SelectedRows.Count == 0) return;" & Chr(13) & _

"}" & Chr(13) & _

"private void dvxxxdv_CellClick(object sender, DataGridViewCellEventArgs e)" & Chr(13) & _

"{" & Chr(13) & _

"if (e.RowIndex < 0) return;//如果不是单击列表头" & Chr(13) & _

"zMySelectionChanged_dv();" & Chr(13) & _

"}" & Chr(13) & _

"" & Chr(13) & _

"private void dvxxxdv_KeyUp(object sender, KeyEventArgs e)" & Chr(13) & _

"{" & Chr(13) & _

"if (e.KeyData == Keys.Down || e.KeyData == Keys.Up)" & Chr(13) & _

"zMySelectionChanged_dv();            " & Chr(13) & _

"}" & Chr(13)

‘将选择起始位置重新设回文本插入前的位置

selection.GotoLine(startLine)

selection.LineDown(True, 18)

‘在选择范围中进行文本替换

DTE.Find.Action = vsFindAction.vsFindActionReplaceAll

DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocumentSelection

DTE.Find.FindWhat = "dvxxxdv"

DTE.Find.ReplaceWith = keyword

DTE.Find.Execute()

End Sub

‘sk 2016.1.9将选择的范围加上#region结构

Sub AddRegion()

Dim selection As TextSelection = DTE.ActiveDocument.Selection ‘定义TextSelection 对象

Dim startLine As Integer

startLine = selection.TopPoint.Line

Dim endLine As Integer

endLine = selection.BottomPoint.Line

selection.GotoLine(startLine) ‘将光标位置跳至某行的起始位置

selection.Text = "#region" + Chr(13)

selection.GotoLine(endLine + 1) ‘将光标位置跳至某行的起始位置

selection.EndOfLine()

selection.NewLine()

selection.Text = "#endregion"

End Sub

‘sk 2016.1.10将当前光标所在模块加上#region结构

Sub RegionOnBlock()

Dim selection As TextSelection = DTE.ActiveDocument.Selection ‘定义TextSelection 对象

Dim startline As Integer

startline = -1

Dim endline As Integer

endline = -1

Dim currentline As Integer = selection.ActivePoint.Line

selection.EndOfDocument() ‘将选择行移到最末一行

Dim documentlines As Integer = selection.ActivePoint.Line ‘整篇文档总行数

selection.GotoLine(currentline) ‘此时光标会停留在行首,如果加上,true参数整行将选择,选择的起始位置保持在行首和末尾

Dim kuohaoleft = 0 ‘从初始位置到当前位置共出现过几次{

selection.SelectLine()

selection.CharLeft(True)

Dim tx As String = selection.Text.Trim()

If (tx = "{") Then

kuohaoleft = 1

startline = selection.ActivePoint.Line

ElseIf (tx.IndexOf("{") >= 0) Then

MsgBox("光标所在行的‘{‘没有单独占一行,请重新选择行")

Return

Else

Do While kuohaoleft <= 0

‘以下行为上移整行选择,选择的起始位置在行首和末尾,如果没有第句,选择末端会停留在下一行,也可用selection.GotoLine(xxline,true)实现

selection.LineUp()

selection.SelectLine()

selection.CharLeft(True)

tx = selection.Text.Trim()

If (tx = "{") Then

kuohaoleft += 1

ElseIf (tx = "}") Then

kuohaoleft -= 1

End If

If (selection.ActivePoint.Line = 1) Then Exit Do

Loop

If (kuohaoleft = 1) Then

startline = selection.ActivePoint.Line

Else

MsgBox("从当前位置以上没有出现单独一行的‘{‘")

Return

End If

End If

‘此时光标停留在初始位置往上真实的第一个{处

kuohaoleft = 1 ‘其实运行到这里肯定kuohaoleft=1

Do While (kuohaoleft > 0)

selection.LineDown()

selection.SelectLine()

selection.CharLeft(True)

tx = selection.Text.Trim()

If (tx = "{") Then

kuohaoleft += 1

ElseIf (tx = "}") Then

kuohaoleft -= 1

End If

If (selection.ActivePoint.Line = documentlines) Then Exit Do

Loop

If (kuohaoleft = 0) Then

endline = selection.ActivePoint.Line

Else

MsgBox("从当前位置以下没有出现单独一行的‘}‘")

Return

End If

If (endline > startline And startline > 0) Then

selection.GotoLine(startline)

selection.EndOfLine()

selection.Text = Chr(13) + "#region"

selection.GotoLine(endline)

selection.EndOfLine()

selection.Text = Chr(13) + "#endregion"

‘将光标放置到起始{行的末尾

selection.GotoLine(startline)

selection.EndOfLine()

End If

End Sub

End Module

时间: 2024-08-04 23:07:34

2016.1.1 VS中宏的使用技巧点滴的相关文章

C++中宏的定义与用法(现已被内联函数所代替)

在noip中,宏还是被经常采用,所以这里讲一下,C++中宏的定义与用法 第一种用法——配合条件编译:#define DEBUG 定义一个叫DEBUG的标识符.它应该与#ifdef或#ifndef配合使用.举例如下: #define DEBUG #ifdef DEBUG void print(int v) { cout << v << endl;} #else void print(int) {} #endif 如果符号DEBUG存在,那么编译器会编译上面的.能输出数值的print,

C语言中宏定义(#define)时do{}while(0)的价值(转)

C语言中宏定义(#define)时do{}while(0)的价值 最近在新公司的代码中发现到处用到do{...}while(0),google了一下,发现Stack Overflow上早有很多讨论,总结了一下讨论,加上自己的理解,do{...}while(0)的价值主要体现在: 1. 增加代码的适应性 下面的宏定义没有使用do{...}while(0) #define FOO(x) foo(x); bar(x); 这样宏定义,单独调用不会出现问题,例如: FOO(100) 宏扩展后变成: 1 f

ATL中宏定义offsetofclass的使用

近日学习ATL,通过对宏定义offsetofclass的解惑过程,顺便分析下虚函数表,以及通过虚函数表调用函数的问题. 1 解开ATL中宏定义offsetofclass的疑惑 #define _ATL_PACKING  8 #define offsetofclass(base, derived) ((unsigned long)(static_cast <base*>((derived*)_ATL_PACKING))-_ATL_PACKING) 分析如下:(base 基类 , derived

ATL中宏定义offsetofclass的分析

近日学习ATL,通过对宏定义offsetofclass的解惑过程.顺便分析下虚函数表,以及通过虚函数表调用函数的问题. 1 解开ATL中宏定义offsetofclass的疑惑 #define _ATL_PACKING  8 #define offsetofclass(base, derived) ((unsigned long)(static_cast <base*>((derived*)_ATL_PACKING))-_ATL_PACKING) 分析例如以下:(base 基类 , derive

C语言中宏的使用(#,##,do…while(0)宏)

1.预定义宏的使用__FILE__,__FUNCTION__,__LINE__. #include <stdio.h> void fun(void) { char v1; short v2; int v3; long v4; printf("v1: %x\n",&v1); printf("v2: %x\n",&v2); printf("v3: %x\n",&v3); printf("v4: %x\n&

百度2016实习 前端试题中的编程题2:Excel地址的相互转换 [2015南桥杯试题]

百度2016实习 前端试题中的编程题2:Excel地址的相互转换  Excel是最常用的办公软件.每个单元格都有唯一的地址表示.比如:第12行第4列表示为:"D12",第5行第255列表示为"IU5". 事实上,Excel提供了两种地址表示方法,还有一种表示法叫做RC格式地址. 第12行第4列表示为:"R12C4",第5行第255列表示为"R5C255". 要求:编写程序,对换两种不同的表示方法表示行列,即 如果输入是常规地址

C语言中一些很酷的技巧(cool tricks)

来自Quora,觉得不错,就实践了一下. 1.  #if 0 ...... #endif 块中的内容不会被编译,因为注释不允许嵌套,我们可以把暂时不用的代码块放在 这里面. 2. 数组初始化的时候可以指定索引,而且可以给特定范围的数组赋值. 比如  int array[] = { [0 ... 9] = 1, [10 ... 20] = 2, [30 ... 40] = 3}; 等价于    int array[] = { 1, 1, 1, 1, ..... 2, 2, 2 ...... 3,

第一周--Centos7中的 at 使用技巧及其历史命令管理和用法

**Centos 7 中的at 使用技巧及其历史命令管理和用法** 我将在本文讲述如下内容: 关于Linux的发行版及不同发行版间的联系和区别 实现晚上20:30自动关机,并提示用户 总结历史命令管理以及用法 总结Linux帮助用法 * ----------------------------- 大片如下 ------------------------------------------------------------------------------------------ 第一章:关

OAF开发中一些LOV相关技巧 (转)

原文地址:OAF开发中一些LOV相关技巧 在OAF开发中,LOV的使用频率是很高的,它由两部分构成一是页面上的LOV输入框(如OAMESSageLovInputBean),二是弹出的LOV模式窗口(OAListOfValueBean).用户选择LOV的按钮就会弹出LOV窗口,用户在LOV窗口查询并选择了值,可以返回到页面上的LOV输入框.在这里就不赘述如何创建LOV,只说一些平时会碰到的应用: 1.控制LOV的查询结果 2.LOV相关事件 3.动态LOV 4.LOV Choice一,控制LOV的