1. 程式人生 > >bcb裡面一些小程式碼..

bcb裡面一些小程式碼..

有不少程式碼是在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;
}
//---------------------------------------------------------------------------