有不少代码是在csdn上网友发的帖子里面搜集的,看到的.先贴一点.
1.
2进制文件读写
fp=fopen("d://documents//ttt.txt","rb");
if (fp != NULL) {
fseek(fp, 0, SEEK_END); //移到文件末尾
int nSize = ftell(fp); //取得文件长度
fseek(fp, 0, SEEK_SET); //移到文件开头
while(nSize-- > 0)
{
c = fgetc(fp);
hexstr+=AnsiString(IntToHex(c,2));
}
fclose(fp);
}
//----------------------
2.Memo后面添加字符函数
void AppendToMemo(TMemo* Memo, LPCSTR text)
{
int nPos, nNum;
if (Memo==NULL || text==NULL) return;
nNum = Memo->Perform(EM_GETLINECOUNT, 0, 0); //memeo第几行
nPos = Memo->Perform(EM_LINEINDEX, nNum, 0); //满行后的字符数,如果此行数没满,为npos为0
if (nPos < 0)
{nPos = Memo->Perform(EM_LINEINDEX, nNum-1, 0); //前面所有行数的字符
}
nNum = Memo->Perform(EM_LINELENGTH, nPos, 0); //新添加的字符数目 。
Memo->Perform(EM_SETSEL, nPos+nNum, nPos+nNum); // 设置新的位置
Memo->Perform(EM_REPLACESEL, 0, (long)text); //添加字符
}
//------------------------------
3.设置edit居中
{DWORD ExStyleLeft;
DWORD ExStyleRight;
DWORD ExStyleCenter;
ExStyleLeft = GetWindowLong(Edit1->Handle, GWL_STYLE);
ExStyleRight = ExStyleLeft | ES_RIGHT;
ExStyleCenter = ExStyleLeft | ES_CENTER;
SetWindowLong(Edit1->Handle, GWL_STYLE, ExStyleLeft); //居左
SetWindowLong(Edit1->Handle, GWL_STYLE, ExStyleCenter); //居中
SetWindowLong(Edit1->Handle, GWL_STYLE, ExStyleRight); //居右
Edit1->Invalidate();
}
//-----------------------------
4.枚举窗口enumwindows
struct WINLIST
{
HANDLE hWnd;
char cWinBuf[256];
} ;
void CloseSpedia(void);
TForm1 *Form1;
struct WINLIST gWinList[256];
int giCountWin,j;
//-------------------------------------------------------------------
bool CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{
char buffer[256];
GetWindowText(hWnd, buffer, 256);
if ( strlen(buffer) )
{
if (giCountWin < 256)
{
gWinList[ giCountWin].hWnd = hWnd;
strcpy(gWinList[ giCountWin].cWinBuf,buffer);
giCountWin ++;
}
}
return TRUE;
}
//----------------------------------------------------------
bool CALLBACK EnumChildProc( HWND hWnd, LPARAM lParam )
{
char buffer[256];
GetWindowText(hWnd, buffer, 256);
if ( strlen(buffer) )
{
if (giCountWin < 256)
{
gWinList[ giCountWin].hWnd = hWnd;
strcpy(gWinList[ giCountWin].cWinBuf,buffer);
giCountWin ++;
}
}
return TRUE;
}
//-----------------------------------------------------
void CloseSpedia(void)//
{
bool geth=false;
HANDLE gethWnd1=0,gethWnd2=0;
giCountWin = 0;
EnumWindows( (WNDENUMPROC)EnumWindowsProc,0);
for ( j = 0; j<giCountWin; j++)
{
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 控制中心") == 0 )
{
if(!geth){gethWnd1=gWinList[j].hWnd;geth=true;}
else gethWnd2=gWinList[j].hWnd;
ShowWindow(gWinList[j].hWnd,SW_MAXIMIZE); //重新打开窗口
if ( strcmp(gWinList[j].cWinBuf,"未命名 - 画图") == 0 )
{
PostMessage( gWinList[j].hWnd,WM_CLOSE,0,0); //Close the window
// break;
}
}
giCountWin = 0;
EnumChildWindows( gethWnd1 , (WNDENUMPROC)EnumChildProc , 0 ) ;
ShowMessage(IntToStr(giCountWin).c_str());
for ( j = 0; j<giCountWin; j++)
{
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
if ( strcmp(gWinList[j].cWinBuf,"斗地主") == 0 )
{
// gethWnd=gWinList[j].hWnd
// ShowWindow(gWinList[j].hWnd,SW_MAXIMIZE); //重新打开窗口
ShowMessage("qqgame");
}
}
giCountWin = 0;
EnumChildWindows( gethWnd2 , (WNDENUMPROC)EnumChildProc , 0 ) ;
ShowMessage(IntToStr(giCountWin).c_str());
for ( j = 0; j<giCountWin; j++)
{
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 控制中心") == 0 )
{
gethWnd=gWinList[j].hWnd
ShowMessage(gWinList[j].cWinBuf);
}
}
}
//----------------------
5.遍历窗口所有具有color属性,改为red.
{PPropInfo PropInfo;
for(int i=0;i<=ComponentCount;i++)
PropInfo=GetPropInfo((TTypeInfo*)Components[i]->ClassInfo(),"Color");
if(PropInfo)
SetOrdProp(Components[i],PropInfo,clRed);
}
6-treeview添加节点.-
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AnsiString Path=Edit1->Text;
if(Path.SubString(Path.Length(),1)!="//")
Path+="//";
TTreeNode * Node1=TreeView3->Items->Add(NULL,Path);
TSearchRec sr;
if (FindFirst(Path+"*.*", faAnyFile, sr) == 0)
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..")
{
TreeView3->Items->AddChild(Node1,sr.Name);
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeView3Change(TObject *Sender, TTreeNode *Node)
{
Screen->Cursor=crHourGlass;
Animate1->Active=true;
TSearchRec sr;
TTreeNode * NodeTmp=Node;
AnsiString DirName,DirTmp;
DirName=Node->Text;
for(int I=Node->Level ;I>0 ;I--)
{
NodeTmp=NodeTmp->Parent;
DirTmp=NodeTmp->Text;
if(DirTmp.SubString(DirTmp.Length(),1)!="//")
DirTmp+="//";
DirName.Insert(DirTmp,0);
}
if(DirName.SubString(DirName.Length(),1)!="//")
DirName+="//";
for(int J=0;J<Node->Count;J++)
{
if(Node->Item[J]->Count==0);
{
if (FindFirst(DirName+Node->Item[J]->Text+"//*.*", faAnyFile, sr) == 0)
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..")
{
TreeView3->Items->AddChild(Node->Item[J] ,sr.Name);
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
}
Screen->Cursor=crDefault;
Animate1->Active=false;
}
//---------------------------------------------------------------------------