線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1141
推到 Plurk!
推到 Facebook!

Using the registry to keep some data.

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-03-31 14:36:22 IP:61.218.xxx.xxx 未訂閱

Using the registry to keep some data.

http://www.hobbypages.net/snip15.html My speed dialer needs to remember what names and numbers I've assigned to it. File operations are cumbersome in this setting, and a string-grid or database is too bulky. The registry is just the right place to keep track of those numbers. The dialer uses a dialog form to enter the information. Here, Button1 is the OK button for the dialog. I have to check to see if the user meant cancel or forgot to enter any information. Then its off to write to the registry and the main form's speed-dialer button.

//------snip------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
if (Edit1->Text == "")
        {
        int button;
                button = MessageBox(0, "You pressed OK but entered no name.\n"
                "This will clear the current button assignment.\n"
                " If that's what you wanted press OK, \n if not press Cancel.",
                "Warning", MB_OKCANCEL);
                if (button == IDCANCEL)
                {
                        StartOver();//function to set focus to Edit1
                        return;
                }
        }
int iWhichOne;    /* these are declared in the header
String bName1, bName2 on thru 20
String bNumber1, bNumber2 on thru 20  */    iWhichOne = StrToInt(Edit3->Text);//which button to write on
Registry = new TRegistry;
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\\HobbyPages\\Dialer2", true);
//Set captions and write to registry
switch (iWhichOne)        {
        case 1:
        try        {
                Form1->Button1->Caption = Edit1->Text;
                bName1 = Edit1->Text;
                bNumber1 = Edit2->Text;
                Registry->WriteString("Name1", bName1);
                Registry->WriteString("Number1", bNumber1);
                } catch(...) {}
                break;
        case 2:
        try        {
                Form1->Button2->Caption = Edit1->Text;
                bName2 = Edit1->Text;
                bNumber2 = Edit2->Text;
                Registry->WriteString("Name2", bName2);
                Registry->WriteString("Number2", bNumber2);
                } catch(...) {}
                break;
                // there are 20 statements like this ->20 buttons
        default :
                MessageBox(0, "You must assign a dialer button (1-20)",
                "Message", MB_OK);
                return;
        }
delete Registry; //always delete after a new
Close();
}
//-----snip-----
The dialer's mainform OnShow event loads the button captions from the registry. If there is no assignment, it leaves the button face blank.

void __fastcall TForm1::FormShow(TObject *Sender)
{
Registry = new TRegistry;
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\\HobbyPages\\Dialer2", true);
        try {
        //--write String Objects from Regisrty-----
        bName1 = Registry->ReadString("Name1");
        bNumber1 = Registry->ReadString("Number1");
        Button1->Caption = bName1;            bName2 = Registry->ReadString("Name2");
        bNumber2 = Registry->ReadString("Number2");
        Button2->Caption = bName2;
        //--on thru 20 buttons--
        } catch (...) {}
delete Registry;
}
bNumber1 holds the phone number so that when Button1 is pressed, it's sent to the dialing code segment, and into the modem string. They say not to use INI files, so into the registry we go... 聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
系統時間:2024-11-24 1:28:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!