C++ builder 2006使用笔记

来源:互联网 发布:网络诈骗电话多少 编辑:程序博客网 时间:2024/05/21 09:57
//初始化GDI+
if (Gdiplus::Ok != GdiplusStartup(&GdiplusToken, &GdiplusStartupInput, NULL)) 
{
   return ;
}

//光标变成手掌

Image1->Cursor=crHandPoint;


//加载XML
TXMLDocument *XmlNetbar;
XmlNetbar->LoadFromFile(ExtractFilePath(Application->ExeName) + "netbar.xml"); 
try {
XmlNetbar->Active = true;//打开
AllAppPath = XmlNetbar->DocumentElement->ChildNodes->FindNode("game1")->GetAttribute("path");//读取game的路径
  }
  catch (...) {
  //弹出警告窗口
Application->MessageBoxA("配置文件加载错误", "蓝海网盟", MB_OK|MB_ICONWARNING);
    Application->Terminate();//安全并且彻底地退出应用程序
  }
//多次使用XML,并写入XML
_di_IXMLDocument XmlNetbargpp = NewXMLDocument();//新建
XmlNetbargpp->Active=true;//打开
try {
XmlNetbargpp->LoadFromFile(ExtractFilePath(Application->ExeName) + "netbar.xml");//加载
XmlNetbargpp->Active=false;//关闭
XmlNetbargpp->Active=true;//打开
String GameTDJpath = XmlNetbargpp->DocumentElement->ChildNodes->FindNode("gametdj")->GetAttribute("path");//读取gametdj的路径
}
catch (...) {
ShowMessage("读取配置错误!请重试!");
return;
}
if(GameTDJpath=="" || !GameTDJpath.Pos("TTPlayer.exe"))//Pos判断是不是包含TTPlayer.exe
{
ShowMessage("无法运行游戏,请输入游戏运行目录。");
if(InputQuery( "请输入路径",   "结尾不带\\号 如:D:\\game",   dk))//输入框
{
if(dk.operator [](dk.Length())=='\\')//1就是第1位,非C++从0开始
{
XmlNetbargpp->Active=false;//关闭==释放
ShowMessage("输入有误!请重新输入!");
return;
}
dk+="\\TTPlayer.exe";
//写入操作,找到唯一节点
_di_IXMLNode node = XmlNetbargpp->DocumentElement->ChildNodes->FindNode("gametdj");
//读取节点下的属性,以数组形式存放
node->AttributeNodes->operator [](0)->NodeValue= (WideString)(dk);
//保存后才能写入
XmlNetbargpp->SaveToFile("netbar.xml");
ShowMessage("设定成功,请重新启动!");
}else
{
    XmlNetbargpp->Active=false;
ShowMessage("进行配置错误!请重试!");
    return;
}
}
else
{
//运行程序
ShellExecute(Application->Handle,"open",GameTDJpath.c_str(),"","",SW_SHOW);
switch(GetLastError())//得到上次执行的错误
{
case 0:
ShowMessage("您的内存不足,请减少运行程序后,再进行游戏!");
break;
case ERROR_FILE_NOT_FOUND:
ShowMessage("无法启动游戏!请设定游戏路径!");
}


//Application: 当前运行的程序
//Application->ExeName:当前运行的程序的exe文件名
//ExtractFilePath(Application->ExeName):当前运行的程序的exe文件所在的目录
String Path = ExtractFilePath(Application->ExeName);


//判断文件是否存在后执行exe与ie
if (FileExists(Path + "Safe\\SafeStart.exe")) {
ShellExecute(Application->Handle,"open",AllAppPath.c_str(),"","",SW_SHOW);//读取XML后的地址存入String AllAppPath
ShellExecute(Application->Handle,"open",Path+"QQ.exe","","",SW_SHOW);//指定文件地址Path+QQ.exe
//执行IE
ShellExecute(NULL, "Open", "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE", "http://www.njoys.com/soft", NULL, SW_SHOW);
  }
//GetLastError()得到上次执行时的错误
switch(GetLastError())
{
case 0:
ShowMessage("您的内存不足,请减少运行程序后,再进行游戏!");
break;
case ERROR_FILE_NOT_FOUND:
ShowMessage("您还没有安装游戏,请下载安装后再进行游戏!");
break;
}


//SetParent指定父窗口(子句柄;父句柄) 成功返回父窗口句柄,失败返0
::SetParent(Handle, FindWindow("Progman", NULL));
//FindWindow查找窗口
//得到了当前桌面上窗口标题为“我的程序”的窗口的窗口句柄
//如果你不知道你要找的窗口的标题,只知道窗口的类名,把NULL该成类名,把“我的程序”改成NULL
HWND   h=FindWindow(NULL, "我的程序 "); 


//移动窗口
MoveWindow(
  hWnd: HWND;               {窗口句柄}
  X, Y: Integer;            {位置}
  nWidth, nHeight: Integer; {大小}
  bRepaint: BOOL            {True 表示刷新; False 表示不刷新}
): BOOL;


//改变指定窗口属性---任务栏隐藏  
SetWindowLong(Application->Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);


//Timer
void   __fastcall   TForm1::Timer1Timer(TObject   *Sender) 

        Timer1-> Enabled   =   0;//先关闭 
        //--------------------------- 
        //在这中间写你的处理函数,确保一个时间只运行一个timer实例 


        //-------------------------- 
        Timer1-> Enabled   =   1;//最后再打开 


//ansistring操作

//截取字符

AnsiString   str= "abcdefghijklmnopqrstuvwxyz "; 
str   =   str.SubString(1,7);

//替换字符串。把字符串中的 "ww "都替换为 "mm " 

void   __fastcall   TForm1::Button1Click(TObject   *Sender) 

        String   strTemp= "wwxxwwxxwwxxxwwxxxxww "; 
        strTemp=StringReplace(strTemp, "ww ", "mm ",TReplaceFlags() < <rfReplaceAll); 
        ShowMessage(strTemp); 
} 

//得到长度

AnsiString   s; 
int   len   =   s.Length();

原创粉丝点击