Saving window size and state into the registry. |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
Saving window size and state into the registry. --------------------------------------------------------------------------------
http://www.hobbypages.net/snip18.html Don't you hate it when you make a few adjustments to a window size and put it where you want it, only to start the program again late and find it's right back where the author wanted it. Well here's what I do to make sure my apps stay where you left them. In the main form's header include these lines: #include
void __fastcall TForm1::FormShow(TObject *Sender) { AppReg = new TRegistry; AppReg->RootKey = HKEY_LOCAL_MACHINE; if (AppReg->OpenKey("SOFTWARE\\Hobby Pages\\Snip of the Week\\1.0", false)==true) { try { sState = AppReg->ReadString("State"); if (sState == wsMaximized) { Form1->WindowState = wsMaximized; } else { iLeft = AppReg->ReadInteger("Left"); iTop = AppReg->ReadInteger("Top"); iHeight = AppReg->ReadInteger("Height"); iWidth = AppReg->ReadInteger("Width"); Form1->Left = iLeft; Form1->Top = iTop; Form1->Height = iHeight; Form1->Width = iWidth; } isSound = AppReg->ReadInteger("Sound"); isFull = AppReg->ReadInteger("ShowFull"); CheckBox1->Checked = isFull; CheckBox2->Checked = isSound; } catch(...) { } } delete AppReg; //Do other opening stuff }Notice that the function OpenKey( ) is wrapped in an if () statement. That way if the program is run for the first time, or the entry was removed from the registry, the program will not fail there. It will simply start in the designed location and size. A compound if() statement, as you see. The second if bypasses the top, width, left, height reads if the window was closed while maximized. Windows then looks at the design size when the normal window button is pushed. Now to write the settings, we'll do it in the forms OnClose event, like this: AppReg = new TRegistry; AppReg->RootKey = HKEY_LOCAL_MACHINE; AppReg->OpenKey("SOFTWARE\\Hobby Pages\\Snip of the Week\\1.0", true); AppReg->WriteInteger("Left", Form1->Left); AppReg->WriteInteger("Top", Form1->Top); AppReg->WriteInteger("Height", Form1->Height); AppReg->WriteInteger("Width", Form1->Width); AppReg->WriteInteger("Sound", CheckBox2->Checked); AppReg->WriteInteger("ShowFull", CheckBox1->Checked); AppReg->WriteString("State", Form1->WindowState); delete AppReg;Besideds the size and state of the window at closing, I also saved the settings for a couple of check boxes as well. The users will appreciate the effort. 網路志工聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2003/03/31 14:42:05 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |