国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++Builder代碼片斷

2019-09-06 23:33:51
字體:
供稿:網(wǎng)友

                    本文中包含了一些常用的代碼片斷,看看想想或許有他山之石可以攻玉的可能。

刪除別名中所有的表、純虛函數(shù)、虛函數(shù)、啟動(dòng)頁面、指針、為指針解除引用、表的For循環(huán)
變量與常量的聲明、檢查表是否存在、組件的類名、剪貼板中的文字、字符流、檢查表是否已打開
表的狀態(tài)操作、改變PageControl的標(biāo)簽、向Query傳遞參數(shù) 日期屬性 繪制狀態(tài)條

刪除別名中所有的表
void TData::CleanTemp()
{
 TStringList *myTables = new TStringList();
 TTable *Table = new TTable(this);
 try
 {
   Session->GetTableNames("Temp", "", True, False, myTables);
 }
 catch (...) {}
 // AnsiString str = myTables->GetText();
 // ShowMessage(str);
 for(int count=0; count < myTables->Count; count++)
 {
   Table->DatabaseName = "Temp";
   Table->TableName = myTables->Strings[count];
   Table->Close();
   Table->DeleteTable();
 }
 delete myTables;
 delete Table;
}

純虛函數(shù)
//純虛函數(shù)只在基類中出現(xiàn),而在子類中必須有
//與其匹配的成員函數(shù)。程序中申明的子類的實(shí)例
//必須為基類中的每一個(gè)純虛函數(shù)提供一個(gè)重載的成員函數(shù)。
class TBaseClass
{
 public:
 virtual void Display() = 0;
};
class TDerivedClass : public TBaseClass
{
 public:
 void Display() { ShowMessage("From Derived"); }
};
 
class TSecondDerivedClass : public TDerivedClass
{
 public:
 void Display() { ShowMessage("From Second Derived"); }
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 TDerivedClass dc; dc.Display();// "From Derived"
 TSecondDerivedClass sc; TBaseClass* bc = ≻
 bc->Display(); // "From Second Derived"
}

虛函數(shù)
//虛函數(shù)作為其他類的父類的成員函數(shù)。
//如果繼承子類成員函數(shù)中存在與父類成員函數(shù)完全相同的函數(shù),
//子類中的成員函數(shù)永遠(yuǎn)有效。
class Base
{
public:
 virtual void Display() { ShowMessage("Base Class"); }
};
 
class DerivedOne : public Base
{
 public:
 void Display() { ShowMessage("DerivedOne"); }
};
 
class DerivedTwo : public Base
{
 public:
 void Display() { ShowMessage("DerivedTwo"); }
};
 
Base* pBases[10];
int count = 0;
DerivedOne aDerOne;
DerivedTwo aDerTwo;
pBases[count++] = &aDerOne;
pBases[count++] = &aDerTwo;
for( int i=0; i < count; i++ )
pBases->Display();


啟動(dòng)頁面
USEDATAMODULE("Datamod.cpp", DataModule);
USEFORM("about.cpp", AboutBox);
USEFORM("main.cpp", MainForm);
USEFORM("splash.cpp", SplashForm);
//---------------------------------------------------------------------------
#include "splash.h"
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
 try
 {
   SplashForm = new TSplashForm(Application);
   SplashForm->Show();
   SplashForm->Update();
   Application->Initialize();
   Application->Title = "Example of Loading Splash Form";
   Application->HelpFile = "SplashHelp.hlp";
   Application->CreateForm(__classid(TMainForm), &MainForm);
   Application->CreateForm(__classid(TDataModule), &DataModule);
   Application->CreateForm(__classid(TAboutBox), &AboutBox);
   SplashForm->Hide();
   SplashForm->Close();
   Application->Run();
 }
 catch (Exception &exception)
 {
   Application->ShowException(&exception);
 }
 return 0;
}


指針
int array[] = { 2, 4, 6, 8, 10}
int myInteger = array[3]; // 值為 8
 
// ----使用指針可以實(shí)現(xiàn)同樣的功能 -----
int array[] = { 2, 4, 6, 8, 10}
int* myPtr = array;
int myInteger = myPtr[3]; // 值為8


為指針解除引用
int x = 32;
int* ptr = &x;
//解除指針的引用
//以獲得內(nèi)存位置的內(nèi)容
int y = *ptr; // y = 32


表的For循環(huán)
void TDataModuleEmployee::ListNames( TStrings *Items )
{
 try
 {
   for ( TableAll->First(); !TableAll->Eof; TableAll->Next() )
   if ( TableAll->FieldByName("Deleted")->AsBoolean == false )
   Items->AddObject( TableAll->FieldByName("Name")->AsString, (TObject *)TableAll->FieldByName("Refnum")->AsInteger );
 }
 catch (Exception &e)
 {
   Application->ShowException(&e);
 };
}


變量與常量的聲明
char ch;
int count = 1;
char* name = "csdn.net";
struct complex { float my, his;};
float real(complex* p) {return p->my};
const double pi = 3.1415926535897932385;
templetate abc(T a) { return a < 0 ? -a : a; };
enum WebSite { one, two, three, four};
int* a; // * 指針
char* p[20]; // [ ] 數(shù)組
void myFunction(int); // ( )函數(shù)
struct str { short length; char* p; };
char ch1 = 'a';
char* p = &ch1; // &引用 ,p保持著ch1的地址
char ch2 = *p; // ch2 = 'a'


檢查表是否存在
#include "io.h"
if (access(Table1->TableName.c_str(),0)) //檢查表是否存在
{ // 若不存在就創(chuàng)建 ...
 Table1->Active = false;
 Table1->TableType = ttParadox;
 Table1->FieldDefs->Clear();
 Table1->FieldDefs->Add("Myfield", ftString, 15, false);
 Table1->IndexDefs->Clear();
 Table1->CreateTable();
 Table1->Active = true;
}
else
 Table1->Active = true;


組件的類名
//找出丟失的組件類名
for(int i=0; i < ComponentCount; i++)
{
 if(String(dynamic_cast<TComponent&>(*Components).Name) == "")
 {
   ShowMessage(Components->ClassName());
 }
}


剪貼板中的文字
#include "memory.h" // 包含 auto_ptr<>
#include "clipbrd.hpp" //包含 TClipboard & Clipboard()
// 范例程序,包含了一個(gè)memo控件
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{ //創(chuàng)建 TStringList對(duì)象
 auto_ptr ClipText(new TStringList); //得到剪貼板中的文字的拷貝
 ClipText->Text = Clipboard()->AsText; //然后加工一下...
 Memo1->Lines->AddStrings(ClipText.get());
}


字符流
//范例一
#include "sstream"
const char *name = "cker";
const char *email = "cker@sina.com";
// 生成 "cker"[SMTP:cker@sina.com]
 
ostringstream ost;
ost << """ << name << ""[SMTP:" << email << "]";
Edit1->Text = ost.str().c_str();
 
//范例二
void TDataModuleEmployee::FullReport(const char *title)
{
 Report.header(title);
 Report << sformat( "Employee #%2d: %s%s", TableAllRefnum->Value, TableAllName->Text.c_str(),
 TableAllManagerFlag->Value ?"(Manager)" : "" ) << " Address: " <<
 TableAllAddress->Text.c_str() << endl << " " << TableAllCityProvZip->Text.c_str() <<
 endl << " " << NameCountry(TableAllCanada->Value) << endl;
 Report.footer();
}


檢查表是否已打開
void TData::CheckEdit()
{
 for(int i=0; i < ComponentCount; i++)
 {
   if(dynamic_cast(Components))
   {
     if(((TTable*)Components)->State == dsEdit)
     {
/tString s = "Table " + Components->Name + " is in edit mode" "Would you like to post it before entering new task?";
/tif(MessageBox(NULL,s.c_str(),"Table in Edit Mode",MB_YESNO | MB_ICONINFORMATION) == IDYES)
/t  ((TTable*)Components)->Post();
/telse
/t  ((TTable*)Components)->Cancel();
     }
   }
 }
}


表的狀態(tài)操作
//關(guān)閉已打開的表并將他們恢復(fù)成初始狀態(tài)。
void TData::MyTables(TForm * sender)
{
 int i;
 TTable * Table;
 bool *active = new bool[MyClass->ComponentCount];//在動(dòng)態(tài)數(shù)組中存放每個(gè)表的初始狀態(tài),然后關(guān)閉所有的表
 for(i = 0; i < MyClass->ComponentCount; i++)
 {
   try
   {
     if((Table = dynamic_cast(MyClass->Components)) != NULL)
     {
/tactive = Table->Active;
/tTable->Active = false;
     }
   }
   catch(...) {}; //異常應(yīng)該只來自于dynamic cast...
 }
 for(i = 0; i < MyClass->ComponentCount; i++)
 {
   try
   {
     if((Table = dynamic_cast(MyClass->Components)) != NULL)
     {
/tif(Table->DatabaseName != OPTARDatabase->DatabaseName)
/t  continue;
/tDBIResult result = DBIERR_NONE + 1;
/twhile(result != DBIERR_NONE) //若希望的話,這樣允許用戶重試!
/t{
/t  result = DbiPackTable (OPTARDatabase->Handle,NULL,Table->TableName.c_str(),NULL, true);
/t  if(result != DBIERR_NONE)
/t  {
/t    AnsiString rsltText = "Unable to pack " + Table->TableName + "." ;
/t    char rslt[DBIMAXMSGLEN + 1];
/t    DbiGetErrorString(result, rslt) rsltText += ". Try again?";
/t    if(Application->MessageBox(rsltText.c_str(), "Error!",MB_YESNO) != IDYES)
/t      break;
/t  }
/t}
     }
   }
   catch (...) {}; //異常應(yīng)該只來自于dynamic cast...
 }
 // 將所有的表設(shè)回初始狀態(tài)。
 for(i = 0; i < MyClass->ComponentCount; i++)
 {
   try
   {
     if((Table = dynamic_cast(MyClass->Components)) != NULL)
/tTable->Active = active;
   }
   catch(...) {};
 }
 delete []active;
}


改變PageControl的標(biāo)簽
void __fastcall TfmMainForm::Cancel1Click(TObject *Sender)
{
 int i;
 switch (PageControl1->ActivePage->Tag))
 {
   case 1:
     for (i=0; i < ComponentCount; i++)
     {
/tif (dynamic_cast(Components))
/t  dynamic_cast(Components)->Enabled = false;
     }
     Data->tbDetail->Cancel();
     break;
   case 2:
     for (i=0; i < ComponentCount; i++)
     {
/tif (dynamic_cast(Components))
/t  dynamic_cast(Components)->Enabled = false;
     }
     Data->tbDetail->Cancel();
     break;
   case 3:
     for (i=0; i < ComponentCount; i++)
     {
/tif (dynamic_cast(Components))
/t  dynamic_cast(Components)->Text = "";
     }
   default:
     break;
 }
}


向Query傳遞參數(shù)
// 直接從表向Query傳遞參數(shù)的一種方法
TQuery *Query = new TQuery(this);
Query->DatabaseName = "dbServer";
Query->SQL->Clear();
Query->SQL->Add("DELETE FROM 'Events.DB' WHERE (TicketNo = " + Data->tbProblem->FieldByName("TicketNo")->AsString + ")" );
Query->ExecSQL();
Query->Close();
delete Query;


日期屬性
TMaskEdit *meOpen;
TLabel *lbCount1;
TDateTime Date2;
void __fastcall TfmMainForm::CountOpen(TObject *Sender)
{
 switch(dynamic_cast<TComponent&>(*Sender).Tag)
 {
   case 1:
     count1 = StrToInt(lbCount1->Caption);
     count1 += 1;
     Date2 = Now() + count1;
     meOpen->Text = Date2.DateString();
     lbCount1->Caption = IntToStr(count1);
     break;
  case 2:
    count1 = StrToInt(lbCount1->Caption);
    count1 -= 1;
    Date2 = Now() + count1;
    meOpen->Text = Date2.DateString();
    lbCount1->Caption = IntToStr(count1);
    break;
 }
}


繪制狀態(tài)條
void __fastcall TForm1::StatusBar1DrawPanel(TStatusBar *StatusBar, TStatusPanel *Panel, const TRect &Rect)
{
 TCanvas& c = *StatusBar->Canvas;
 switch (Panel->Index)
 {
   case 0 :
   {
     StatusBar1->Panels->Items[0]->Text = "Hello C++";
     c.Brush->Style = bsClear;
     TRect temp = Rect;
     temp.Top += 1;
     temp.Left += 1;
     c.Font->Color = clWhite;
     DrawText(c.Handle,Panel->Text.c_str(),-1,(RECT*)&temp,DT_SINGLELINE|DT_CENTER);
     c.Font->Color = clBlack;
     DrawText(c.Handle,Panel->Text.c_str(),-1,(RECT*)&Rect,DT_SINGLELINE|DT_CENTER);
     break;
   }
   case 1:
   {
     c.Brush->Color = clYellow;
     c.FillRect(Rect);
     c.Font->Color = clRed;
     DrawText(c.Handle,"clYellow Color", -1, (RECT*)&Rect, DT_SINGLELINE | DT_CENTER);
     break;
   }
   case 2:
   {
     Graphics::TBitmap* bm = new Graphics::TBitmap;
     bm->Handle = LoadBitmap(NULL, MAKEINTRESOURCE(32760));
     c.Draw(Rect.Left, Rect.Top, bm);
     delete bm;
     break;
   }
 }
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 海安县| 蒙山县| 东辽县| 黔西县| 岳阳市| 威海市| 美姑县| 黄梅县| 弥勒县| 克拉玛依市| 吴川市| 比如县| 石家庄市| 兴仁县| 徐闻县| 睢宁县| 将乐县| 西林县| 四川省| 宜昌市| 金堂县| 平和县| 五大连池市| 西乡县| 汝城县| 皋兰县| 宿迁市| 上林县| 买车| 霍林郭勒市| 盐亭县| 广河县| 沁源县| 辽源市| 肃宁县| 湟中县| 新余市| 江油市| 宝兴县| 自治县| 霞浦县|