星期日, 十一月 11, 2007

MFC中常用类,宏,函数介绍

常用类

CRect:用来表示矩形的类,拥有四个成员变量:top left bottom right。分别表是左上角和右下角的坐标。可以通过以下的方法构造:
CRect( int l, int t, int r, int b ); 指明四个坐标
CRect( const RECT& srcRect ); 由RECT结构构造
CRect( LPCRECT lpSrcRect ); 由RECT结构构造
CRect( POINT point, SIZE size ); 有左上角坐标和尺寸构造
CRect( POINT topLeft, POINT bottomRight ); 有两点坐标构造。下面介绍几个成员函数:
int Width( ) const; 得到宽度
int Height( ) const; 得到高度
CSize Size( ) const; 得到尺寸
CPoint& TopLeft( ); 得到左上角坐标
CPoint& BottomRight( ); 得到右下角坐标
CPoint CenterPoint( ) const; 得当中心坐标
此外矩形可以和点(CPoint)相加进行位移,和另一个矩形相加得到“并”操作后的矩形。
CPoint:用来表示一个点的坐标,有两个成员变量:x y。 可以和另一个点相加。
CString:用来表示可变长度的字符串。使用CString可不指明内存大小,CString会根据需要自行分配。 下面介绍几个成员函数:
GetLength 得到字符串长度
GetAt 得到指定位置处的字符
operator + 相当于strcat
void Format( LPCTSTR lpszFormat, ... ); 相当于sprintf
Find 查找指定字符,字符串
Compare 比较
CompareNoCase 不区分大小写比较
MakeUpper 改为小写
MakeLower 改为大写
CStringArray:用来表示可变长度的字符串数组。数组中每一个元素为CString对象的实例。下面介绍几个成员函数:
Add 增加CString
RemoveAt 删除指定位置CString对象
RemoveAll 删除数组中所有CString对象
GetAt 得到指定位置的CString对象
SetAt 修改指定位置的CString对象
InsertAt 在某一位置插入CString对象

常用宏

RGB
TRACE
ASSERT
VERIFY

常用函数

CWindApp* AfxGetApp();
HINSTANCE AfxGetInstanceHandle( );
HINSTANCE AfxGetResourceHandle( );
int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );用于弹出一个消息框。

VC快捷键

F1: 帮助

Ctrl+O :Open
Ctrl+P :Print
Ctrl+N :New
Ctrl+Shift+F2 :清除所有书签
F2 :上一个书签
Shift+F2 :上一个书签
Alt+F2 :编辑书签
Ctrl+F2 :添加/删除一个书签
F12 :Goto definition
Shift+F12 :Goto reference
Ctrl+'Num+' :Displays the next symbol definition or reference
Ctrl+'Num-' :Displays the previous symbol definition or reference
Ctrl+J/K :寻找上一个/下一个预编译条件
Ctrl+Shift+J/K :寻找上一个/下一个预编译条件并将这一块选定
Ctrl+End :文档尾
Ctrl+Shift+End :选定从当前位置到文档尾
Ctrl+Home :文档头
Ctrl+Shift+Home :选定从当前位置到文档头
Ctrl+B/Alt+F9 :编辑断点
Alt+F3/Ctrl+F :查找
F3 :查找下一个
Shift+F3 :查找上一个
Ctrl+]/Ctrl+E :寻找下一半括弧
Ctrl+Shift+] :寻找下一半括弧并选定括弧之间的部分(包括括弧)
Ctrl+Shift+E :寻找下一半括弧并选定括弧之间的部分(包括括弧)
F4 :寻找下一个错误/警告位置
Shift+F4 :寻找上一个错误/警告位置
Shift+Home :选定从当前位置到行首
Shift+End :选定从当前位置到行尾
Ctrl+L :剪切当前行
Ctrl+Shift+L :删除当前行
Alt+Shift+T :交换当前行和上一行
Ctrl+Alt+T :Brings up the completion list box
Shift+PageDown :选定从当前位置到下一页当前位置
Shift+PageUp :选定从当前位置到上一页当前位置
Ctrl+Shift+Space:显示函数参数的Tooltip
Ctrl+Z/Alt+Backspace :Undo
Ctrl+Shift+Z/Ctrl+Y :Redo
F8 :当前位置变成选定区域的头/尾(再移动光标或者点鼠标就会选定)
Ctrl+Shift+F8 :当前位置变成矩形选定区域的头/尾(再移动光标或者点鼠标就会选定)
Alt+F8 :自动格式重排
Ctrl+G :Goto
Ctlr+X/Shift+Del:Cut
Ctrl+C/Ctrl+Ins :Copy
Ctrl+V/Shift+Ins:Paste
Ctrl+U :将选定区域转换成小写
Ctrl+Shift+U :将选定区域转换成大写
Ctrl+F8 :当前行变成选定区域的头/尾(再移动上下光标或者点鼠标就会选定多行)
Ctrl+Shift+L :删除从当前位置到行尾
Ctrl+Shift+8 :将所有Tab变成`或者还原
Ctrl+T :显示变量类型
Ctrl+↑ :向上滚屏
Ctrl+↓ :向下滚屏
Ctrl+Del :删除当前单词的后半截(以光标为分割)
Ctrl+Backspace :删除当前单词的前半截(以光标为分割)
Ctrl+← :移到前一个单词
Ctrl+→ :移到后一个单词
Ctrl+Shift+← :选定当前位置到前一个单词
Ctrl+Shift+→ :选定当前位置到后一个单词
Ctrl+Shift+T :将本单词和上一个单词互换


Alt+0 :Workspace Window
Alt+2 :Output Window
Alt+3 :Watch Window
Alt+4 :Variables Window
Alt+5 :Registers Window
Alt+6 :Memory Window
Alt+7 :CallStack Window
Alt+8 :Disassembly Window
Ctrl+W :ClassWizard
Alt+Enter :属性

Alt+F7 :Project Settings

F7 :Build
Ctrl+F7 :Compile
Ctrl+F5 :Run
Ctrl+Break :Stops the build
F5 :Go
Ctrl+F10 :Run to cursor
F11 :step into
Alt+F10 :Apply codes changes
Ctrl+F9 :Enable/Disable a breakpoint
Alt+F11 :将 Memory Window 切换到下一种显示模式
Alt+Shift+F11 :将 Memory Window 切换到上一种显示模式
Ctrl+Shift+F9 :去掉所有断点
Ctrl+Shift+F5 :Restarts the program
Ctrl+Shift+F10 :将当前行设为下一条指令执行的行
Alt+Num* :滚动到当前指令
Shift+F11 :跳出当前函数
F9 :断点
F10 :step over
Shift+F5 :停止 Debugging
Ctrl+F11 :Switches between the source view and the disassembly view for this instruction
Alt+F12 :Queries on the selected object or current context

Alt+F6 :Toggles the docking feature for the window on/off
Shift+Esc :隐藏窗口



Ctrl+Shift+G :?
Ctrl+* :打开string table
Ctrl+Space :?
Ctrl+F3 :向下查找下一个
Ctrl+Shift+F3 :查找上一个
Ctrl+D :查找
Ctrl+I :向下查找下一个
Ctrl+Shift+I :查找上一个
F6 :?Activates the next pane
Shift+F6 :?Activates the previous pane
Ctrl+M :?Detects duplicate mnemonics in the resource
Alt +O : 头文件与cpp文件的交互显示

VC快捷键

F1: 帮助

Ctrl+O :Open
Ctrl+P :Print
Ctrl+N :New
Ctrl+Shift+F2 :清除所有书签
F2 :上一个书签
Shift+F2 :上一个书签
Alt+F2 :编辑书签
Ctrl+F2 :添加/删除一个书签
F12 :Goto definition
Shift+F12 :Goto reference
Ctrl+'Num+' :Displays the next symbol definition or reference
Ctrl+'Num-' :Displays the previous symbol definition or reference
Ctrl+J/K :寻找上一个/下一个预编译条件
Ctrl+Shift+J/K :寻找上一个/下一个预编译条件并将这一块选定
Ctrl+End :文档尾
Ctrl+Shift+End :选定从当前位置到文档尾
Ctrl+Home :文档头
Ctrl+Shift+Home :选定从当前位置到文档头
Ctrl+B/Alt+F9 :编辑断点
Alt+F3/Ctrl+F :查找
F3 :查找下一个
Shift+F3 :查找上一个
Ctrl+]/Ctrl+E :寻找下一半括弧
Ctrl+Shift+] :寻找下一半括弧并选定括弧之间的部分(包括括弧)
Ctrl+Shift+E :寻找下一半括弧并选定括弧之间的部分(包括括弧)
F4 :寻找下一个错误/警告位置
Shift+F4 :寻找上一个错误/警告位置
Shift+Home :选定从当前位置到行首
Shift+End :选定从当前位置到行尾
Ctrl+L :剪切当前行
Ctrl+Shift+L :删除当前行
Alt+Shift+T :交换当前行和上一行
Ctrl+Alt+T :Brings up the completion list box
Shift+PageDown :选定从当前位置到下一页当前位置
Shift+PageUp :选定从当前位置到上一页当前位置
Ctrl+Shift+Space:显示函数参数的Tooltip
Ctrl+Z/Alt+Backspace :Undo
Ctrl+Shift+Z/Ctrl+Y :Redo
F8 :当前位置变成选定区域的头/尾(再移动光标或者点鼠标就会选定)
Ctrl+Shift+F8 :当前位置变成矩形选定区域的头/尾(再移动光标或者点鼠标就会选定)
Alt+F8 :自动格式重排
Ctrl+G :Goto
Ctlr+X/Shift+Del:Cut
Ctrl+C/Ctrl+Ins :Copy
Ctrl+V/Shift+Ins:Paste
Ctrl+U :将选定区域转换成小写
Ctrl+Shift+U :将选定区域转换成大写
Ctrl+F8 :当前行变成选定区域的头/尾(再移动上下光标或者点鼠标就会选定多行)
Ctrl+Shift+L :删除从当前位置到行尾
Ctrl+Shift+8 :将所有Tab变成`或者还原
Ctrl+T :显示变量类型
Ctrl+↑ :向上滚屏
Ctrl+↓ :向下滚屏
Ctrl+Del :删除当前单词的后半截(以光标为分割)
Ctrl+Backspace :删除当前单词的前半截(以光标为分割)
Ctrl+← :移到前一个单词
Ctrl+→ :移到后一个单词
Ctrl+Shift+← :选定当前位置到前一个单词
Ctrl+Shift+→ :选定当前位置到后一个单词
Ctrl+Shift+T :将本单词和上一个单词互换


Alt+0 :Workspace Window
Alt+2 :Output Window
Alt+3 :Watch Window
Alt+4 :Variables Window
Alt+5 :Registers Window
Alt+6 :Memory Window
Alt+7 :CallStack Window
Alt+8 :Disassembly Window
Ctrl+W :ClassWizard
Alt+Enter :属性

Alt+F7 :Project Settings

F7 :Build
Ctrl+F7 :Compile
Ctrl+F5 :Run
Ctrl+Break :Stops the build
F5 :Go
Ctrl+F10 :Run to cursor
F11 :step into
Alt+F10 :Apply codes changes
Ctrl+F9 :Enable/Disable a breakpoint
Alt+F11 :将 Memory Window 切换到下一种显示模式
Alt+Shift+F11 :将 Memory Window 切换到上一种显示模式
Ctrl+Shift+F9 :去掉所有断点
Ctrl+Shift+F5 :Restarts the program
Ctrl+Shift+F10 :将当前行设为下一条指令执行的行
Alt+Num* :滚动到当前指令
Shift+F11 :跳出当前函数
F9 :断点
F10 :step over
Shift+F5 :停止 Debugging
Ctrl+F11 :Switches between the source view and the disassembly view for this instruction
Alt+F12 :Queries on the selected object or current context

Alt+F6 :Toggles the docking feature for the window on/off
Shift+Esc :隐藏窗口



Ctrl+Shift+G :?
Ctrl+* :打开string table
Ctrl+Space :?
Ctrl+F3 :向下查找下一个
Ctrl+Shift+F3 :查找上一个
Ctrl+D :查找
Ctrl+I :向下查找下一个
Ctrl+Shift+I :查找上一个
F6 :?Activates the next pane
Shift+F6 :?Activates the previous pane
Ctrl+M :?Detects duplicate mnemonics in the resource
Alt +O : 头文件与cpp文件的交互显示

C++ builder快捷键大全

Clipboard control (default)
Ctrl+Ins EditCopy
Shift+Del EditCut
Shift+Ins EditPaste

Ctrl+C EditCopy
Ctrl+V EditPaste
Ctrl+X EditCut



--------------------------------------------------------------------------------


Debugger (default, classic, Brief, Epsilon, and Visual Studio)
Breakpoint view

Ctrl+V View Source
Ctrl+S Edit Source
Ctrl+E Edit Breakpoint
Enter Edit Breakpoint
Ctrl+D Delete Breakpoint
Del Delete Breakpoint
Ctrl+A Add Breakpoint
Ins Add Breakpoint
Ctrl+N Enable Breakpoint

Call stack view

Ctrl+V View Source
Ctrl+S Edit Source
Space View Source (Epsilon only)
Ctrl+Enter Edit Source (Epsilon only)

Message view

Ctrl+V View Source
Space View Source
Ctrl+S Edit Source
Ctrl+Enter Edit Source

Watch view

Ctrl+E Edit Watch
Enter Edit Watch
Ctrl+A Add Watch
Ins Add Watch
Ctrl+D Delete Watch
Del Delete Watch



--------------------------------------------------------------------------------


Editor (default)

Shortcut Action or command

F1 HelpTopic Search
Ctrl+F1 HelpTopic Search
F3 SearchSearch Again

Ctrl+E SearchIncremental Search
Ctrl+F SearchFind
Ctrl+I Inserts a tab character
Ctrl+j Templates pop-up menu
Ctrl+N Inserts a new line
Ctrl+P Causes next character to be interpreted as an ASCII sequence
Ctrl+R SearchReplace
Ctrl+S FileSave
Ctrl+T Deletes a word

Ctrl+Y Deletes a line
Ctrl+Z EditUndo
Ctrl+ Code Completion pop-up window

Ctrl+Shift+I Indents block
Ctrl+Shift+U Outdents block
Ctrl+Shift+Y Deletes to the end of a line
Ctrl+Shift+Z EditRedo
Ctrl+Shift+ Code Parameters pop-up window

Alt+[ Finds the matching delimiter (forward)
Alt+] Finds the matching delimiter (backward)

End Moves to the end of a line
Home Moves to the start of a line
Enter Inserts a carriage return

Ins Turns insert mode on/off
Del Deletes the character to the right of the cursor
Backspace Deletes the character to the left of the cursor
Tab Inserts a tab
Space Inserts a blank space
Left Arrow Moves the cursor left one column, accounting for the autoindent setting
Right Arrow Moves the cursor right one column, accounting for the autoindent setting
Up Arrow Moves up one line
Down Arrow Moves down one line

Page Up Moves up one page
Page Down Moves down one page

Ctrl+Left Arrow Moves one word left
Ctrl+Right Arrow Moves one word right
Ctrl+Tab Moves to the next code page (or file)
Ctrl+Shift+Tab Moves to the previous code page (or file)
Ctrl+Backspace Deletes the word to the right of the cursor
Ctrl+Home Moves to the top of a file
Ctrl+End Moves to the end of a file
Ctrl+Del Deletes a currently selected block

Ctrl+Space Inserts a blank space
Ctrl+PgDn Moves to the bottom of a screen
Ctrl+PgUp Moves to the top of a screen
Ctrl+Up Arrow Scrolls up one line
Ctrl+Down Arrow Scrolls down one line
Ctrl+Enter Opens file at cursor

Shift+Tab Moves the cursor to the left one tab position
Shift+Backspace Deletes the character to the left of the cursor
Shift+Left Arrow Selects the character to the left of the cursor

Shift+Right Arrow Selects the character to the right of the cursor
Shift+Up Arrow Moves the cursor up one line and selects from the left of the starting cursor position
Shift+Down Arrow Moves the cursor down one line and selects from the right of the starting cursor position
Shift+PgUp Moves the cursor up one screen and selects from the left of the starting cursor position
Shift+PgDn Moves the cursor down one line and selects from the right of the starting cursor position

Shift+End Selects from the cursor position to the end of the current line
Shift+Home Selects from the cursor position to the start of the current line
Shift+Space Inserts a blank space
Shift+Enter Inserts a new line with a carriage return

Ctrl+Shift+Left Arrow Selects the word to the left of the cursor
Ctrl+Shift+Right Arrow Selects the word to the right of the cursor
Ctrl+Shift+Home Selects from the cursor position to the start of the current file

Ctrl+Shift+End Selects from the cursor position to the end of the current file
Ctrl+Shift+PgDn Selects from the cursor position to the bottom of the screen
Ctrl+Shift+PgUp Selects from the cursor position to the top of the screen
Ctrl+Shift+Tab Moves to the previous page

Alt+Backspace EditUndo
Alt+Shift+Backspace EditRedo
Alt+Shift+Left Arrow Selects the column to the left of the cursor

Alt+Shift+Right Arrow Selects the column to the right of the cursor
Alt+Shift+Up Arrow Moves the cursor up one line and selects the column from the left of the starting cursor position
Alt+Shift+Down Arrow Moves the cursor down one line and selects the column from the left of the starting cursor position
Alt+Shift+Page Up Moves the cursor up one screen and selects the column from the left of the starting cursor position
Alt+Shift+Page Down Moves the cursor down one line and selects the column from the right of the starting cursor position

Alt+Shift+End Selects the column from the cursor position to the end of the current line
Alt+Shift+Home Selects the column from the cursor position to the start of the current line

Ctrl+Alt+Shift+Left Arrow Selects the column to the left of the cursor
Ctrl+Alt+Shift+Right Arrow Selects the column to the right of the cursor
Ctrl+Alt+Shift+Home Selects the column from the cursor position to the start of the current file
Ctrl+Alt+Shift+End Selects the column from the cursor position to the end of the current file

Ctrl+Alt+Shift+Page Up Selects the column from the cursor position to the bottom of the screen
Ctrl+Alt+Shift+Page Down Selects the column from the cursor position to the top of the screen



--------------------------------------------------------------------------------

Block commands (default and classic)
Shortcut Action or command

Ctrl+K+B Marks the beginning of a block
Ctrl+K+C Copies a selected block
Ctrl+K+H Hides/shows a selected block
Ctrl+K+I Indents a block by the amount specified in the Block Indent combo box on the General page of the Editor Options dialog box.
Ctrl+K+K Marks the end of a block
Ctrl+K+L Marks the current line as a block
Ctrl+K+N Changes a block to uppercase
Ctrl+K+O Changes a block to lowercase

Ctrl+K+P Prints selected block
Ctrl+K+R Reads a block from a file
Ctrl+K+T Marks a word as a block
Ctrl+K+U Outdents a block by the amount specified in the Block Indent combo box on the General page of the Editor Options dialog box.
Ctrl+K+V Moves a selected block
Ctrl+K+W Writes a selected block to a file
Ctrl+K+Y Deletes a selected block

Ctrl+O+C Marks a column block
Ctrl+O+I Marks an inclusive block

Ctrl+O+K Marks a non-inclusive block
Ctrl+O+L Marks a line as a block

Ctrl+Q+B Moves to the beginning of a block
Ctrl+Q+K Moves to the end of a block



--------------------------------------------------------------------------------


Bookmark operations (default, classic, and Visual Studio)
Shortcut Action

Ctrl+K+0 Sets bookmark 0
Ctrl+K+1 Sets bookmark 1
Ctrl+K+2 Sets bookmark 2
Ctrl+K+3 Sets bookmark 3
Ctrl+K+4 Sets bookmark 4
Ctrl+K+5 Sets bookmark 5
Ctrl+K+6 Sets bookmark 6
Ctrl+K+7 Sets bookmark 7
Ctrl+K+8 Sets bookmark 8
Ctrl+K+9 Sets bookmark 9

Ctrl+K+Ctrl+0 Sets bookmark 0
Ctrl+K+Ctrl+1 Sets bookmark 1
Ctrl+K+Ctrl+2 Sets bookmark 2

Ctrl+K+Ctrl+3 Sets bookmark 3
Ctrl+K+Ctrl+4 Sets bookmark 4
Ctrl+K+Ctrl+5 Sets bookmark 5
Ctrl+K+Ctrl+6 Sets bookmark 6
Ctrl+K+Ctrl+7 Sets bookmark 7
Ctrl+K+Ctrl+8 Sets bookmark 8
Ctrl+K+Ctrl+9 Sets bookmark 9

Ctrl+Q+0 Goes to bookmark 0
Ctrl+Q+1 Goes to bookmark 1
Ctrl+Q+2 Goes to bookmark 2
Ctrl+Q+3 Goes to bookmark 3
Ctrl+Q+4 Goes to bookmark 4
Ctrl+Q+5 Goes to bookmark 5

Ctrl+Q+6 Goes to bookmark 6
Ctrl+Q+7 Goes to bookmark 7
Ctrl+Q+8 Goes to bookmark 8
Ctrl+Q+9 Goes to bookmark 9

Ctrl+Q+Ctrl+0 Goes to bookmark 0
Ctrl+Q+Ctrl+1 Goes to bookmark 1
Ctrl+Q+Ctrl+2 Goes to bookmark 2
Ctrl+Q+Ctrl+3 Goes to bookmark 3
Ctrl+Q+Ctrl+4 Goes to bookmark 4
Ctrl+Q+Ctrl+5 Goes to bookmark 5
Ctrl+Q+Ctrl+6 Goes to bookmark 6
Ctrl+Q+Ctrl+7 Goes to bookmark 7

Ctrl+Q+Ctrl+8 Goes to bookmark 8
Ctrl+Q+Ctrl+9 Goes to bookmark 9



--------------------------------------------------------------------------------


Cursor movement (default and classic)
Shortcut Action

Ctrl+Q+B Moves to the beginning of a block
Ctrl+Q+C Moves to end of a file
Ctrl+Q+D Moves to the end of a line
Ctrl+Q+E Moves to the top of the window
Ctrl+Q+K Moves to the end of a block
Ctrl+Q+P Moves to previous position
Ctrl+Q+R Moves to the beginning of a file
Ctrl+Q+S Moves to the beginning of a line
Ctrl+Q+T Moves to the top of the window
Ctrl+Q+U Moves to the bottom of the window

Ctrl+Q+X Moves to the bottom of the window



--------------------------------------------------------------------------------


Miscellaneous commands (default and classic)
Shortcut Action or command

Ctrl+K+D Accesses the menu bar
Ctrl+K+E Changes a word to lowercase
Ctrl+K+F Changes a word to uppercase
Ctrl+K+S FileSave (default and classic only)

Ctrl+Q+A SearchReplace
Ctrl+Q+F SearchFind
Ctrl+Q+Y Deletes to the end of a line
Ctrl+Q+[ Finds the matching delimiter (forward)
Ctrl+Q+Ctrl+[ Finds the matching delimiter (forward)
Ctrl+Q+] Finds the matching delimiter (backward)

Ctrl+Q+Ctrl+] Finds the matching delimiter (backward)

Ctrl+O+A Open file at cursor
Ctrl+O+G SearchGo to line number
Ctrl+O+O Inserts compiler options and directives
Ctrl+O+U Toggles case



--------------------------------------------------------------------------------


system
Shortcut Action or command

F1 Displays context-sensitive Help
F4 RunGo to Cursor
F5 RunToggle Breakpoint
Ctrl+F6 Open Source/Header file
F7 RunTrace Into
F8 RunStep Over
F9 RunRun
F11 ViewObject Inspector
F12 ViewToggle Form/Unit


Alt+0 ViewWindow List
Alt+F2 ViewDebug WindowsCPU
Alt+F7 Displays previous error in Message view
Alt+F8 Displays next error in Message view
Alt+F10 Displays a context menu
Alt+F11 FileInclude Unit Hdr
Alt+F12 Displays the Code editor

Ctrl+F1 HelpTopic Search
Ctrl+F2 RunProgram Reset
Ctrl+F3 ViewDebug WindowsCall Stack
Ctrl+F4 Closes current file
Ctrl+F5 Add Watch at Cursor

Ctrl+F6 Displays header file in Code editor

Ctrl+F7 Evaluate/Modify
Ctrl+F9 ProjectMake project

Ctrl+F11 FileOpen Project

Ctrl+F12 ViewUnits


Ctrl+D Descends item (replaces Inspector window)
Ctrl+E ViewClassExplorer
Ctrl+N Opens a new Inspector window
Ctrl+S Incremental search
Ctrl+T Displays the Type Cast dialog

Shift+F7 RunTrace To Next Source Line
Shift+F11 ProjectAdd To Project
Shift+F12 ViewForms

Ctrl+Shift+P Plays back a key macro
Ctrl+Shift+R Records a key macro

Ctrl+K+D Accesses the menu bar
Ctrl+K+S FileSave

用OLE操作Excel

本文档部分资料来自互联网,大部分是ccrun(老妖)在Excel中通过录制宏-->察看宏代码-->转为CB代码而来.本文档不断更新中.欢迎大家关注.

要在应用程序中控制Excel的运行,首先必须在编制自动化客户程序时包含Comobj.hpp
#include "Comobj.hpp"

C++ Builder把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。
设置对象属性:void OlePropertySet(属性名,参数……);
获得对象属性:Variant OlePropertyGet(属性名,参数……);
调用对象方法:1) Variant OleFunction(函数名,参数……);
2) void OleProcedure(过程名,参数……);

在程序中可以用宏定义来节省时间:

#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure

举例:
ExcelApp.OlePropertyGet("workbooks").OleFunction("Add");
可写为
ExcelApp.PG("workbooks").FN("Add");

C++ Builder中使用OLE控制Excel2000,必须掌握Excel2000的自动化对象及Microsoft Word Visual Basic帮助文件中的关于Excel的对象、方法和属性。对象是一个Excel元素,属性是对象的一个特性或操作的一个方面,方法是对象可以进行的动作。
首先定义以下几个变量:
Variant ExcelApp,Workbook1,Sheet1,Range1;

1、Excel中常用的对象是:Application,Workbooks,Worksheets等。

★创建应用对象★
Variant ExcelApp;
ExcelApp = Variant::CreateObject ("Excel.Application");
或者
ExcelApp = CreateOleObject ("Excel.Application");

★创建工作簿对象★
Variant WorkBook1;
WorkBook1 = ExcelApp.PG("ActiveWorkBook");

★创建工作表对象★
Variant Sheet1;
Sheet1 = WorkBook1.PG("ActiveSheet");

★创建区域对象★
Variant Range;
Range = Sheet1.PG("Range","A1:A10");
或者使用
Excel.Exec(PropertyGet("Range")<<"A1:C1").Exec(Procedure("Select"));

2、常用的属性操作:

★使Excel程序不可见★
ExcelApp.PS("Visible", (Variant)false);

★新建EXCEL文件★

◎ 新建系统模板的工作簿
ExcelApp.PG("workbooks").FN("Add") //默认工作簿
ExcelApp.PG("workbooks").FN("Add", 1) //单工作表
ExcelApp.PG("workbooks").FN("Add", 2) //图表
ExcelApp.PG("workbooks").FN("Add", 3) //宏表
ExcelApp.PG("workbooks").FN("Add", 4) //国际通用宏表
ExcelApp.PG("workbooks").FN("Add", 5) //与默认的相同
ExcelApp.PG("workbooks").FN("Add", 6) //工作簿且只有一个表
或者使用ExcelApp的Exec方法
Excel.Exec(PropertyGet("Workbooks")).Exec(Procedure("Add"));
◎ 新建自己创建的模板的工作簿
ExcelApp.PG("workbooks").FN("Add", "C:\\Temp\\result.xlt");

★打开工作簿★
ExcelApp.PG("workbooks").FN("open", "路径名.xls")

★保存工作簿★
WorkBook1.FN("Save"); //保存工作簿
WorkBook1.FN("SaveAs", "文件名");//工作簿保存为,路径注意用"\\"

★退出EXCEL★
ExcelApp.FN ("Quit");
ExcelApp = Unassigned;
或者
ExcelApp.Exec(Procedure("Quit"));

★操作工作表★

◎ 选择选择工作表中第一个工作表
Workbook1.PG("Sheets", 1).PR("Select");
Sheet1 = Workbook1.PG("ActiveSheet");

◎ 重命名工作表
Sheet1.PS("Name", "Sheet的新名字");

◎ 当前工作簿中的工作表总数
// 本文转自 C++Builder 研究 - http://www.ccrun.com/article.asp?i=529&d=0iezy5
int nSheetCount=Workbook1.PG("Sheets").PG("Count");

★操作行和列★

◎ 获取当前工作表中有多少行和多少列:
Sheet1.PG("UsedRange").PG("Columns").PG("Count"); //列数
Sheet1.PG("UsedRange").PG("Rows").PG("Count"); //行数

◎ 设置列宽
ExcelApp.PG("Columns", 1).PS("ColumnWidth", 22);
或者
Range = ExcelApp.PG("Cells", 1, 3);
Range.PS("ColumnWidth", 22);

◎ 设置行高
ExcelApp.PG("Rows", 2).PS("RowHeight", 25);
或者
Range = ExcelApp.PG("Cells", 2, 1);
Range.PS("RowHeight", 25);

◎ 在工作表最前面插入一行
Sheet1.PG("Rows", 1).PR("Insert");

◎ 删除一行
ExcelApp.PG("Rows", 2).PR("Delete"); //将第2行删除

// 本文作者:ccrun ,如转载请保证本文档的完整性,并注明出处。
// 欢迎光临 C++ Builder 研究 www.ccrun.com
// 摘自:http://www.ccrun.com/doc/go.asp?id=529

★操作单元格★

◎ 设置单元格字体
Sheet1.PG("Cells", 1, 1).PG("Font").PS("Name", "隶书"); //字体
Sheet1.PG("Cells", 2, 3).PG("Font").PS("size", 28); //大小

◎ 设置所选区域字体
Range.PG("Cells").PG("Font").PS("Size", 28);
Range.PG("Cells").PG("Font").PS("Color", RGB(0, 0, 255));
其中参数的设置:
Font Name : "隶书" //字体名称
Size : 12 //字体大小
Color : RGB(*,*,*) //颜色
Underline : true/false //下划线
Italic: true/false //斜体

◎ 设置单元格格式为小数百分比
Sheet1.PG("Cells", 1, 1).PS("NumberFormatLocal", "0.00%");

◎ 设定单元格的垂直对齐方式
Range = ExcelApp.PG("Cells", 3, 4);
// 1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐
Range.PS("VerticalAlignment", 2);

◎ 设定单元格的文本为自动换行
Range = ExcelApp.PG("Cells", 3, 4);
Range.PS("WrapText", true);

★单元格的合并★

◎ Range = Sheet1.PG("Range", "A1:A2"); //A1和A2单元格合并
String strRange = "A" + IntToStr(j) + ":" + "C" + IntToStr(j); //比如:A1:C5
Range1=Sheet1.PG("Range", strRange.c_str()); //可以用变量控制单元格合并
Range1.FN("Merge", false);

★读写单元格★

◎ 指定单元格赋值
String strValue = "abcdefg";
Sheet1.PG("Cells", 3, 6).PS("Value", strValue.c_str());
Sheet1.PG("Cells", j, 1).PS("Value", "总记录:" + String(j-6));
或者使用
Excel.Exec(PropertyGet("Cells")<<1<<3).Exec(PropertySet("Value")<<15);

◎ 所选区域单元格赋值
Range.PG("Cells").PS("Value", 10);

◎ 所选区域行赋值
Range.PG("Rows",1).PS("Value", 1234);

◎ 工作表列赋值
Sheet1.PG("Columns",1).PS("Value", 1234);

◎ 读取取值语句:
String strValue = Sheet1.PG("Cells", 3, 5).PG("Value");

★窗口属性★

◎ 显示属性
ExcelApp.PS("Windowstate", 3); //最大化显示
1---------xlNormal //正常显示
2---------xlMinimized //最小化显示
3---------xlMaximized //最大化显示

◎ 状态栏属性
ExcelApp.PS("StatusBar", "您好,请您稍等。正在查询!");
ExcelApp.PS("StatusBar", false); //还原成默认值

◎ 标题属性:
ExcelApp.PS("Caption", "查询系统");

3、操作图表

★添加图表

Variant Chart;
Chart = ExcelApp.Exec(PropertyGet("Charts")).Exec(Function("Add"));
ExcelApp.Exec(PropertySet("Visible") << true);
Chart.Exec(PropertySet("Type") << -4100);

★滚动图表

for(int nRotate=5; nRotate <= 180; nRotate += 5)
{
Chart.Exec(PropertySet("Rotation") << nRotate);
}
for (int nRotate = 175; nRotate >= 0; nRotate -= 5)
{
Chart.Exec(PropertySet("Rotation") << nRotate);
}

另外,为保证程序能正常运行,需要在程序中判断目标机器是否安装了Office;
try
{
ExcelApp = Variant::CreateObject ("Excel.Application");
}
catch(...)
{
ShowMessage("运行Excel出错,请确认安装了Office");
return;
}


#include "comobj.hpp"
//---------------------------------------------------------------------------
// 对指定Excel文件中的指定列进行排序
// strExcelFileName : excel文件名
// nCol : 指定的列号
// nSortStyle : 1:升序,2:降序
void SortExcelColumn(String strExcelFileName, int nCol, int nSortStyle)
{
Variant vExcelApp, vWorkbook, vRange;
vExcelApp = Variant::CreateObject("Excel.Application");
vExcelApp.OlePropertySet("Visible", false);
vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", strExcelFileName.c_str());
vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
vExcelApp.OlePropertyGet("Columns", nCol).OleProcedure("Select");
vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells", 1, nCol).OleProcedure("Select");
vRange = vExcelApp.OlePropertyGet("Selection");
vRange.Exec(Function("Sort")<<vExcelApp.OlePropertyGet("Selection")<<nSortStyle);
vWorkbook.OleProcedure("Save");
vWorkbook.OleProcedure("Close");
vExcelApp.OleFunction("Quit");
vWorkbook = Unassigned;
vExcelApp = Unassigned;
ShowMessage("ok");
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 对C:\123\123.xls文件中第一个Sheet的第四列进行升序排序
SortExcelColumn("C:\\123\\123.xls", 4, 1);
}
//---------------------------------------------------------------------------