很 多 软 件 可 以 判 断 所 运 行 的 电 脑 类 型 而 自 动 做 不同 的 处 理。 如PhotoShop 5 可 以 探 测CPU 是 否 有MMX 支 持 而 调 用 不同 的 处 理 函 数,《 金 山 词 霸》 发 现 有MMX 支 持 会 产 生 半 透 明的 翻 译 提 示, 很 多 软 件 可 以 区 分Intel,Cyrix,AMD 的CPU...
---- 现 在, 且 让 我 细 细 道 来 如 何 让 你 在 自 己 的 程 序 中 取得CPU 信 息。
---- 主 要 可 利 用 CPUID 汇 编 指 令( 机 器 码:0FH A2H, 如 果 你 的编 译 器 不 支 持CPUID 指 令, 只 有 emit 机 器 码 了) 该 指 令 可 以 被如 下CPU 识 别
Intel 486 以 上 的CPU,
Cyrix M1 以 上 的CPU,
AMD Am486 以 上 的CPU
(1) 取CPU OEM 字 符 串, 判 断CPU 厂 商
先 让EAX=0, 再 调 用CPUID
Inel 的CPU 将 返 回:
EBX:756E6547H 'Genu'
EDX:49656E69H 'ineI'
ECX:6C65746EH 'ntel'
EBX,EDX,ECX 将 连 成"GenuineIntel", 真 正 的Intel。
Cyrix 的CPU 将 返 回:
EBX:43797269H
EDX:78496E73H
ECX:74656164H
"CyrixInstead","Cyrix 来 代 替"。
AMD 的CPU 将 返 回:
EBX:41757468H
EDX:656E7469H
ECX:63414D44H
"AuthenticAMD", 可 信 的AMD。
---- 在Windows98 中, 用 右 键 单 击" 我 的 电 脑", 选 择" 属 性- 常规" 在 计 算 机 描 述 处 就 可 看 见CPU OEM 字 符 串。
(2)CPU 到 底 是 几86, 是 否 支 持MMX
先 让EAX=1, 再 调 用CPUID
EAX 的 8 到11 位 就 表 明 是 几86
3 - 386
4 - i486
5 - Pentium
6 - Pentium Pro Pentium II
2 - Dual Processors
EDX 的 第0 位: 有 无FPU
EDX 的 第23 位:CPU 是 否 支 持IA MMX, 很 重 要 啊!如 果 你 想 用 那57 条 新 增 的 指 令, 先 检 查 这 一 位 吧, 否 则 就等 着 看Windows 的" 该 程 序 执 行 了 非 法 指 令, 将 被 关 闭" 吧。
(3) 专 门 检 测 是 否P6 架 构
先 让EAX=1, 再 调 用CPUID
如 果AL=1, 就 是Pentium Pro 或Pentium II
(4) 专 门 检 测AMD 的CPU 信 息
先 让EAX=80000001H, 再 调 用CPUID
如 果EAX=51H, 是AMD K5
如 果EAX=66H, 是K6
EDX 第0 位: 是 否 有FPU
EDX 第23 位,CPU 是 否 支 持MMX,
程 序 如 下: 是C++Builder 的 控 制 台 程 序, 可 以 给 出 你 的" 心" 的 信 息。 如 果 把 这 个 技 术 用 于 DLL 中, 便 可 以 使VB 程 序 也 知道" 心" 的 信 息。
file://------CPUID Instruction Demo Program------------ #include < conio.h > #include < iostream.h > #pragma hdrstop file://------------------------------------------------
#pragma inline #pragma argsused
int main(int argc, char **argv) { char OEMString[13]; int iEAXValue,iEBXValue,iECXValue,iEDXValue;
_asm { mov eax,0 cpuid mov DWORD PTR OEMString,ebx mov DWORD PTR OEMString+4,edx mov DWORD PTR OEMString+8,ecx mov BYTE PTR OEMString+12,0 }
cout< < "This CPU 's OEM String is:"< < OEMString< < endl;
_asm { mov eax,1 cpuid mov iEAXValue,eax mov iEBXValue,ebx mov iECXValue,ecx mov iEDXValue,edx }
if(iEDXValue&0x800000) cout < < "This is MMX CPU"< < endl; else cout < < "None MMX Support."< < endl;
int iCPUFamily=(0xf00 & iEAXValue) > >8;
cout < < "CPU Family is:"< < iCPUFamily< < endl;
_asm{ mov eax,2 CPUID }
if(_AL==1) cout < < "Pentium Pro or Pentium II Found";
getch();
return 0;
} ShowMessage("Could not MakeCurrent");
请问如何用ENTER键进行焦点转移,?
1 在Windows 环 境 下, 要 使 一 个 控 件 取 得 焦 点, 可 在 该 控 件 上 用 鼠 标 单 击 一 下, 或 按Tab 键 将 焦 点 移 至 该 控 件 上。 这 种 控 制 焦 点 切 换 的 方 法 有 时 不 符 合 用 户 的 习 惯。 就 图 一 而 言, 用 户 就 希 望 用Enter 键, 控 制 焦 点 由Edit1 切 换 到Edit2。 要 实 现 这 样 的 功 能 需 借 助WinAPI 函 数SendMessage 来 完 成。 方 法 是: 先 设Form1 的KeyPreview 属 性 为true, 然 后 在Form1 的OnKeyPress 事 件 中 加 入 如 下 的 代 码。 这 样, 用 户 就 可 以 通 过 按Enter, 键 控 制 焦 点 按 定 义 好 的Taborder 顺 序 来 移 动 了 ! void __fastcall TForm1:: FormKeyPress(TObject *Sender, char &Key) { if(Key==VK_RETURN) { SendMessage(this- >Handle,WM_NEXTDLGCTL,0,0); Key=0; } }
2 在edit1的OnKeyPress(event)中加入 if(Key==VK_RETURN)Edit3->SetFocus();
网络蚂蚁的窗口消失?? Timer->Intrval=10; void __fastcall TForm1::Timer1Timer(TObject *Sender) { Form1->Width-=20; Form1->Height-=20; if(Form1->Width==108&&Form1->Height==27) {Form1->Close ();} Form1->Top+=10; Form1->Left+=10;}
(2)TDateTime t1;Form1->Caption="今天是:"+DateToStr(Date())+"现在时间是:"+TimeToStr(Time());(3) AnsiString S; S = ""; if (SelectDirectory("Select Directory", "", S)) SetPath(S);(4) AnsiString S; InputQuery("标题","提示",s);
窗口背景 public: Graphics::TBitmap *bitmap;file://--------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { bitmap=new Graphics::TBitmap; bitmap->LoadFromFile("Arc.bmp"); }file://---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender) { int x,y; y=0; while(y<Height) {x=0; while(x<Width) {Canvas->Draw (x,y,bitmap); x=x+bitmap->Width ; } y=y+bitmap->Height ; } }
如何把JPG文件转成BMP文件??? 先用TImage控件 Image1->Picture->LoadFromFile(你的Jpeg文件) 然后 Graphics::TBitmap*tmp=new Graphics::TBitmap(); tmp->Assign(Image1->Picture->Graphic); tmp->SaveToFile(FileName);
文件读取写入 int i=0; char sn[]="123"; if(!FileExists("aaa.txt")) { i=FileCreate("aaa.txt"); FileWrite(i,sn,strlen(sn)); FileClose(i); ShowMessage("ok");}
{char buf[3]; AnsiString str; int i; i=FileOpen("d://aaa.txt",fmOpenReadWrite); FileRead(i,buf,3); FileClose(i); if(Edit1->Text==AnsiString(buf,3)) {ShowMessage("ok'");}
按键发声 #include <mmsystem.h> AnsiString SoundFile="d://ding.wav"; sndPlaySound(SoundFile.c_str() ,SND_ASYNC);
怎么来使Win98休眠 SetSystemPowerState(true,true); 其中的第一个参数指定是休眠还是挂起(true :前者;false:后者) 第二个参数指定是否强制。
如 何 将 浮 点 数 转 成AnsiString同 时 控 制 其 只 保 留 两 位 小 数 FLOAT aa=9032.569; AnsiString bb=FormatFloat("0.00",aa);
能否从AnsiString a="FF";把a转成10进制? 这样更简单: 前面补上16进制标志0x再转换, AnsiString s="FF"; s="0x"+s; int x=s.ToInt();
如何获取屏幕的分辨率? 系统自动定义了一个全局变量Screen, 可在程序任何地方用Screen->Height,Screen->Width获得当前屏幕分辨率。
谁知道动态数组怎样用 动态数组必须在程序运行期间动态改变大小。 用malloc():为指针分配空间和realloc():动态调整指针分配空间 配合实现。注意要包含stdlib.h。示例如下: #include <stdio.h> #include <alloc.h> #include <string.h> int main(void) { char *str; str = (char *) malloc(10); strcpy(str, "Hello"); printf("String is %s/n Address is %p/n", str, str); str = (char *) realloc(str, 20); printf("String is %s/n New address is %p/n", str, str); free(str); return 0; }
怎样显示随机数? Timer->Interval = 150, 效果不错 void __fastcall TForm1::Timer1Timer(TObject *Sender) { Label1->Caption = random(100);}
void __fastcall TForm1::Button1Click(TObject *Sender) {Timer1->Enabled = ! Timer1->Enabled; } 不要用线程了, Timer占用很小, 不影响程序的响应。 如果要过一段时间停下来:(比如是 1分钟 = 60 秒 =60000ms = 400次*150ms/次 int TimeOut = 400; file://Timer->Interval = 150, 效果不错 void __fastcall TForm1::Timer1Timer(TObject *Sender) { Label1->Caption = random(100); if(--Timeout==0) Timer1->Enabled = ! Timer1->Enabled; } Timer的最小分辨率是55ms也就是18.2次一秒,和8253/8254的频率一样。 最小间隔可以达到1ms 使用<mmsystem.h>的函数windows每隔20ms就要重新确认调用一次线程。
TQuery的参数设置 一、TQuery的参数设置 1. 在SQL属性中:Select * from 表名 where 字段名=:变量名 跟在“ : ”后面的是变量。这样写后,在参数属性中就可以修改该变量的数 据类型等。 2. 对变量的赋值: Query1->Active=false; Query1->Params->Items[0]->AsString=Edit1->Text; Query1->Active=true;//查找符合变量的记录 3. 用DBGrid显示结果 DBGrid的DataSource与DataSource1连接,而DataSource1的DataSet与Tquery1 连接。 二、嵌入SQL语言 通过Query控件嵌入SQL语句建立的查询比Table更简单、更高效。 用一个简单的代码来说明如何建立查询程序: 例如,要建立一个检索表1中书名为book1的程序则在表单上放置 DBGrid,DataSource,Query三个控件加入以下代码: DBGrid1-〉DataSource=DataSource1; DataSource1-〉DataSet=Tqery1; Query1-〉Close(); Query1-〉SQL-〉Clear(); Query1-〉SQL-〉Add(″Select * From 表 Where (书名=′book1′ ″); Query1-〉ExecSQL(); Query-〉Active=true; 你就可以在生成的表格中看到所有名称为book1的记录。
窗口渐变背景色 TColor StartColor,EndColor;//全局变量 file://获得开始颜色 if(ColorDialog1->Execute ()) StartColor=ColorDialog1->Color; file://获得结束颜色 if(ColorDialog1->Execute()) EndColor=ColorDialog1->Color; float pwidth; int redstart,greenstart,bluestart,redend,greenend,blueend; float redinc,greeninc,blueinc; pwidth=float(PaintBox1->Width); file://通过Get(X)Value函数来获得开始的颜色的红,蓝,绿 redstart=GetRValue(StartColor); greenstart=GetGValue(StartColor); bluestart=GetBValue(StartColor); file://通过Get(X)Value函数来获得结束的颜色的红,蓝,绿 redend=GetRValue(EndColor); greenend=GetGValue(EndColor); blueend=GetBValue(EndColor); file://设置颜色变化率 redinc=(redend-redstart)/pwidth; greeninc=(greenend-greenstart)/pwidth; blueinc=(blueend-bluestart)/pwidth; for(int i=0;i<PaintBox1->Width;i++) { file://定义画笔颜色 PaintBox1->Canvas->Pen->Color=TColor(RGB(redstart+int(redinc*i),greenstart+int(greeninc*i),bluestart+int(blueinc*i))); file://移动画笔到 PaintBox1->Canvas->MoveTo(i,0); file://由PanitBox1的上向下画 PaintBox1->Canvas->LineTo(i,PaintBox1->Height);
如何分别对DBGrid和SrtingGRid的行换背景色? TForm1::DBGrid1DrawColumnCell( ) if(Column->Field->DataSet->RecNo%2) { DBGrid1->Canvas->Brush->Color=clYellow; DBGrid1->Canvas->FillRect(Rect); } DrawText(DBGrid1->Canvas->Handle, Column->Field->Text.c_str(),-1,(RECT*)&Rect,DT_SINGLELINE | DT_VCENTER |DT_CENTER);如何分别对DBGrid和SrtingGRid的列动态换背景色? Query1->Close(); Query1->SQL->Clear(); Query1->SQL->Add(Memo1->Text); Query1->Open(); for(int i=0;i<DBGrid1->Columns->Count;i++) { if(i%2==0)DBGrid1->Columns->Items[i]->Color=clAqua; else DBGrid1->Columns->Items[i]->Color=clInfoBk; }
怎样得到当前的月份是多少啊
TDateTime time=Now(); AnsiString Time=time.FormatString("yyyy-mm-dd"); AnsiString month=Time.SubString(6,2); Label1->Caption=month; AnsiString sMonth; sMonth=Now().FormatString("mm");//取出当前月分 Label2->Caption=sMonth;
窗体标题闪烁 Timer() {FlashWindow(Form1->Handle,TRUE):}
像星际争霸开头由大到小的字幕特效 在窗体中加入一个Timer(定时器),标签(Label) Label1->AutoSize=true Label1->Caption=歪歪的VB教程" Label1->Font->Name = "宋体" Label1->Font->Size = 150 Label1->Fore->Color = vbGreen Label1->Visible = true Time1->Interval = 1 Time1Enabled = true void __fastcall TForm1::Timer1Timer(TObject *Sender) { if(Label1->Font->Size >=2) {Label1->Font->Size =Label1->Font->Size-2; Label1->Left = (Form1->Width-Form1->Width)/2; Label1->Top = (Form1->Height -Label1->Height) / 2 ;} else{ Label1->Visible = false; Timer1->Enabled = false; } }
CPUID GetCPUID(void) { asm { PUSH EBX file://{Save affected register} PUSH EDI MOV EDI,EAX file://{@Resukt} MOV EAX,1 DW 0xa20f file://{CPUID Command} STOSD file://{CPUID[1]} file://cpu名称 MOV EAX,EBX STOSD file://{CPUID[2]}//无用 MOV EAX,ECX STOSD file://{CPUID[3]}//无用 MOV EAX,EDX STOSD file://{CPUID[4]}//cpu编号 POP EDI file://{Restore registers} POP EBX } } file://---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { Label1->Caption=(AnsiString)GetCPUID(); }
自动调用浏览器或邮件程序 Windows 提供了ShellExecute函数,用来调用外部程序或与某程序关联的文件。
其原型如下:
HINSTANCE ShellExecute(
HWND hwnd, // handle to parent window
LPCTSTR lpOperation, // pointer to string that specifies operation to perform
LPCTSTR lpFile, // pointer to filename or folder name string
LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory, // pointer to string that specifies default directory
INT nShowCmd // whether file is shown when opened
);
如若要自动浏览器程序访问我的主页,程序代码如下:
ShellExecute(Handle,NULL,"http://lmq.4y.com.cn",NULL,NULL,SW_SHOWNORMAL);
若要启动系统默认邮件程序,给我写信,程序代码如下:
ShellExecute(Handle,NULL,"mailto:lmq@4y.com.cn",NULL,NULL,SW_SHOWNORMAL);
BCB中的获取打印机函数是Printer(); 其返回值是一个TPrinter对象,你可以查找TPrinter对象的帮助。 你需要添加引用的头文件是#include <Printers.hpp> 其中Printer()->Printers->Count是打印机的数目 Printer()->Printers->Strings[]是列举打印机的名称 Printer()->BeginDoc()开始打印准备 Printer()->EndDoc()结束打印准备,开始真正打印 Printer()->AbortDoc()终止打印准备。 在BeginDoc()和EndDoc()之间可以使用Printer()->Canvas来画打印页 画完一页后就可以用Printer()->NewPage()开始新的一页 此外Printer()->Handle是一个HDC,使用GetDeviceCaps(Printer()->Handle, ...) 可以得到当前打印机的分辨率等等很多属性,而不需要BeginDoc() (如果使用Printer()->Canvas->Handle则需要BeginDoc()才能得到准确信息)
获取屏幕的颜色 void __fastcall TForm1::Button1Click(TObject *Sender) {int BitsPerPixel = GetDeviceCaps(Canvas->Handle,BITSPIXEL); int Planes = GetDeviceCaps(Canvas->Handle,PLANES); BitsPerPixel *= Planes; bool UsesPalettes = (bool)(GetDeviceCaps(Canvas->Handle, RASTERCAPS) & RC_PALETTE); if(UsesPalettes) Label1->Caption = "屏幕使用饿调色板"; else Label1->Caption = "屏幕没有使用调色板";
switch (BitsPerPixel) { case 32: Label2->Caption ="32-位 真彩"; case 24: Label2->Caption = "24-位 真彩"; break; case 16: Label2->Caption = "16-位 高 彩"; break; case 8: Label2->Caption = "256 色 模式"; break; case 4: Label2->Caption = "16 色模式"; break; case 1: Label2->Caption = "单色模式"; break; default: Label2->Caption = "屏幕支持 " + IntToStr(1<< BitsPerPixel) +" 不同色"; break; }
使用自己的函数库: new->cpp int jia(int a,int b){return (a+b);} save to File1.cpp at unit.cpp #include "File1.cpp" int c=jia(a,b);
如何使StatusBar上的文字滚动起来?? 如下操作可使StatusBar中的字串向左运动 StatusBar1->SimpleText=StatusBar1->SimpleText.SubString(2,StatusBar1->SimpleText.Length()-1)+StatusBar1->SimpleText.SubString(1,1); file://------------ 设计时定 simplePanel=true; SimpleText=love you SizeGrip=false;
在TStringGrid控件中怎么显示double型数? double aaa = 1.1; AnsiString str = FloatToStr(aaa); StringGrid1->Cells[1][2]=str; StringGrid1->Cells[1][1]= FormatFloat("0.00000",3.1415926);
Table1快速查询 Table1->Filtered=false; Table1->Filter=Edit1->Text; Table1->Filtered=true; Edit1->Text=内容是:字段是字符 name='zzz' and age>18 and sex!='男' FilterOption<<foCaseInsensitive(大小写)<<foNoPartialCompare(完全匹配)
数据库输入员只能修改自己输入的表的内容(好比是输入本月工资可以修改输入的错误确不可修改上月的工资) 建立临时表createbuttonclike() { if(!Table1->Exists){ Table1->Active=false; Table1->DatabaseName="MSACCESS1";//建立一样的表结构 Table1->TableType=ttDefault; Table1->TableName="SCadd"; Table1->FieldDefs->Clear(); Table1->FieldDefs->Add("Sno",ftInteger,0,false);//字段名,类型,小数位或长度,是否唯一 Table1->FieldDefs->Add("Cno",ftInteger,0,false); Table1->FieldDefs->Add("Grade",ftInteger,0,false); Table1->CreateTable();} Table1->Active=true;}ExitButtonClik(){ Table2->BatchMove(Table1,batAppend);batUpdate//用原表的记录更新目标表的记录;batAppendUpdate//把原表不存在于目标表的记录添加到目标表的末尾batDelete//删除原表在目标表中重复的记录batCopy//把原表复制到目标表 Table1->Active=false; Table1->DatabaseName="MSACCESS1"; Table1->TableType=ttDefault; Table1->TableName="SCadd"; Table1->DeleteTable();}
一、用C++Builder设计自己的浏览器
C++Builder5提供了一个浏览器控件CppWebBrowser,它位于internet控件栏,其的主要方法有:
Navigate函数,用于浏览给定的url的资源
GoBack(),浏览上一页
GoForward(),浏览下一页
Stop(),停止浏览
Refresh(),刷新当前页面
新建一应用程序,将工程名保存为myie,设置Form1的Name为Main_Form,在Main_Form上加入一CppWebBrowser控件和一个ToolBar控件,在此ToolBar控件放入一ComBox框,并加上五个ToolButton,设置其Name属性分别为“CppWebBrowser1”,“ToolBar1”,“ CB_URL”,“ TB_Prior,TB_Forward,TB_Stop,TB_Fresh,TB_Navigate”。
TB_Navigate的OnClick事件代码如下:
void __fastcall TMain_Form::NavigateExecute(TObject *Sender)
{
CppWebBrowser1->Navigate((WideString)CB_URL->Text, TNoParam(), TNoParam(), TNoParam(), TNoParam());
}
ComBox1的OnKeyPress事件代码如下:
void __fastcall TMain_Form::CB_URLKeyPress(TObject *Sender, char &Key)
{
if(Key==13) file://若按下的键为回车键
NavigateExecute(Sender);
}
其余的代码类似…
编译运行,一个具有基本浏览功能的浏览器就生成了。
2、获得html文件的源文件
我们在用IE浏览主页时,若点击右键,选择“查看源文件”,系统会自动启动记事本显示此html的源文件。在编程时,有时需分析html文件的源文件,用C++ Builder的 NMHTTP控件可以轻松解决这个问题。
新建 一工程,从FastNet控件栏拖一NMHTTP控件到窗体上,再拖一Memo控件到窗体,假设要获得本人主页(http://lmq.4y.com.cn)的源文件,在Form1的OnCreate事件键入代码:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Memo1->Clear(); file://清空Memo1
NMHTTP1->Get("http://lmq.4y.com.cn");
Memo1->Text = NMHTTP1->Body;
}
编译运行程序,Memo1框中立即显示本人主页的源文件.
另外,NMHTTP控件还支持代理Proxy,其属性Proxy和Port分别指代理服务器的IP地址和端口号。
三、自动调用浏览器或邮件程序
Windows 提供了ShellExecute函数,用来调用外部程序或与某程序关联的文件。
其原型如下:
HINSTANCE ShellExecute(
HWND hwnd, // handle to parent window
LPCTSTR lpOperation, // pointer to string that specifies operation to perform
LPCTSTR lpFile, // pointer to filename or folder name string
LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters
LPCTSTR lpDirectory, // pointer to string that specifies default directory
INT nShowCmd // whether file is shown when opened
);
如若要自动浏览器程序访问我的主页,程序代码如下:
ShellExecute(Handle,NULL,"http://lmq.4y.com.cn",NULL,NULL,SW_SHOWNORMAL);
若要启动系统默认邮件程序,给我写信,程序代码如下:
ShellExecute(Handle,NULL,"mailto:lmq@4y.com.cn",NULL,NULL,SW_SHOWNORMAL);
以上程序在Pwin98+BCB5下运行通过。
file://---------------------------------------------------------------------------------------------------------------
BCB下如何改变CppWebBrowser的Html内容
void __fastcall TForm1::SetHtml( TCppWebBrowser *WebBrowser,AnsiString Html ){ IStream *Stream; HGLOBAL hHTMLText; IPersistStreamInit *psi;
if( WebBrowser->Document == NULL ) return; hHTMLText = GlobalAlloc( GPTR, Html.Length() + 1 ); if( 0 == hHTMLText ) { ShowMessage( "GlobalAlloc Error" ); return; }
CopyMemory( hHTMLText, Html.c_str(), Html.Length() );
OleCheck( CreateStreamOnHGlobal( hHTMLText, true, &Stream ) );
try { OleCheck( WebBrowser->Document->QueryInterface( __uuidof(IPersistStreamInit), (void **)&psi ) ); try { OleCheck( psi->InitNew() ); OleCheck( psi->Load(Stream) ); } catch( ... ) { delete psi; } } catch( ... ) { delete Stream; } delete psi; delete Stream;}
cpu使用情况
#ifndef fmTestH#define fmTestHfile://---------------------------------------------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#include <ExtCtrls.hpp>file://---------------------------------------------------------------------------class TTestForm : public TForm{__published: // IDE-managed Components TLabel *LbAldynUrl; TMemo *MInfo; TTimer *Timer; void __fastcall LbAldynUrlClick(TObject *Sender); void __fastcall FormCreate(TObject *Sender); void __fastcall TimerTimer(TObject *Sender);private: // User declarationspublic: // User declarations __fastcall TTestForm(TComponent* Owner);};file://---------------------------------------------------------------------------extern PACKAGE TTestForm *TestForm;file://---------------------------------------------------------------------------#endif
file://---------------------------------------------------------------------------
#include <vcl.h>#pragma hdrstop
#include "fmTest.h"#include "adCpuUsage.hpp"file://---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TTestForm *TestForm;file://---------------------------------------------------------------------------__fastcall TTestForm::TTestForm(TComponent* Owner) : TForm(Owner){}file://---------------------------------------------------------------------------void __fastcall TTestForm::LbAldynUrlClick(TObject *Sender){ ShellExecute(Application->Handle, "open", "http://www.aldyn.ru/", NULL, NULL, SW_SHOWDEFAULT);}file://---------------------------------------------------------------------------void __fastcall TTestForm::FormCreate(TObject *Sender){ int i;
MInfo->Lines->Clear();
MInfo->Lines->Add(Format("There are %d total CPU in your system",ARRAYOFCONST((GetCPUCount()))));
for (i=0; i < GetCPUCount(); i++) MInfo->Lines->Add("");}file://---------------------------------------------------------------------------void __fastcall TTestForm::TimerTimer(TObject *Sender){ int i;
CollectCPUData();
MInfo->Lines->BeginUpdate();
for(i=0; i < GetCPUCount(); i++) MInfo->Lines->Strings[i+1] = Format("CPU #%d - %5.2f%%",ARRAYOFCONST((i,GetCPUUsage(i)*100)));
MInfo->Lines->EndUpdate();
如何保存程运行信息到ini文件
现在许多软件把程序中需要的数据保存在注册表中,这样当用户装的软件越来越多时,致使注册表越来越庞大,容易使系统出错。 当然,微软也建议在注册表中保存数据,但当我们需要保存的数据不多时完全可以把数据保存在WIN.INI中,这样可以很方便地维护, 实现方法相对来说比较简单。下面我以Borland C++ Builder为例来说说如何实现。 原理其实很简单,只需调用API的 WriteProfileString和GetProfileInt函数就可以了。这两个函数的原型是: BOOL WriteProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString ); UINT GetProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT nDefault); 其中lpAppName指在WIN.INI中段的名字,即用[]括起来的字符串,lpKeyName指在这个段中每一个项目的名字,lpString指这个项目的值, 即“=”后的数, nDefault为当GetProfileInt没有找到lpAppName和lpKeyName时返回的值,即缺省值,前者返回为布尔值(true 或 false), 后者返回为无符号整形值。当在WriteProfileString函数中 lpKeyName 为空(NULL)时,则清除这个段的全部内容,lpString 为空时, 则清除这一项目的内容,即这一行将清除掉。 下面举一例子来说明这两个函数的用法。新建一个应用程序,在Form1上放两个Edit和三个Button,其中Edit的Text为空, 三个Button的Caption分别为“添加”、“查看”、“清除”。双击“添加”按钮加入下面代码: WriteProfileString(“例子程序”,“项目”,Edit1→Text.c_str()); 双击“查看”按钮加入如下代码: unsigned int Temp; Temp=GetProfileInt(“例子程序”,“项目”,100); Edit2→Text=IntToStr(Temp); 双击“清除”按钮加入如下代码: WriteProfileString(“例子程序”,NULL,NULL); 然后按F9键运行程序。 下来可以检验一下程序的正确性。在Edit1中输入数字,如“3265”,按“添加”按钮,这时运行“sysedit”来查看 “WIN.INI”文件的最后面,可以看到加入了如下内容: [例子程序] 项目=3265 其中“[]”和“=”是函数自动加上的。按下“查看”按钮,在Edit2中出现“3265”,当按下“清除”按钮可清除添加的部分。 经过查看可知程序已达到预期的目的。 喜爱编程的朋友可以把上述方法应用到自己的程序中去,来达到保存数据信息的作用。当确实要把信息保存到注册表中, 可以在C++ Builder中定义一个TRegistry类的对象来进行相关的操作,或者直接调用Windows的API函数.
取得本地internet机器的名字及IP地址取得本地internet机器的名字及IP地址
一、下面的例子使用 Winsock API 取得本地主机的名字及地址 void __fastcall TForm1::Button1Click(TObject *Sender) { hostent *p; char s[128]; char *p2; file://Get the computer name gethostname(s, 128); p = gethostbyname(s); Memo1->Lines->Add(p->h_name); file://Get the IpAddress p2 = inet_ntoa(*((in_addr *)p->h_addr)); Memo1->Lines->Add(p2); } void __fastcall TForm1::FormCreate(TObject *Sender) { WORD wVersionRequested; WSADATA wsaData; file://Start up WinSock wVersionRequested = MAKEWORD(1, 1); WSAStartup(wVersionRequested, &wsaData); } void __fastcall TForm1::FormDestroy(TObject *Sender) { WSACleanup(); }
修改显示方式的:
file://---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H file://--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include "MyEdit.h" #include <Grids.hpp> #include <CheckLst.hpp> #include <ExtCtrls.hpp> #include <ActnList.hpp> file://--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TPanel *Panel1; TButton *Button1; TLabel *Label1; TButton *Button2; TListBox *ListBox1; TButton *Button3; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender); void __fastcall Button3Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); DEVMODE table[400]; bool Listed; }; file://--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; file://--------------------------------------------------------------------------- #endif
unit.cpp file://---------------------------------------------------------------------------
#include <vcl.h> #include <mmsystem.h> #pragma hdrstop
#include "Unit1.h" file://--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "MyEdit" #pragma resource "*.dfm" TForm1 *Form1; file://--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){Listed=false;Button2->Enabled=false;Button3->Enabled=false;
}file://---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender){DWORD index=0;bool flag;do{flag=EnumDisplaySettings(NULL,index,& table[index]);if(flag){ if( table[index].dmPelsWidth<640) ; else if (table[index].dmBitsPerPel<8) ; else if (table[index].dmDisplayFrequency>85); else if( table[index].dmPelsWidth>1024) ; else { AnsiString st; st=index; st+=" "; st+=table[index].dmPelsWidth; st+=" X "; st+=table[index].dmPelsHeight; st+=" X "; st+=table[index].dmBitsPerPel; st+=" X "; st+=table[index].dmDisplayFrequency; ListBox1->Items->Add(st); }}index++;}while(flag & index<300);
AnsiString t;t.sprintf(" %d Modes Found",index);Label1->Caption=t;Listed=true;Button2->Enabled=true;}file://---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender){file://GetDeviceCapsint i;if(!Listed){Label1->Caption="List display modes first.";return;}
for(i=0;i<ListBox1->Items->Count;i++){if(ListBox1->Selected[i]){ int x; String st,st1; st=ListBox1->Items->Strings[i]; x=st.Pos(" "); st1=st.SubString(1,x-1); x=st1.ToInt(); Label1->Caption=x; ChangeDisplaySettings(&table[x],0); break; }}if(i==ListBox1->Items->Count)Label1->Caption="Select a modes first.";Button3->Enabled=true;int r=MessageBox(0,"can you see anything?","display",MB_OKCANCEL);if(r==IDCANCEL) Button3Click(Sender);elseButton3->SetFocus();}file://---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender){ChangeDisplaySettings(NULL,0);Button3->Enabled=false;}file://---------------------------------------------------------------------------
怎样实现将Memo的文本信息打印出来
TPrinter * pPrinter=Printer(); pPrinter->Title="打印Memo1中的数据"; pPrinter->BeginDoc(); int y=10; for(int i=0;i<Memo1->Lines->Count;i++) { pPrinter->Canvas->TextOut(10,y,Memo1->Lines->Strings[i]); y+=pPrinter->Canvas->TextHeight("A"); } pPrinter->EndDoc();