载入中,请稍候……

vc编程常用小技巧

Admin 于 2008-09-23 06:55:43 发表C/C++

被阅442次, 0票VC++ 发表评论

C++实现获取汉字拼音首字母

Admin 于 2008-09-21 05:52:59 发表C/C++

    中英文数字字母标点混合的情况,会自动过滤中文汉字和英文字母外的内容,如果取不到某个字符的拼音首字母就自动跳过。测试了下,基本上可以用。不过不是很完美,对某些汉字没办法取到首字母,比如现在很火的 “囧”,多音字也没办法处理。

  1. //根据汉字字符串返回首字母字符串 
  2. #include <Windows.h> 
  3. class GB2Alpha 
  4. public
  5.  
  6.     GB2Alpha() 
  7.     { 
  8.         pAlpha = NULL; 
  9.     } 
  10.      
  11.     ~GB2Alpha() 
  12.     { 
  13.         if (pAlpha) 
  14.         { 
  15.             delete[] pAlpha; 
  16.             pAlpha = NULL; 
  17.         } 
  18.     } 
  19.  
  20.     //得到实际的拼音首字母buffer 
  21.     const char* GetAlpha(const char* pStr) 
  22.     { 
  23.         if (pAlpha) 
  24.         { 
  25.             delete pAlpha; 
  26.             pAlpha = NULL; 
  27.         } 
  28.         if (!pStr) 
  29.         { 
  30.             return NULL; 
  31.         } 
  32.  
  33.         char   chr[3];    
  34.         wchar_t   wchr   =   0;  
  35.  
  36.         unsigned int nlen = strlen(pStr); 
  37.         pAlpha = new char[nlen+1]; 
  38.         memset(pAlpha,0x00,nlen+1); 
  39.  
  40.         int nIndex = 0; //首字母数组的长度索引 
  41.  
  42.         for (int i = 0;i< nlen ;) 
  43.         { 
  44.             char tmpchar = '\0'
  45.  
  46.             if (pStr[i] > 0) //非汉字 
  47.             { 
  48.                 if (pStr[i] >= 'a' && pStr[i] <= 'z'
  49.                 { 
  50.                     tmpchar = (char)(pStr[i] - 'a' + 'A'); 
  51.                     pAlpha[nIndex++] = tmpchar; 
  52.                 } 
  53.                 if (pStr[i] >= 'A' && pStr[i] <= 'Z'
  54.                 { 
  55.                     tmpchar = pStr[i]; 
  56.                     pAlpha[nIndex++] = tmpchar; 
  57.                 } 
  58.                 i++;  
  59.             } 
  60.             else //汉字 
  61.             { 
  62.                 memset(chr,   0x00,   sizeof(chr));    
  63.                 chr[0]   =   pStr[i++];    
  64.                 chr[1]   =   pStr[i++];    
  65.                 chr[2]   =   '\0';    
  66.  
  67.                 wchr   =   0;    
  68.                 wchr   =   (chr[0]   &   0xff)   <<   8;    
  69.                 wchr   |=  (chr[1]   &   0xff);    
  70.  
  71.                 tmpchar = convert(wchr); 
  72.                 if(tmpchar != '\0'
  73.                 { 
  74.                     pAlpha[nIndex++] = tmpchar; 
  75.                 } 
  76.             } 
  77.         } 
  78.         return pAlpha; 
  79.     } 
  80.  
  81. private
  82.     //汉字首字母转换表 
  83.     char   convert(wchar_t   n)    
  84.     {    
  85.         if   (In(0xB0A1,0xB0C4,n))   return   'A';    
  86.         if   (In(0XB0C5,0XB2C0,n))   return   'B';    
  87.         if   (In(0xB2C1,0xB4ED,n))   return   'C';    
  88.         if   (In(0xB4EE,0xB6E9,n))   return   'D';    
  89.         if   (In(0xB6EA,0xB7A1,n))   return   'E';    
  90.         if   (In(0xB7A2,0xB8c0,n))   return   'F';    
  91.         if   (In(0xB8C1,0xB9FD,n))   return   'G';    
  92.         if   (In(0xB9FE,0xBBF6,n))   return   'H';    
  93.         if   (In(0xBBF7,0xBFA5,n))   return   'J';    
  94.         if   (In(0xBFA6,0xC0AB,n))   return   'K';    
  95.         if   (In(0xC0AC,0xC2E7,n))   return   'L';    
  96.         if   (In(0xC2E8,0xC4C2,n))   return   'M';    
  97.         if   (In(0xC4C3,0xC5B5,n))   return   'N';    
  98.         if   (In(0xC5B6,0xC5BD,n))   return   'O';    
  99.         if   (In(0xC5BE,0xC6D9,n))   return   'P';    
  100.         if   (In(0xC6DA,0xC8BA,n))   return   'Q';    
  101.         if   (In(0xC8BB,0xC8F5,n))   return   'R';    
  102.         if   (In(0xC8F6,0xCBF0,n))   return   'S';    
  103.         if   (In(0xCBFA,0xCDD9,n))   return   'T';    
  104.         if   (In(0xCDDA,0xCEF3,n))   return   'W';    
  105.         if   (In(0xCEF4,0xD188,n))   return   'X';    
  106.         if   (In(0xD1B9,0xD4D0,n))   return   'Y';    
  107.         if   (In(0xD4D1,0xD7F9,n))   return   'Z';    
  108.         return   '\0';    
  109.     }    
  110.  
  111.     bool   In(wchar_t   start,   wchar_t   end,   wchar_t   code)    
  112.     {    
  113.         if   (code   >=   start   &&   code   <=   end)      
  114.         {    
  115.             return   true;    
  116.         }    
  117.         return   false;    
  118.     }    
  119.  
  120. protected
  121.     char* pAlpha; 
  122. }; 

 

被阅733次, 0票 发表评论

编码字符之间的转换(C/C++)

Admin 于 2008-09-21 04:22:10 发表C/C++

最近一段做一些关于文字编码方面的东西,常常涉及到各种编码字符之间的转换。主要是做中日文方面的,包括中文gb2312, 日文JIS, SHIFT-JIS,以及他们和Unnicode码之间的转换。

被阅841次, 0票编码 VC++ 发表评论

VC实现文件拖拽

Admin 于 2008-09-20 22:36:27 发表C/C++

使用过QQ的人都知道,只要把文件拖拽到消息框中就可以传送文件了。那么这种功能是如何实现的呢?其实很简单,只需要响应一个WM_DROPFILES消息就可以了。
在基于对话框的程序中,默认是没有这个消息的,按下Ctrl+W,弹出类向导对话框,选择Class Info标签,在Message fileter下拉列表中选择Window,然后再点击Message Maps标签,就出现WM_DROPFILES消息了,添加该消息的响应函数:

  1. void CTestDlg::OnDropFiles(HDROP hDropInfo)  
  2.          // TODO: Add your message handler code here and/or call default 
  3.          CDialog::OnDropFiles(hDropInfo); 

另外,要让对话框能够接受文件拖拽,还需要设置对话框属性。在对话框上点击右键,选择Properties->Extended Styles,点选Accept files选项即可。

要获得当前拖拽的文件的完整文件名(含路径),只需要一个函数:

  1. UINT DragQueryFile( 
  2.     HDROP hDrop, 
  3.     UINT iFile, 
  4.     LPTSTR lpszFile, 
  5.     UINT cch 
  6. ); 

参数解释:
    hDrop: HDROP标识符,即响应函数中的hDropInfo参数
    iFile: 待查询的文件索引号,从0开始。可以同时拖拽多个文件,因此就需要一个索引号来进行区分。如果该参数为0xFFFFFFFF,则该函数返回拖拽的文件的个数
    lpszFile: 用于存放文件名的缓冲区首地址
    cch: 缓冲区长度
    返回值:文件名长度

另外,查询完成后需要释放系统分配内存,使用下面这个函数:

  1. VOID DragFinish( 
  2.     HDROP hDrop 
  3. ); 

下面是一个完整的代码示例,将文件拖拽到对话框上后会弹出消息框显示完整文件名:

  1. void CTestDlg::OnDropFiles(HDROP hDropInfo)  
  2.          // TODO: Add your message handler code here and/or call default 
  3.          UINT count; 
  4.          char filePath[200]; 
  5.  
  6.          count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0); 
  7.          if(count) 
  8.          {         
  9.                   for(UINT i=0; i<count; i++) 
  10.                   { 
  11.                            int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath)); 
  12.                            AfxMessageBox(filePath); 
  13.                   } 
  14.          } 
  15.  
  16.          DragFinish(hDropInfo); 
  17.  
  18.          CDialog::OnDropFiles(hDropInfo); 

同理,如果只有把文件拖拽到特定的控件中时才有响应,只需要把该控件的Accept files样式勾选上即可。

被阅512次, 0票 发表评论

VC++中网页内容提取范例:提取企业黄页信息

Admin 于 2008-09-20 16:52:22 发表C/C++

内容包括网页下载、分析、文本编码转换等:

被阅988次, 0票 发表评论

VC系统托盘编程

Admin 于 2008-09-20 16:34:47 发表C/C++

被阅805次, 0票 发表评论

在MFC或者C++中使用GDI+

Admin 于 2008-09-13 03:08:30 发表C/C++

首先在stdafx.h或者其他头文件里面加上以下代码,以包含GDI+的头文件和lib文件:

  1. #include <gdiplus.h> 
  2. using namespace Gdiplus; 
  3. #pragma comment(lib, "gdiplus.lib") 

然后在CWinApp继承下来的应用程序类里面声明两个变量:

  1. private
  2. GdiplusStartupInput gdiplusStartupInput; 
  3. ULONG_PTR gdiplusToken; 

在初始化函数InitInstance()里面加入:

  1. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 

到这里,GDI+的初始化工作已经完成,可以在应用程序当中使用GDI+了!
在退出应用程序之前,还需要关闭GDI+,你可以在CWinApp::ExitInstance()当中加入:

  1. GdiplusShutdown(gdiplusToken); 

 

被阅656次, 0票MFC GDI+ VC++ 发表评论

Visual C++获取程序当前路径

Admin 于 2008-09-13 03:06:53 发表C/C++

使用第一段代码可以获得应用程序运行时所在目录。但由于在使用Microsoft Visual Studio.NET编译并运行项目时,真正的宿主是IDE,所以当前目录是项目所在目录,并不是DEBUG或者RELEASE目录,这一点需要注意。而第二段代码可以动态解决此问题。并且增加了Unicode支持(TCHAR)。

被阅496次, 0票VC++ 发表评论
2 / 3 / 20 | « 1 2 3 » |

Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 迷你博客. All rights reserved.

粤ICP备07500939号