CText类使用例程

//========================================================================
//TITLE:
//    CText类使用例程
//AUTHOR:
//    norains
//DATE:
//    Tuesday  17-April-2007
//Environment:
//        EVC4.0 + Standard SDK 4.2
//        EVC4.0 + Standard SDK 5.0
//========================================================================

        CText是为了方便在屏幕中输出文本而封装的类.该类将复杂的设置操作封装成简单的函数,便于代码书写的简便性.

        该类使用简单,示例如下:

           //设置显示范围
 txtInfo.SetPosition( & rcWnd);

    //设置字体颜色  
 txtInfo.SetTextColor(RGB( 255 , 0 , 0 ));

   
    //设置字体磅值,0为默认的字体大小   
 txtInfo.SetTextHeight( 0 );
   
    //设置显示的文本
 txtInfo.SetText(TEXT( " 测试 " ));
      
    //设置格式,该参数和DrawText()相同
 txtInfo.SetFormat(DT_RIGHT);

    //绘制文本
 txtInfo.Draw(hdc);
   
    默认的是透明显示,如果需要显示背景颜色,可以如下操作:
       
    //设置背景色
 txtInfo.SetBkColor(RGB( 0 , 0 , 233 ));

    //OPAQUE为绘制背景色
 txtInfo.SetBkMode(OPAQUE);
   
    然后再调用Draw进行绘制即可.


    
///
//  Text.h: interface for the CText class.
//
// Version: 
//     1.0
// Data:
//     2007.03.27   
//

#ifndef TEXT_H
#define  TEXT_H


// ------------------------------------------------------------------
class  CText  
{
public:
    
void GetPosition(RECT * prcOut);
    
void SetTextHeight(int iHeight);
    
void SetFormat(UINT uFormat);
    
void SetBkColor(COLORREF crColor);
    
void SetTextColor(COLORREF crColor);
    BOOL SetBkMode(
int iMode);
    
void Draw(HDC hdc);
    BOOL SetText(
const TCHAR *pszText);
    
void SetPosition(const RECT *prc);
    CText();
    
virtual ~CText();
protected:
    RECT m_rcWndPos;    
    TCHAR 
*m_pszText;
    ULONG m_ulSizeText;
    UINT m_uFormat;
    
int m_iTextHeight;
    
int m_iBkMode;
    COLORREF m_crTextColor;
    COLORREF m_crBkColor;

}
;

#endif   //  #ifndef TEXT_H


//
//  Text.cpp: implementation of the CText class.
//
//

#include 
" stdafx.h "
#include 
" Text.h "


// --------------------------------------------------------------------
// Macro define
#define  DEFAULT_BKMODE            TRANSPARENT
#define  DEFAULT_TEXT_COLOR        RGB(0,0,0)
#define  DEFAULT_BK_COLOR        RGB(255,255,255)
#define  DEFAULT_FORMAT            (DT_LEFT | DT_SINGLELINE)
#define  DEFAULT_TEXT_HEIGHT        0


// --------------------------------------------------------------------
//
//  Construction/Destruction
//

CText::CText()
{
    memset(
&m_rcWndPos,0,sizeof(m_rcWndPos));
    m_pszText 
= NULL;
    m_ulSizeText 
= 0;
    m_iBkMode 
= DEFAULT_BKMODE;
    m_crTextColor 
= DEFAULT_TEXT_COLOR;
    m_crBkColor 
= DEFAULT_BK_COLOR;
    m_uFormat 
= DEFAULT_FORMAT;
    m_iTextHeight 
= DEFAULT_TEXT_HEIGHT;
}


CText::
~ CText()
{
    
if(m_pszText != NULL)
    
{
        delete [] m_pszText;
        m_pszText 
= NULL;
    }

}


// --------------------------------------------------------------------
// Description:
//     Set the control position
//
// --------------------------------------------------------------------
void  CText::SetPosition( const  RECT  * prc)
{
    m_rcWndPos 
= *prc;
}



// --------------------------------------------------------------------
// Description:
//     Set the text. If you want to display the text ,you should call the Display()
//
// --------------------------------------------------------------------
BOOL CText::SetText( const  TCHAR  * pszText)
{
    ULONG ulLen 
= _tcslen(pszText);

    
if(m_pszText == NULL)
    
{        
        m_pszText 
= new TCHAR [ulLen + 1];
        
        
if(m_pszText == NULL)
        
{
            
return FALSE;
        }


        m_ulSizeText 
= ulLen + 1;
    }

    
else if(ulLen + 1 > m_ulSizeText)
    
{
        delete [] m_pszText;

        m_pszText 
= new TCHAR [ulLen + 1];
        
        
if(m_pszText == NULL)
        
{
            
return FALSE;
        }


        m_ulSizeText 
= ulLen + 1;
    }


    _tcscpy(m_pszText,pszText);

    
return TRUE;
}



// --------------------------------------------------------------------
// Description:
//     Display the text stored.
//
// --------------------------------------------------------------------
void  CText::Draw(HDC hdc)
{
    COLORREF crOldTextColor 
= ::SetTextColor(hdc,m_crTextColor);
    COLORREF crOldBkColor 
= ::SetBkColor(hdc,m_crBkColor);
    
int iOldMode = ::SetBkMode(hdc,m_iBkMode);

    LOGFONT lf 
= {0};
    HFONT hFontNew, hFontOld;
    lf.lfQuality 
= CLEARTYPE_QUALITY;
    lf.lfHeight 
= -1 * (m_iTextHeight * GetDeviceCaps(hdc,LOGPIXELSY) / 72); //pound
    hFontNew = CreateFontIndirect(&lf);
    hFontOld 
= (HFONT) SelectObject(hdc, hFontNew);

    DrawText(hdc,m_pszText,
-1,&m_rcWndPos,m_uFormat);

    SelectObject(hdc, hFontOld);
    DeleteObject(hFontNew);

    ::SetTextColor(hdc,crOldTextColor);
    ::SetBkColor(hdc,crOldBkColor);
    ::SetBkMode(hdc,iOldMode);
}



// --------------------------------------------------------------------
// Description:
//     Set the background mode.
//
// Parameters:
//     iMode: [in] The value is just like as follow:
//         OPAQUE      -- Background is filled with the current background color before the text, 
//                         hatched brush, or pen is drawn. 
//         TRANSPARENT -- Background remains untouched. 

// --------------------------------------------------------------------
BOOL CText::SetBkMode( int  iMode)
{
    
if(iMode == OPAQUE || iMode == TRANSPARENT)
    
{
        m_iBkMode 
= iMode;
        
return TRUE;
    }

    
else
    
{
        
return FALSE;
    }

}



// --------------------------------------------------------------------
// Description:
//     Set the text color
//
// --------------------------------------------------------------------
void  CText::SetTextColor(COLORREF crColor)
{
    m_crTextColor 
= crColor;
}



// --------------------------------------------------------------------
// Description:
//     Set the background color
//
// --------------------------------------------------------------------
void  CText::SetBkColor(COLORREF crColor)
{
    m_crBkColor 
= crColor;
}


// --------------------------------------------------------------------
// Description:
//     Set Format.
//
// Parameters:
//     The value you should see the uFormat of DrawText()
// --------------------------------------------------------------------
void  CText::SetFormat(UINT uFormat)
{
    m_uFormat 
= uFormat;
}



// --------------------------------------------------------------------
// Description:
//     Set the height of text as pound
//
// ---------------------------------------------------------------------
void  CText::SetTextHeight( int  iHeight)
{
    m_iTextHeight 
= iHeight;
}



// --------------------------------------------------------------------
// Description:
//     Get the position as rect
//
// ---------------------------------------------------------------------
void  CText::GetPosition(RECT  * prcOut)
{
    
*prcOut = m_rcWndPos;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
----------Database-------------- 1.DataTable帮助(DataTableHelper.cs) 2.Access数据库文件操作辅助(JetAccessUtil.cs) 3.常用的Access数据库Sql操作辅助库(OleDbHelper.cs) 4.根据各种不同数据库生成不同【分页语句的辅助】 PagerHelper(PageHelper.cs) 5.查询条件组合辅助(SearchCondition.cs) 6.查询信息实体(SearchInfo.cs) 7.型(例如:int,string,double)转换(SmartDataReader.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 1.声音播放辅助(AudioHelper.cs) 2.摄像头操作辅助,包括开启、关闭、抓图、设置等功能(Camera.cs) 3.提供用于操作【剪切板】的方法(ClipboardHelper.cs) 4.获取电脑信息(Computer.cs) 5.提供用户硬件唯一信息的辅助(FingerprintHelper.cs) 6.读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 7.提供访问键盘当前状态的属性(KeyboardHelper.cs) 8.全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 9.模拟鼠标点击(MouseHelper.cs) 10.全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) 11.MP3文件播放操作辅助(MP3Helper.cs) ----------Encrypt-------------- 1.基于Base64的加密编码(Base64Util.cs) 2.字符串的加密/解密(EncodeHelper.cs) 3.MD5各种长度加密字符、验证MD5等操作辅助(MD5Util.cs) 4.QQ的EncryptUtil(QQEncryptUtil.cs) 5.非对称加密验证辅助(RSASecurityHelper.cs) ----------File-------------- 1.用于获取或设置Web.config/*.exe.config中节点数据的辅助(AppConfig.cs) 2.CSV文件和DataTable对象转换辅助(CSVHelper.cs) 3.DatabaseInfo 的摘要说明。(DatabaseInfo.cs) 4.常用的目录操作辅助(DirectoryUtil.cs) 5.Excel操作辅助(无需VBA引用)(ExcelHelper.cs) 6.利用VBA对象,导出DataView到一个Excel文档中的Excel辅助(Export2Excel.cs) 7.关联文件(ExtensionAttachUtil.cs) 8.注册文件关联的辅助(FileAssociationsHelper.cs) 9.打开、保存文件对话框操作辅助(FileDialogHelper.cs) 10.常用的文件操作辅助FileUtil(FileUtil.cs) 11.INI文件操作辅助(INIFileUtil.cs) 12.独立存储操作辅助(IsolatedStorageHelper.cs) 13.序列号操作辅助(Serializer.cs) 14.获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 15.简单的Word操作对象(WordCombineUtil.cs) 16.这个提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) 17.XML操作(XmlHelper.cs) ----------Format-------------- 1.参数验证的通用验证程序。(ArgumentValidation.cs) 2.这个提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) 3.byte字节数组操作辅助(BytesTools.cs) 4.处理数据型转换,数制转换、编码转换相关的(ConvertHelper.cs) 5.CRC校验辅助(CRCUtils.cs) 6.枚举操作公共(EnumHelper.cs) 7.身份证操作辅助(IDCardHelper.cs) 8.检测字符编码的(IdentifyEncoding.cs) 9.RGB颜色操作辅助(MyColors.cs) 10.日期操作(MyDateTime.cs) 11.转换人民币大小金额辅助(RMBUtil.cs) 12.常用的字符串常量(StringConstants.cs) 13.简要说明TextHelper。(StringUtil.cs) 14.获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 15.各种输入格式验证辅助(ValidateUtil.cs) ----------Network-------------- 1.Cookie操作辅助(CookieManger.cs) 2.FTP操作辅助(FTPHelper.cs) 3.HTML操作(HttpHelper.cs) 4.网页抓取帮助(HttpWebRequestHelper.cs) 5.Net(NetworkUtil.cs) 6.IE代理设置辅助(ProxyHelper.cs) ----------Winform-------------- 1.跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) 2.CheckBoxList(CheckBoxListUtil.cs) 3.窗口管理(ChildWinManagement.cs) 4.由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 5.显示,隐藏或关闭动画形式。(FormAnimator.cs) 6.对窗体进行冻结、解冻操作辅助(FreezeWindowUtil.cs) 7.窗体全屏操作辅助(FullScreenHelper.cs) 8.GDI操作辅助(GDI.cs) 9.提供静态方法来读取这两个文件夹和文件的系统图标。(IconReaderHelper.cs) 10.图片对象比较、缩放、缩略图、水印、压缩、转换、编码等操作辅助(ImageHelper.cs) 11.输入法帮助,全角 转换为半角(ImeHelper.cs) 12.Winform提示框 的摘要说明。(MessageUtil.cs) 13.包含互操作方法调用的应用程序中使用。(NativeMethods.cs) 14.托盘图标辅助(NotifyIconHelper.cs) 15.打印机(POSPrinter.cs) 16.图片、光标、图标、位图等资源操作辅助(ResourceHelper.cs) 17.RTF字符格式辅助(RTFUtility.cs) 18.串口开发辅助(SerialPortUtil.cs) 19.设置文本属性提供一个ToolStripStatusLabel(SafeToolStripLabel.cs) 20.只运行一个实例及系统自动启动辅助(StartupHelper.cs) 21.Web页面预览效果图片抓取辅助(WebPageCapture.cs) 22.供Asp.Net直接调用的包装(WebPreview.cs) 23.计算机重启、关电源、注销、关闭显示器辅助(WindowsExitHelper.cs) ----------NONONONO-------------- 1.全局统一的缓存(Cache.cs) 2.常用显示日期时间、农历、生肖的日历(CCalendar.cs,DateTimeHelper.cs) 3.中国农历年处理(ChineseCalendar.cs) 4.正则表达式辅助(CRegex.cs) 5.CString 的摘要说明。(CString.cs) 6.CText文本内容的库(CText.cs) 7.初始化语言环境(CultureInfoUtil.cs) 8.压缩文本、字节或者文件的压缩辅助(GZipUtil.cs) 9.Log4Net日志记录辅助(LogHelper.cs) 10.中文字符串转换为拼音或者拼音首字母的辅助(PinYinUtil.cs) 11.随机汉字辅助(RandomChinese.cs) 12.反射操作辅助,如获取或设置字段、属性的值等反射信息。(ReflectionUtil.cs) 13.注册表操作辅助(RegistryHelper.cs) 14.用于验证码图片识别的(UnCodebase.cs) 15.将原始字串转换为unicode,格式为\u.\u.( UnicodeHelper.cs)
----------Database-------------- 1.DataTable帮助(DataTableHelper.cs)2.Access数据库文件操作辅助(JetAccessUtil.cs)3.常用的Access数据库Sql操作辅助库(OleDbHelper.cs4.根据各种不同数据库生成不同【分页语句的辅助】 PagerHelper(PageHelper.cs)5.查询条件组合辅助(SearchCondition.cs)6.查询信息实体(SearchInfo.cs)7.型(例如:int,string,double)转换(SmartDataReader.cs)8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅助(AudioHelper.cs)摄像头操作辅助,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs)提供用户硬件唯一信息的辅助(FingerprintHelper.cs)读取指定盘符的硬盘序列号(HardwareInfoHelper.cs)提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点击(MouseHelper.cs)全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs)MP3文件播放操作辅助(MP3Helper.cs) ----------Encrypt-------------- 基于Base64的加密编码(Base64Util.cs)字符串的加密/解密(EncodeHelper.cs)MD5各种长度加密字符、验证MD5等操作辅助(MD5Util.cs)QQ的EncryptUtil(QQEncryptUtil.cs) 非对称加密验证辅助(RSASecurityHelper.cs) ----------File-------------- 用于获取或设置Web.config/*.exe.config中节点数据的辅助(AppConfig.cs)CSV文件和DataTable对象转换辅助(CSVHelper.cs)DatabaseInfo 的摘要说明。(DatabaseInfo.cs)常用的目录操作辅助(DirectoryUtil.cs) Excel操作辅助(无需VBA引用)(ExcelHelper.cs)利用VBA对象,导出DataView到一个Excel文档中的Excel辅助(Export2Excel.cs)关联文件(ExtensionAttachUtil.cs)注册文件关联的辅助(FileAssociationsHelper.cs)打开、保存文件对话框操作辅助(FileDialogHelper.cs)常用的文件操作辅助FileUtil(FileUtil.cs)INI文件操作辅助(INIFileUtil.cs)独立存储操作辅助(IsolatedStorageHelper.cs)序列号操作辅助(Serializer.cs)获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs)简单的Word操作对象(WordCombineUtil.cs)这个提供了一些实用的方法来转换XML和对XmlConvertor.cs)XML操作(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs)这个提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs)byte字节数组操作辅助(BytesTools.cs) 处理数据型转换,数制转换、编码转换相关的(ConvertHelper.cs) CRC校验辅助(CRCUtils.cs) 枚举操作公共(EnumHelper.cs) 身份证操作辅助(IDCardHelper.cs) 检测字符编码的(IdentifyEncoding.cs) RGB颜色操作辅助(MyColors.cs) 日期操作(MyDateTime.cs) 转换人民币大小金额辅助(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅
改正下面C#的代码错误public partial class Form7 : Form { private BigInteger p, q, n, phi_n, e, d; public Form7() { InitializeComponent(); } private void Form7_Load(object sender, EventArgs e) { GenerateKeys(); } private void GenerateKeys() { // 选择两个质数p和q string string1, string2; Int64 n, p, q, phi_n, e; p = BigInteger.Parse("12347534159895123"); q = BigInteger.Parse( "987654321357159852"); n = p * q; phi_n = (p - 1) * (q - 1); // 选择e并计算d e = 65537; d = ModInverse(e, phi_n); publicKeyTextBox.Text = $"{n} {e}"; privateKeyTextBox.Text = $"{n} {d}"; } private BigInteger ModInverse(BigInteger e, long phi_n) { throw new NotImplementedException(); } // 加密函数 private string Encrypt(string message, BigInteger n, BigInteger e) { BigInteger m = StrToBig(message); BigInteger c = BigInteger.ModPow(m, e, n); return BigToStr(c); } // 解密函数 private string Decrypt(string ctext, BigInteger n, BigInteger d) { BigInteger c = StrToBig(ctext); BigInteger m = BigInteger.ModPow(c, d, n); return BigToStr(m); } // 字符串转换为大整数 private BigInteger StrToBig(string str) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str); return new BigInteger(bytes); } // 大整数转换为字符串 private string BigToStr(BigInteger big) { byte[] bytes = big.ToByteArray(); return System.Text.Encoding.Unicode.GetString(bytes); } // 计算模反元素 private BigInteger ModInverse(BigInteger a, BigInteger m) { BigInteger x, y; ExtendedGCD(a, m, out x, out y); return x; } // 扩展欧几里得算法 private void ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y) { if (b == 0) { x = 1; y = 0; } else { ExtendedGCD(b, a % b, out y, out x); y -= a / b * x; } } private void encryptButton_Click(object sender, EventArgs e) { string message = inputTextBox.Text; string ctext = Encrypt(message, n, e); outputTextBox.Text = ctext; } private void decryptButton_Click(object sender, EventArgs e) { string ctext = outputTextBox.Text; string message = Decrypt(ctext, n, d); outputTextBox.Text = message; } } }
06-12
在代码中,有以下几处错误: 1. 在中定义了多个变量,名称相同但型不同: ``` private BigInteger p, q, n, phi_n, e, d; ... Int64 n, p, q, phi_n, e; ``` 需要将其中一个变量改名,避免重名。 2. 在调用 ModInverse 函数时,传入了错误的参数型: ``` private BigInteger ModInverse(BigInteger e, long phi_n) { ... } // 应该改为: private BigInteger ModInverse(BigInteger a, BigInteger m) { ... } ``` 3. 在 Encrypt 和 Decrypt 函数中,调用了未定义的 StrToBig 和 BigToStr 函数。这两个函数的实现已经在代码中给出,但是需要将它们的访问修饰符改为 public,以便在其他函数中调用。 修改后的代码如下: ``` public partial class Form7 : Form { private BigInteger p, q, n, phi_n, e, d; public Form7() { InitializeComponent(); } private void Form7_Load(object sender, EventArgs e) { GenerateKeys(); } private void GenerateKeys() { // 选择两个质数p和q BigInteger p, q, phi_n; p = BigInteger.Parse("12347534159895123"); q = BigInteger.Parse("987654321357159852"); n = p * q; phi_n = (p - 1) * (q - 1); // 选择e并计算d e = 65537; d = ModInverse(e, phi_n); publicKeyTextBox.Text = $"{n} {e}"; privateKeyTextBox.Text = $"{n} {d}"; } // 加密函数 public string Encrypt(string message, BigInteger n, BigInteger e) { BigInteger m = StrToBig(message); BigInteger c = BigInteger.ModPow(m, e, n); return BigToStr(c); } // 解密函数 public string Decrypt(string ctext, BigInteger n, BigInteger d) { BigInteger c = StrToBig(ctext); BigInteger m = BigInteger.ModPow(c, d, n); return BigToStr(m); } // 字符串转换为大整数 public BigInteger StrToBig(string str) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str); return new BigInteger(bytes); } // 大整数转换为字符串 public string BigToStr(BigInteger big) { byte[] bytes = big.ToByteArray(); return System.Text.Encoding.Unicode.GetString(bytes); } // 计算模反元素 public BigInteger ModInverse(BigInteger a, BigInteger m) { BigInteger x, y; ExtendedGCD(a, m, out x, out y); return x; } // 扩展欧几里得算法 public void ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y) { if (b == 0) { x = 1; y = 0; } else { ExtendedGCD(b, a % b, out y, out x); y -= a / b * x; } } private void encryptButton_Click(object sender, EventArgs e) { string message = inputTextBox.Text; string ctext = Encrypt(message, n, e); outputTextBox.Text = ctext; } private void decryptButton_Click(object sender, EventArgs e) { string ctext = outputTextBox.Text; string message = Decrypt(ctext, n, d); outputTextBox.Text = message; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值