i am creating desktop applicaton so when i am try to resize window of winform then all controls are resize at runtime. i am also try C# and VB.net code in project but it does not work...
What I have tried:
#include <string>
#include <unordered_map>
#include <stdexcept>
#include <any>
#include "floating_point_to_integer.h"
namespace Rect { class Resizer; }
//Main_Form
namespace Rect
{
class Form1 : public System::Windows::Forms::Form
{
private:
Resizer *rs = new Resizer();
public:
virtual ~Form1()
{
delete rs;
}
Form1()
{
InitializeComponent();
}
private:
void Form1_Resize(std::any sender, EventArgs *e)
{
rs->ResizeAllControls(this);
}
void Form1_Load(std::any sender, EventArgs *e)
{
rs->FindAllControls(this);
}
};
}
//class Resizer
namespace Rect
{
class Resizer
{
private:
class ControlInfo
{
public:
std::wstring name;
std::wstring parentName;
double leftOffsetPercent = 0;
double topOffsetPercent = 0;
double heightPercent = 0;
int originalHeight = 0;
int originalWidth = 0;
double widthPercent = 0;
float originalFontSize = 0;
};
private:
std::unordered_map<std::wstring, controlinfo=""> ctrlDict = std::unordered_map<std::wstring, controlinfo="">();
public:
void FindAllControls(System::Windows::Forms::Control *thisCtrl)
{
// -- If the current control has a parent, store all original relative position
// -- and size information in the dictionary.
// -- Recursively call FindAllControls for each control contained in the
// -- current Control
for (auto ctl : *thisCtrl->Controls)
{
try
{
if (ctl->Parent != nullptr)
{
auto parentHeight = ctl->Parent->Height;
auto parentWidth = ctl->Parent->Width;
auto c = ControlInfo();
c.name = ctl->Name;
c.parentName = ctl->Parent->Name;
c.topOffsetPercent = static_cast<double>(ctl->Top) / static_cast<double>(parentHeight);
c.leftOffsetPercent = static_cast<double>(ctl->Left) / static_cast<double>(parentWidth);
c.heightPercent = static_cast<double>(ctl->Height) / static_cast<double>(parentHeight);
c.widthPercent = static_cast<double>(ctl->Width) / static_cast<double>(parentWidth);
c.originalFontSize = ctl->Font->Size;
c.originalHeight = ctl->Height;
c.originalWidth = ctl->Width;
ctrlDict.emplace(c.name, c);
}
}
catch (const std::runtime_error &ex)
{
Debug::Print(ex.what());
}
if (ctl->Controls->Count > 0)
{
FindAllControls(ctl);
}
} // -- For Each
}
void ResizeAllControls(System::Windows::Forms::Control *thisCtrl)
{
float fontRatioW;
float fontRatioH;
float fontRatio;
System::Drawing::Font *f;
// -- Resize and reposition all controls in the passed control
for (auto ctl : *thisCtrl->Controls)
{
try
{
if (ctl->Parent != nullptr)
{
auto parentHeight = ctl->Parent->Height;
auto parentWidth = ctl->Parent->Width;
auto c = ControlInfo();
bool ret = false;
try
{
// -- Get the current control's info from the control info dictionary
std::unordered_map<std::wstring, controlinfo="">::const_iterator ctrlDict_iterator = ctrlDict.find(ctl.Name);
ret = ctrlDict_iterator != ctrlDict.end();
c = ctrlDict_iterator->second;
// -- If found, adjust the current control based on control relative
// -- size and position information stored in the dictionary
if (ret)
{
// -- Size
ctl->Width = FloatingPointToInteger::ToInt32(parentWidth * c.widthPercent);
ctl->Height = FloatingPointToInteger::ToInt32(parentHeight * c.heightPercent);
// -- Position
ctl->Top = FloatingPointToInteger::ToInt32(parentHeight * c.topOffsetPercent);
ctl->Left = FloatingPointToInteger::ToInt32(parentWidth * c.leftOffsetPercent);
// -- Font
f = ctl->Font;
fontRatioW = ctl->Width / c.originalWidth;
fontRatioH = ctl->Height / c.originalHeight;
fontRatio = (fontRatioW + fontRatioH) / 2; // -- average change in control Height and Width
ctl->Font = new Font(f->FontFamily, c.originalFontSize * fontRatio, f->Style);
}
}
catch (...)
{
}
}
}
catch (const std::runtime_error &ex)
{
}
// -- Recursive call for controls contained in the current control
if (ctl->Controls->Count > 0)
{
ResizeAllControls(ctl);
}
} // -- For Each
}
};
}