C++ Builder技巧集锦

来源:互联网 发布:淘宝新店引流的软件 编辑:程序博客网 时间:2024/05/16 10:07
  1 /*  2 调用DOS程序时不显示窗口  3 使 用 ShellExecute调 用 DOS程 序 时 可 以 不 显 示 窗 口 , 如 :*/  4 ShellExecute(0, "open", "c:\\tools\\arj.exe", "a c:\\p.arj c:\\*.bat c:\\*.sys", NULL, SW_HIDE);    5 /*产生随机数代码*/  6 int SecVal;  7 AnsiString Hour,Minute,Second;  8 int RandomVal;  9 Hour=Now().FormatString("hh"); 10 Minute=Now().FormatString("nn"); 11 Second=Now().FormatString("ss"); 12 SecVal=StrToInt(Hour)*3600+StrToInt(Minute)*60+StrToInt(Second); 13 for(int i=1;i<=SecVal;i++) 14 { 15     RandomVal=random(1000); 16 } 17 Form1->Caption=IntToStr(RandomVal); 18 /*得到文件版本的函数*/ 19 AnsiString __fastcall TMainForm::returnversion( void ) 20 { 21     int ii; 22     unsigned int jj = sizeof( VS_FIXEDFILEINFO ); 23     char *pp, pp2[100]; 24     AnsiString ss; 25     VS_FIXEDFILEINFO *aa; 26     void **pp1 = (void **)&aa; 27     pp2[0] = 0; 28     ss = "()"; 29     ii = GetFileVersionInfoSize( Application->ExeName.c_str(), NULL ); 30     if( ii != 0 ) { 31         pp = new char[ii]; 32         if( GetFileVersionInfo( Application->ExeName.c_str(), 33             0, ii, pp ) ) { 34             if( VerQueryValue( pp, "\\", pp1, &jj ) ){ 35                 ss = " V"; 36                 ss += IntToStr( HIWORD(aa->dwFileVersionMS) )+"."; 37                 ss += IntToStr( LOWORD(aa->dwFileVersionMS) )+"."; 38                 ss += IntToStr( HIWORD(aa->dwFileVersionLS) )+"(build:"; 39                 ss += IntToStr( LOWORD(aa->dwFileVersionLS) )+")"; 40             } 41         } 42         delete pp; 43     } 44     return ss; 45 } 46 /* 47 得到正在运行的WINDOWS版本 48 WINDOWS版本多的很,你想这么多总是会存在着不兼容性,所以看看清它是什么有时会很重要! 49 加入下要的代码吧:*/ 50 void __fastcall TForm1::FormCreate(TObject *Sender) 51 { 52     OSVERSIONINFO info ; 53     info.dwOSVersionInfoSize = sizeof (info) ; 54     GetVersionEx (&info) ; 55     switch (info.dwPlatformId) 56     { 57     case VER_PLATFORM_WIN32s: 58         Label1->Caption = "System: Windows Win 32s" ; 59         break ; 60     case VER_PLATFORM_WIN32_WINDOWS: 61         Label1->Caption = "System: Windows 95" ; 62         break ; 63     case VER_PLATFORM_WIN32_NT: 64         Label1->Caption = "System: Windows NT" ; 65         break ; 66     default: 67         Label1->Caption = "System: Unknown" ; 68         break ; 69     } 70     Label2->Caption = String ("Version: ") 71         + String ((int) info.dwMajorVersion) + "." + String((int)info.dwMinorVersion) ; 72     Label3->Caption = String ("Build: ") + String ((int) (info.dwBuildNumber & 0xFFFF)) ; 73     Label4->Caption = String ("System Info: '") + info.szCSDVersion + "'" ; 74 } 75  76 /* 77 让TRichEdit自动滚动 78 我们用TIMER加上对WINDOWS发消息就可以实现了! 79 */ 80 void __fastcall TForm1::Timer1Timer(TObject *Sender) 81 { 82     SendMessage(RichEdit1->Handle, EM_SCROLL, SB_LINEDOWN, 0); 83 } 84  85 /* 86 除了一行一行的向下滚还可以有什么效果呢?看看下面! 87 */ 88 SB_LINEDOWN 向下一行 89 SB_LINEUP 向上一行 90 SB_PAGEDOWN 向下一页 91 SB_PAGEUP 向上一页 92  93 /* 94 改一下就行了。 95 如何加入主页链接 96 你是不是有注意在一些软件中有一个htpp://www.XXX.com/一点就会在浏览器中自动打开这个主页。我们要如何实现这一步呢?简单的很看看下面! 97 在窗体上加入一个LABEL; 98 定义Label的CAPTION为“加密金刚锁主页”; 99 加入下面代码:100 */101 void __fastcall TForm2::Label1Click(TObject *Sender)102 {103     ShellExecute(Handle,"open","http/www.encrypter.net",0,0,SW_SHOW);104 }105 106 /*107 好了,你可以点了,当然你出入一些特效如Cursor变成手形就更好了!108 如何检测本机是否与Internet连接109 */110 HRASCONN TMainForm::CheckForConnections()111 {112     char buff[256];113     RASCONN rc;114     rc.dwSize = sizeof(RASCONN);115     DWORD numConns;116     DWORD size = rc.dwSize;117     // Enumerate the connections.118     DWORD res = fRasEnumConnections(&rc, &size, &numConns);119     if (!res && numConns == 0)120         // No connections, return 0.121         return 0;122     if (res) {123         // Error. Report it.124         fRasGetErrorString(res, buff, sizeof(buff));125         Memo1->Lines->Add(buff);126     } else {127         // Get the connection status.128         RASCONNSTATUS status;129         status.dwSize = sizeof(status);130         res = fRasGetConnectStatus(rc.hrasconn, &status);131         if (res) {132             // Error. Report it.133             fRasGetErrorString(res, buff, sizeof(buff));134             Memo1->Lines->Add(buff);135             return 0;136         } else {137             // Found connection, show details.138             if (status.rasconnstate == RASCS_Connected) {139                 Memo1->Lines->Add("Existing connection found:");140                 Memo1->Lines->Add(" Device type: " +141                     String(rc.szDeviceType));142                 Memo1->Lines->Add(" Device name: " +143                     String(rc.szDeviceName));144                 Memo1->Lines->Add(" Connected to: " +145                     String(rc.szEntryName));146                 return rc.hrasconn;147             } else {148                 // A connection was detected but its149                 // status is RASCS_Disconnected.150                 Memo1->Lines->Add("Connection Error");151                 return 0;152             }153         }154     }155     return 0;156 }157 158 /*159 如何显示和不显示鼠标160 在超级解霸中我们以发现,在播放的时候MOUSE会自动消失,这样的效果有时真的有用,它是如何实现的。161 在WINDOWS中API函数ShowCursor(bool bShow);是这个程序的主角。当bShow为true,Mouse出现,为false,MOUSE消失。下面我们来做一个单击窗体MOUSE消失,再击又出来的效果。162 */163 void __fastcall TForm1::FormClick(TObject *Sender)164 {165     if(i==1)166         ShowCursor(false);167     if(-i==1)168         ShowCursor(true);169     i=-i;170 }171 172 /*173 你可以跟据需要来做出各种效果!174 如何显示透明图片175 GIF是可以透明的,但是只能支持256色,BMP不支持透明但可以支持真彩,有没有方法可以实现BMP的透明呢?答案是肯定的。176 我们点一个BUTTON是出现透明的图片画在FORM上的效果!177 */178 void __fastcall TForm1::Button1Click(TObject *Sender)179 {180     Graphics::TBitmap *pBitmap=new Graphics::TBitmap();181     pBitmap->LoadFromFile("test.bmp");//图片文件182     pBitmap->Transparent = true; //设置透明属性为真183     pBitmap->TransparentColor=clBlack;//设置透明色为黑色184     pBitmap->TransparentMode = tmAuto;185     Form1->Canvas->Draw(0,0,pBitmap);186     delete pBitmap;187 }188 189 /*190 如何自己写代码遍历文件夹?191 比如 遍历C:\winnt\ 下的所有文件夹,并把所有文件夹的名字写入到C:\folder.txt中去192 */193 void __fastcall TForm1::Button1Click(TObject *Sender)194 {195     TSearchRec sr;196     TStringList* sss=new TStringList();197     if (FindFirst("c:\\winnt", iAttributes, sr) == 0)198     {199         do200         {201             if ((sr.Attr & iAttributes) == sr.Attr)202             {203                 sss->Add(sr.Name);204             }205         } while (FindNext(sr) == 0);206         FindClose(sr);207     }208     sss->SaveToFile("c:\\folder.txt");209 }210 211 /*212 锁定鼠标213 以下是锁定在Form中的例子,如果你想锁定你规定的区域,改变 R 即可。214 譬如215 */216 R = Rect(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));217 218 /*219 锁定在Form中:220 */221 TRect R;222 R = BoundsRect;223 ClipCursor(&R);224 225 /*226 解锁227 */228 TRect R;229 R = Rect(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));230 ClipCursor(&R);231 232 /*233 拖动一个无标题栏的窗体234 在前面我们刚说了如何做一个不规则的窗体,这时我们会有一个新问题,不规则的窗体我们不能让标题栏还在上面,这样多不好看,那没有有标题栏我们如何来拖动这个窗体呢?下面我们先看看分析吧!235 */236 Windows分客户区和非客户区(标题栏、菜单、窗口框),我们只要在点下客户区时发出在非客户区标题栏的消息就能达到目的了。看看是如何做到的。237 void __fastcall TForm1::CreateParams(TCreateParams &Params)238 {239     TForm::CreateParams(Params);240     Params.Style &= ~WS_CAPTION;241 }242 void __fastcall TForm1::WMNCHitTest(TMessage &Msg)243 {244     if (GetAsyncKeyState(VK_LBUTTON)<0)245         Msg.Result = HTCAPTION;246     else247         Msg.Result = HTCLIENT;248 }249 250 /*251 获取进程句柄后如何和窗口句柄相联系?252 按钮按下调用函数CreateProcess(strFileName,NULL,NULL,NULL,FALSE,0,NULL,&StartupInfo,&ProcessInfo);253 */254 //ProcessInfo是一个全局变量,据此可获取进程句柄,进程ID,线程ID等255 256 /*257 进程启动完毕,然后调用函数EnumWindows(EnumWindowsProc,0)258 */259 //EnumWindowsProc是回调函数260 261 /*262 回调函数EnumWindowsProc为:263 */264 BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)265 {266     CString strPrompt;267     DWORD ProcId;268     DWORD ThreadId;269     ThreadId=GetWindowThreadProcessId(hwnd,&ProcId)270         if(ProcId==ProcessInfo.dwProcessId)271         {272             HWND targetWin;273             targetWin=hwnd;274             while(true)275             {276                 HWND hTemp;277                 hTemp=GetParent(targetWin);278                 if(hTemp==NULL)279                     break;280                 else281                     targetWin=hTemp;282             }283             char szTemp[255];284             sprintf(szTemp,"%x",ProcId);285             CString strTemp=szTemp;286             GetWindowText(targetWin,strTemp.GetBuffer(255),255);287             return FALSE;288         }289         else290             return true;291 }292 293 /*294 最小化和恢复程序的方法295 1)296 */297 // To minimize, pass SC_MINIMIZE as the WPARAM298 SendMessage(Application->Handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);299 // To restore, pass SC_RESTORE as the WPARAM300 SendMessage(Application->Handle, WM_SYSCOMMAND, SC_RESTORE, 0);301 2)302 // To minimize, pass SW_MINIMIZE to ShowWindow303 ShowWindow(Application->Handle, SW_MINIMIZE);304 // To restore, pass SW_RESTORE to ShowWindow305 ShowWindow(Application->Handle, SW_RESTORE);306 3)307 // To minimize the app, call Minimize308 Application->Minimize();309 // To restore the app, call Restore310 Application->Restore();311 312 /*313 个人看法,第一组方法最为灵活,可能用处比较的大,如果网友象再最小化窗口是搞出一些动画效果,那么用第二组函数,第三组没有什么特别的地方。314 关闭外部应用程序315 先利用FindWindow函数找出要关闭的应用程序的主窗口名字,然后发送一个消息即可。316 实例如下:317 */318 HWnd HWndCalculator;319 HWndCalculator = Winprocs->FindWindow(nil, "计算器"); // close the exist Calculator320 if (HWndCalculator)321 SendMessage(HWndCalculator, WM_CLOSE, 0, 0);

 

0 0
原创粉丝点击