Board index » cppbuilder » Problem with Form1's position and size at runtime...
Vladimir Stefanovic
![]() CBuilder Developer |
Problem with Form1's position and size at runtime...2003-12-14 06:15:43 PM cppbuilder77 Hi, This is a simple and useful fragment of code for setting Form1's window position and size at runtime. It could help if you want to allow users to configure (*.INI) how they want to appear the MainForm next time they start the application. // Add this code as global in Unit1.cpp for testing... // (it's easier for this example) enum WindowPositionAndSize { wpHardcoded, // Show Form1 with hardcoded Height and Width but centered on desktop wpLastUsed, // Show Form1 with last used position and size wpMaximized // Show Form1 maximized } MyChoice = wpMaximized; // Change to other values for testing... // Imagine that we got these from INI ... int LastLeft = 50; int LastTop = 50; int LastHeight = 200; int LastWidth = 300; // This is our desired hardcoded Form1's size int HardcodedHeight = 200; int HardcodedWidth = 300; __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { switch ( MyChoice ) { case wpHardcoded: Left = ( Screen->Width - HardcodedWidth ) / 2; Top = ( Screen->Height - HardcodedHeight ) / 2; Width = HardcodedWidth; Height = HardcodedHeight; break; case wpLastUsed: Left = LastLeft; Top = LastTop; Width = LastWidth; Height = LastHeight; break; case wpMaximized: // Do this (same as wpHardcoded) to avoid // ... design time Form1's Size when restored // I think it should restore to wpHardcoded Left = ( Screen->Width - HardcodedWidth ) / 2; Top = ( Screen->Height - HardcodedHeight ) / 2; Width = HardcodedWidth; Height = HardcodedHeight; WindowState = wsMaximized; break; } QUESTION: This code works fine in this clean situation and I followed this approach in my app. But in some of my (complex) projects some strange things happens when I try to Maximize (this can be viewed better on some slower machines). These steps are executed very fast, but I managed to see the hronology: 1) The Form1 realy maximizes for a second 2) I see the client arrea (MDI frame below main menu and toolbar) that has not maximized (stayed I think in design time size).The same is if app is SDI but then you cannot see the frame. 3) Controls on the Form1 stays fixed on their disign time sizes though NONE OF THEM are Aligned to 'alNone'. They are aligned as necessary (alLeft, alTop, alBottom, alClient). 4) The same controls I mentioned that stayed at design time suddenly aligns to Maximized state (the state that I wanted to be) 5) Then, Form1 returns to its designed size (Height, Weight), but with Left = Top = 0; 6) The gadged in the Form1's Caption Bar has left in the 'Restore' state. The Form1 obviously think it's maximized. That means my Form1 is restored and I can only Restore it through the gadget :) I cannot even resize it. What I did wrong? I suspected on FormResize() that takes care of some metrics of Panels which can be moved by Splitter, and some recursions that can be so produced. I added some code like this: __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { OnResize = NULL; switch ( MyChoice ) { // ... } OnResize = FormResize; } ... but unfortunately the problem was not solved. Strange thing is also that if I add this piece of code Form1 realy stayes Maximized but it's behaves ugly when you want to Restore it (it restores for a milimetres). case wpMaximized: // This 4 lines are added... Left = 0; Top = 0; Width = Screen->Width; Height = Screen->Height; Vladimir. |