Q: http://community.csdn.net/Expert/topic/3179/3179109.xml?temp=.7848627
作者想要一个ocx的解决方案,我没有给出来。bcb在这个方面一直是诟病多多,呵呵。不过他的那个控件是open source的。原作者使用vc+mfc来开发的,不过接口就是api+消息了。这样的使用vcl来封装这个控件变得不是十分困难了。
CodeSense 是一个源码着色的编辑控件。
这是原文件的下载地址。
使用之前你需要用implib -a cmcs21.lib cmcs21.dll 来导出import library。
以下我做的封装:
#include <vcl.h>
#define _T //added by cker. A dirty hack.
#include "CodeSense.h"
#include <assert.h>
#define ASSERT assert
class TCodeSenseCtrl : public TWinControl
{
protected:
virtual void __fastcall CreateParams(Controls::TCreateParams &Params)
{
CMRegisterControl();
TWinControl::CreateParams(Params);
CreateSubClass(Params, CODESENSEWNDCLASS);
}
public:
__fastcall virtual TCodeSenseCtrl(TComponent* AOwner) : TWinControl(AOwner)
{
}
__fastcall virtual ~TCodeSenseCtrl()
{
CMUnregisterControl();
}
public:
CME_CODE SetLanguage( LPCTSTR pszName )
{
return CM_SetLanguage( Handle, pszName );
}
CME_CODE GetLanguage( LPTSTR pszName )
{
return CM_GetLanguage( Handle, pszName );
}
CME_CODE EnableColorSyntax( BOOL bEnable = TRUE )
{
return CM_EnableColorSyntax( Handle, bEnable );
}
BOOL IsColorSyntaxEnabled()
{
return CM_IsColorSyntaxEnabled( Handle );
}
CME_CODE OpenFile( LPCTSTR pszFileName )
{
return CM_OpenFile( Handle, pszFileName );
}
CME_CODE InsertFile( LPCTSTR pszFileName, const CM_POSITION *pPos = NULL )
{
return CM_InsertFile( Handle, pszFileName, pPos );
}
CME_CODE InsertText( LPCTSTR pszText, const CM_POSITION *pPos = NULL )
{
return CM_InsertText( Handle, pszText, pPos );
}
CME_CODE SetText( LPCTSTR pszText )
{
return CM_SetText( Handle, pszText );
}
CME_CODE SetColors( const CM_COLORS *pColors )
{
return CM_SetColors( Handle, pColors );
}
CME_CODE GetColors( CM_COLORS *pColors )
{
return CM_GetColors( Handle, pColors );
}
CME_CODE EnableWhitespaceDisplay( BOOL bEnable = TRUE )
{
return CM_EnableWhitespaceDisplay( Handle, bEnable );
}
BOOL IsWhitespaceDisplayEnabled()
{
return CM_IsWhitespaceDisplayEnabled( Handle );
}
CME_CODE EnableTabExpand( BOOL bEnable = TRUE )
{
return CM_EnableTabExpand( Handle, bEnable );
}
BOOL IsTabExpandEnabled()
{
return CM_IsTabExpandEnabled( Handle );
}
CME_CODE EnableSmoothScrolling( BOOL bEnable = TRUE )
{
return CM_EnableSmoothScrolling( Handle, bEnable );
}
BOOL IsSmoothScrollingEnabled()
{
return CM_IsSmoothScrollingEnabled( Handle );
}
CME_CODE SetTabSize( int nTabSize )
{
return CM_SetTabSize( Handle, nTabSize );
}
int GetTabSize()
{
return CM_GetTabSize( Handle );
}
CME_CODE SetReadOnly( BOOL bReadOnly = TRUE )
{
return CM_SetReadOnly( Handle, bReadOnly );
}
BOOL IsReadOnly()
{
return CM_IsReadOnly( Handle );
}
CME_CODE EnableLineToolTips( BOOL bEnable = TRUE )
{
return CM_EnableLineToolTips( Handle, bEnable );
}
BOOL IsLineToolTipsEnabled()
{
return CM_IsLineToolTipsEnabled( Handle );
}
CME_CODE EnableLeftMargin( BOOL bEnable = TRUE )
{
return CM_EnableLeftMargin( Handle, bEnable );
}
BOOL IsLeftMarginEnabled()
{
return CM_IsLeftMarginEnabled( Handle );
}
CME_CODE SaveFile( LPCTSTR pszFileName, BOOL bClearUndo = TRUE )
{
return CM_SaveFile( Handle, pszFileName, bClearUndo );
}
CME_CODE ReplaceText( LPCTSTR pszText, const CM_RANGE *pRange = NULL )
{
return CM_ReplaceText( Handle, pszText, pRange );
}
int GetTextLength( const CM_RANGE *pRange = NULL, BOOL bLogical = FALSE )
{
return CM_GetTextLength( Handle, pRange, bLogical );
}
CME_CODE GetText( LPTSTR pszBuff, const CM_RANGE *pRange = NULL )
{
return CM_GetText( Handle, pszBuff, pRange );
}
// CME_CODE GetText( CString &strText, const CM_RANGE *pRange = NULL )
// { ASSERT( ::IsWindow( Handle ) );
// CME_CODE nCode = CM_GetText( Handle, strText.GetBufferSetLength( GetTextLength( pRange ) + 1 ), pRange );
// strText.ReleaseBuffer();
// return nCode; }
CME_CODE GetLine( int nLine, LPTSTR pszBuff )
{
return CM_GetLine( Handle, nLine, pszBuff );
}
// CME_CODE GetLine( int nLine, CString &strLine )
// { ASSERT( ::IsWindow( Handle ) );
// CME_CODE nCode = CM_GetLine( Handle, nLine, strLine.GetBufferSetLength( GetLineLength( nLine ) + 1 ) );
// strLine.ReleaseBuffer();
// return nCode; }
int GetLineCount()
{
return CM_GetLineCount( Handle );
}
int GetLineLength( int nLine, BOOL bLogical = FALSE )
{
return CM_GetLineLength( Handle, nLine, bLogical );
}
CME_CODE GetWord( LPTSTR pszBuff, CM_POSITION *pPos )
{
return CM_GetWord( Handle, pszBuff, pPos );
}
// CME_CODE GetWord( CString &strWord, CM_POSITION *pPos )
// { ASSERT( ::IsWindow( Handle ) );
// CME_CODE nCode = CM_GetWord( Handle, strWord.GetBufferSetLength( GetWordLength( pPos ) + 1 ), pPos );
// strWord.ReleaseBuffer();
// return nCode; }
int GetWordLength( CM_POSITION *pPos, BOOL bLogical = FALSE )
{
return CM_GetWordLength( Handle, pPos, bLogical );
}
CME_CODE AddText( LPCTSTR pszText )
{
return CM_AddText( Handle, pszText );
}
CME_CODE DeleteLine( int nLine )
{
return CM_DeleteLine( Handle, nLine );
}
CME_CODE InsertLine( int nLine, LPCTSTR pszText )
{
return CM_InsertLine( Handle, nLine, pszText );
}
CME_CODE GetSel( CM_RANGE *pRange, BOOL bNormalized = TRUE )
{
return CM_GetSel( Handle, pRange, bNormalized );
}
CME_CODE SetSel( const CM_RANGE *pRange, BOOL bMakeVisible = TRUE )
{
return CM_SetSel( Handle, pRange, bMakeVisible );
}
CME_CODE DeleteSel()
{
return CM_DeleteSel( Handle );
}
CME_CODE ReplaceSel( LPCTSTR pszText )
{
return CM_ReplaceSel( Handle, pszText );
}
CME_CODE ExecuteCmd( WORD wCmd, DWORD dwCmdData = 0 )
{
return CM_ExecuteCmd( Handle, wCmd, dwCmdData );
}
CME_CODE SetSplitterPos( BOOL bHorz, int nPos )
{
return CM_SetSplitterPos( Handle, bHorz, nPos );
}
int GetSplitterPos( BOOL bHorz )
{
return CM_GetSplitterPos( Handle, bHorz );
}
CME_CODE SetAutoIndentMode( int nMode )
{
return CM_SetAutoIndentMode( Handle, nMode );
}
int GetAutoIndentMode()
{
return CM_GetAutoIndentMode( Handle );
}
BOOL CanCopy()
{
return CM_CanCopy( Handle );
}
BOOL CanCut()
{
return CM_CanCut( Handle );
}
BOOL CanPaste()
{
return CM_CanPaste( Handle );
}
CME_CODE Copy()
{
return CM_Copy( Handle );
}
CME_CODE Cut()
{
return CM_Cut( Handle );
}
CME_CODE Paste()
{
return CM_Paste( Handle );
}
BOOL CanUndo()
{
return CM_CanUndo( Handle );
}
BOOL CanRedo()
{
return CM_CanRedo( Handle );
}
CME_CODE Redo()
{
return CM_Redo( Handle );
}
CME_CODE Undo()
{
return CM_Undo( Handle );
}
CME_CODE ClearUndoBuffer()
{
return CM_ClearUndoBuffer( Handle );
}
CME_CODE SetUndoLimit( int nLimit )
{
return CM_SetUndoLimit( Handle, nLimit );
}
int GetUndoLimit()
{
return CM_GetUndoLimit( Handle );
}
BOOL IsModified()
{
return CM_IsModified( Handle );
}
CME_CODE SetModified( BOOL bModified = TRUE )
{
return CM_SetModified( Handle, bModified );
}
CME_CODE EnableOvertype( BOOL bEnable = TRUE )
{
return CM_EnableOvertype( Handle, bEnable );
}
BOOL IsOvertypeEnabled()
{
return CM_IsOvertypeEnabled( Handle );
}
CME_CODE EnableCaseSensitive( BOOL bEnable = TRUE )
{
return CM_EnableCaseSensitive( Handle, bEnable );
}
BOOL IsCaseSensitiveEnabled()
{
return CM_IsCaseSensitiveEnabled( Handle );
}
CME_CODE EnablePreserveCase( BOOL bEnable = TRUE )
{
return CM_EnablePreserveCase( Handle, bEnable );
}
BOOL IsPreserveCaseEnabled()
{
return CM_IsPreserveCaseEnabled( Handle );
}
CME_CODE EnableWholeWord( BOOL bEnable = TRUE )
{
return CM_EnableWholeWord( Handle, bEnable );
}
BOOL IsWholeWordEnabled()
{
return CM_IsWholeWordEnabled( Handle );
}
CME_CODE EnableRegExp( BOOL bEnable = TRUE )
{
return CM_EnableRegExp( Handle, bEnable );
}
BOOL IsRegExpEnabled()
{
return CM_IsRegExpEnabled( Handle );
}
CME_CODE SetTopIndex( int nView, int nLine )
{
return CM_SetTopIndex( Handle, nView, nLine );
}
int GetTopIndex( int nView )
{
return CM_GetTopIndex( Handle, nView );
}
int GetVisibleLineCount( int nView, BOOL bFullyVisible = TRUE )
{
return CM_GetVisibleLineCount( Handle, nView, bFullyVisible );
}
CME_CODE EnableCRLF( BOOL bEnable = TRUE )
{
return CM_EnableCRLF( Handle, bEnable );
}
BOOL IsCRLFEnabled()
{
return CM_IsCRLFEnabled( Handle );
}
CME_CODE SetFontOwnership( BOOL bEnable )
{
return CM_SetFontOwnership( Handle, bEnable );
}
BOOL GetFontOwnership()
{
return CM_GetFontOwnership( Handle );
}
int GetCurrentView()
{
return CM_GetCurrentView( Handle );
}
CME_CODE SetCurrentView(int nView)
{
return CM_SetCurrentView( Handle, nView );
}
int GetViewCount()
{
return CM_GetViewCount( Handle );
}
CME_CODE ShowScrollBar( BOOL bHorz, BOOL bShow = TRUE )
{
return CM_ShowScrollBar( Handle, bHorz, bShow );
}
BOOL HasScrollBar( BOOL bHorz )
{
return CM_HasScrollBar( Handle, bHorz );
}
CME_CODE GetSelFromPoint( int xClient, int yClient, CM_POSITION *pPos )
{
return CM_GetSelFromPoint( Handle, xClient, yClient, pPos );
}
CME_CODE SelectLine( int nLine, BOOL bMakeVisible = TRUE )
{
return CM_SelectLine( Handle, nLine, bMakeVisible );
}
int HitTest( int xClient, int yClient )
{
return CM_HitTest( Handle, xClient, yClient );
}
CME_CODE EnableDragDrop( BOOL bEnable = TRUE )
{
return CM_EnableDragDrop( Handle, bEnable );
}
BOOL IsDragDropEnabled()
{
return CM_IsDragDropEnabled( Handle );
}
CME_CODE EnableSplitter( BOOL bHorz, BOOL bEnable = TRUE )
{
return CM_EnableSplitter( Handle, bHorz, bEnable );
}
BOOL IsSplitterEnabled( BOOL bHorz )
{
return CM_IsSplitterEnabled( Handle, bHorz );
}
CME_CODE EnableColumnSel( BOOL bEnable = TRUE )
{
return CM_EnableColumnSel( Handle, bEnable );
}
BOOL IsColumnSelEnabled()
{
return CM_IsColumnSelEnabled( Handle );
}
CME_CODE EnableGlobalProps( BOOL bEnable = TRUE )
{
return CM_EnableGlobalProps( Handle, bEnable );
}
BOOL IsGlobalPropsEnabled()
{
return CM_IsGlobalPropsEnabled( Handle );
}
BOOL IsRecordingMacro()
{
return CM_IsRecordingMacro( Handle );
}
BOOL IsPlayingMacro()
{
return CM_IsPlayingMacro( Handle );
}
CME_CODE SetDlgParent( HWND *hWnd )
{
return CM_SetDlgParent( Handle, hWnd );
}
CME_CODE EnableSelBounds( BOOL bEnable = TRUE )
{
return CM_EnableSelBounds( Handle, bEnable );
}
BOOL IsSelBoundsEnabled()
{
return CM_IsSelBoundsEnabled( Handle );
}
CME_CODE SetFontStyles( CM_FONTSTYLES *pFontStyles )
{
return CM_SetFontStyles( Handle, pFontStyles );
}
CME_CODE GetFontStyles( CM_FONTSTYLES *pFontStyles )
{
return CM_GetFontStyles( Handle, pFontStyles );
}
CME_CODE SetItemData( int nLine, LPARAM lParam )
{
return CM_SetItemData( Handle, nLine, lParam );
}
LPARAM GetItemData( int nLine )
{
return CM_GetItemData( Handle, nLine );
}
CME_CODE SetLineStyle( int nLine, DWORD dwStyle )
{
return CM_SetLineStyle( Handle, nLine, dwStyle );
}
DWORD GetLineStyle( int nLine )
{
return CM_GetLineStyle( Handle, nLine );
}
CME_CODE SetBookmark( int nLine, BOOL bEnable = TRUE )
{
return CM_SetBookmark( Handle, nLine, bEnable );
}
BOOL GetBookmark( int nLine )
{
return CM_GetBookmark( Handle, nLine );
}
CME_CODE SetAllBookmarks( int nCount, LPDWORD pdwLines )
{
return CM_SetAllBookmarks( Handle, nCount, pdwLines );
}
int GetAllBookmarks( LPDWORD pdwLines )
{
return CM_GetAllBookmarks( Handle, pdwLines );
}
CME_CODE SetLineNumbering( CM_LINENUMBERING *pNumbering )
{
return CM_SetLineNumbering( Handle, pNumbering );
}
CME_CODE GetLineNumbering( CM_LINENUMBERING *pNumbering )
{
return CM_GetLineNumbering( Handle, pNumbering );
}
CME_CODE PosFromChar( CM_POSITION *pPos, LPRECT pRect )
{
return CM_PosFromChar( Handle, pPos, pRect );
}
CME_CODE EnableHideSel( BOOL bEnable = TRUE )
{
return CM_EnableHideSel( Handle, bEnable );
}
BOOL IsHideSelEnabled()
{
return CM_IsHideSelEnabled( Handle );
}
CME_CODE SetHighlightedLine( int nLine )
{
return CM_SetHighlightedLine( Handle, nLine );
}
int GetHighlightedLine()
{
return CM_GetHighlightedLine( Handle );
}
CME_CODE EnableNormalizeCase( BOOL bEnable = TRUE )
{
return CM_EnableNormalizeCase( Handle, bEnable );
}
BOOL IsNormalizeCaseEnabled()
{
return CM_IsNormalizeCaseEnabled( Handle );
}
CME_CODE SetDivider( int nLine, BOOL bEnable = TRUE )
{
return CM_SetDivider( Handle, nLine, bEnable );
}
BOOL GetDivider( int nLine )
{
return CM_GetDivider( Handle, nLine );
}
CME_CODE EnableOvertypeCaret( BOOL bEnable = TRUE )
{
return CM_EnableOvertypeCaret( Handle, bEnable );
}
BOOL IsOvertypeCaretEnabled()
{
return CM_IsOvertypeCaretEnabled( Handle );
}
CME_CODE SetFindText( LPCTSTR pszText )
{
return CM_SetFindText( Handle, pszText );
}
CME_CODE GetFindText( LPTSTR pszText )
{
return CM_GetFindText( Handle, pszText );
}
// CME_CODE GetFindText( CString &strText )
// {
// CME_CODE nCode = CM_GetFindText( Handle, strText.GetBufferSetLength( CM_MAX_FINDREPL_TEXT ) );
// strText.ReleaseBuffer();
// return nCode;
// }
CME_CODE SetReplaceText( LPCTSTR pszText )
{
return CM_SetReplaceText( Handle, pszText );
}
CME_CODE GetReplaceText( LPTSTR pszText )
{
return CM_GetReplaceText( Handle, pszText );
}
// CME_CODE GetReplaceText( CString &strText )
// {
// CME_CODE nCode = CM_GetReplaceText( Handle, strText.GetBufferSetLength( CM_MAX_FINDREPL_TEXT ) );
// strText.ReleaseBuffer();
// return nCode;
// }
CME_CODE SetImageList( HIMAGELIST hImageList )
{
return CM_SetImageList( Handle, hImageList );
}
HIMAGELIST GetImageList()
{
return CM_GetImageList( Handle );
}
CME_CODE SetMarginImages( int nLine, BYTE byImages )
{
return CM_SetMarginImages( Handle, nLine, byImages );
}
BYTE GetMarginImages( int nLine )
{
return CM_GetMarginImages( Handle, nLine );
}
CME_CODE AboutBox()
{
return CM_AboutBox( Handle );
}
CME_CODE Print( HDC hDC, DWORD dwFlags )
{
return CM_Print( Handle, hDC, dwFlags );
}
CME_CODE SetCaretPos( int nLine, int nCol )
{
return CM_SetCaretPos( Handle, nLine, nCol );
}
int ViewColToBufferCol( int nLine, int nViewCol )
{
return CM_ViewColToBufferCol( Handle, nLine, nViewCol );
}
int BufferColToViewCol( int nLine, int nBufferCol )
{
return CM_BufferColToViewCol( Handle, nLine, nBufferCol );
}
CME_CODE SetBorderStyle( DWORD dwStyle )
{
return CM_SetBorderStyle( Handle, dwStyle );
}
DWORD GetBorderStyle()
{
return CM_GetBorderStyle( Handle );
}
DWORD GetCurrentToken()
{
return CM_GetCurrentToken( Handle );
}
void UpdateControlPositions()
{
CM_UpdateControlPositions( Handle );
}
};
注释掉的是关于Cstring的部分,没什么大意义。懒得再写AnsiString的封装了。
下面是调用示例:
//----------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "CodeSense.h"
#include "TCodeSenseCtrl.h"
//----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------------------------------------------------
TCodeSenseCtrl* s;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete s;
}
//----------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
s = new TCodeSenseCtrl(this);
Panel1->DoubleBuffered = true;
s->Parent = Panel1;
s->Align = alClient;
s->Visible = true;
s->SetLanguage(CMLANG_CPP);
s->EnableColorSyntax();
s->ShowScrollBar(false);
s->ShowScrollBar(true);
s->EnableSplitter(false,true);
s->EnableSplitter(true,true);
::SendMessage(Handle,WM_SETFONT,(int)Panel1->Font->Handle,0);
}
//----------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog->Execute())
{
s->OpenFile(OpenDialog->FileName.c_str());
}
}
//----------------------------------------------------------------------