![]() |
#2
追梦人zmrghy2022-09-03 18:24
根据自己的思路,修改了代码,自己感觉没问题,不知什么原因不管用,宽、高可以随意调整,不受程序控制。。。。
![]() #pragma endregion private: static int RecordForm1Width; //记录窗口宽度 private: static int RecordForm1Height; //记录窗口高度 //确保pictureBox1为正方形的情况下,窗口允许最大尺寸 private: void SetMaxSize() { int maxwidth = Screen::PrimaryScreen->WorkingArea.Width; int maxheight = Screen::PrimaryScreen->WorkingArea.Height; maxwidth += 16; maxheight += 16; if (maxwidth > ((maxheight - 39) * 3) / 2 + 16) maxwidth = ((maxheight - 39) * 3) / 2 + 16; else if (maxheight > ((maxwidth - 16) * 2) / 3 + 39) maxheight = ((maxwidth - 16) * 2) / 3 + 39; this->MaximumSize = System::Drawing::Size(maxwidth, maxheight); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { SetMaxSize(); //设置窗口最大尺寸 RecordForm1Width = this->Width; RecordForm1Height = this->Height; } private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e) { Control^ control = dynamic_cast<Control^>(sender); //拖动窗口比例缩放,确保pictureBox1 Size 为正方形. int swidth = control->Size.Width; int sheight = control->Size.Height; //if (sheight > 28) //sheight > 28 窗口不是最小化。 //{ // int maxwidth = Screen::PrimaryScreen->WorkingArea.Width; // int maxheight = Screen::PrimaryScreen->WorkingArea.Height; // if (swidth > maxwidth) // swidth = maxwidth; // sheight = ((swidth - 16) * 2) / 3 + 39; // if (sheight > maxheight) // { // sheight = maxheight; // swidth = ((sheight - 39) * 3) / 2 + 16; // } // control->Size = System::Drawing::Size(swidth, sheight); //} if (sheight != this->MaximumSize.Height || swidth != this->MaximumSize.Width) //窗口不是最大化 { if (RecordForm1Width == swidth) //窗口新宽度等于记录中的宽度,说明鼠标在拖动窗口高度 { swidth = ((sheight - 39) * 3) / 2 + 16; //计算出对应的窗口新宽度, control->Size = System::Drawing::Size(swidth, sheight); //调整窗口尺寸。。。 RecordForm1Width = swidth; //记录新的窗口尺寸 RecordForm1Height = sheight; } else if (RecordForm1Height == sheight) //窗口新高度等于记录中的高度,说明鼠标在拖动窗口宽度 { sheight = ((swidth - 16) * 2) / 3 + 39; //计算出对应的窗口新高度, control->Size = System::Drawing::Size(swidth, sheight); //调整窗口尺寸。。。 RecordForm1Width = swidth; //记录新的窗口尺寸 RecordForm1Height = sheight; } } } |
只有本站会员才能查看附件,请 登录
窗体Form1
Form1工作区放置一个一行俩列的tableLayoutPanel1。
第1列宽度占66.66%, 第2列宽度占33.33%。
第1列中放置pictureBox1,第2列中放置pictureBox2。
初始状态pictureBox1,宽度等于高度,为正方形。
如何,确保pictureBox1为正方形的情况下改变窗口尺寸。。。
Form1_Resize()函数中,只做到了,水平拖动宽度时确保pictureBox1为正方形比例缩放。
上下拖动高度时,尺寸不变,没相应。。。
如何,确保pictureBox1为正方形的情况下拖动高度,也发生比例缩放。

#pragma once
namespace Zoom {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::PictureBox^ pictureBox2;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
this->tableLayoutPanel1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->BeginInit();
this->SuspendLayout();
//
// tableLayoutPanel1
//
this->tableLayoutPanel1->ColumnCount = 2;
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
66.66666F)));
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
33.33333F)));
this->tableLayoutPanel1->Controls->Add(this->pictureBox1, 0, 0);
this->tableLayoutPanel1->Controls->Add(this->pictureBox2, 1, 0);
this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
this->tableLayoutPanel1->RowCount = 1;
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 100)));
this->tableLayoutPanel1->Size = System::Drawing::Size(784, 522);
this->tableLayoutPanel1->TabIndex = 0;
//
// pictureBox1
//
this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
this->pictureBox1->Location = System::Drawing::Point(3, 3);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(516, 516);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// pictureBox2
//
this->pictureBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->pictureBox2->Dock = System::Windows::Forms::DockStyle::Fill;
this->pictureBox2->Location = System::Drawing::Point(525, 3);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(256, 516);
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(784, 522);
this->Controls->Add(this->tableLayoutPanel1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
this->tableLayoutPanel1->ResumeLayout(false);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
//确保pictureBox1为正方形的情况下,窗口允许最大尺寸
private: void SetMaxSize()
{
int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
if (maxwidth > ((maxheight - 39) * 3) / 2 + 16)
maxwidth = ((maxheight - 39) * 3) / 2 + 16;
else if (maxheight > ((maxwidth - 16) * 2) / 3 + 39)
maxheight = ((maxwidth - 16) * 2) / 3 + 39;
this->MaximumSize = System::Drawing::Size(maxwidth, maxheight);
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
SetMaxSize(); //设置窗口最大尺寸
}
private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e)
{
Control^ control = dynamic_cast<Control^>(sender);
//拖动窗口比例缩放,确保pictureBox1 Size 为正方形.
int swidth = control->Size.Width;
int sheight = control->Size.Height;
if (sheight > 28) //sheight > 28 窗口不是最小化。
{
int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
if (swidth > maxwidth)
swidth = maxwidth;
sheight = ((swidth - 16) * 2) / 3 + 39;
if (sheight > maxheight)
{
sheight = maxheight;
swidth = ((sheight - 39) * 3) / 2 + 16;
}
control->Size = System::Drawing::Size(swidth, sheight);
}
}
};
}
namespace Zoom {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private: System::Windows::Forms::PictureBox^ pictureBox2;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
this->tableLayoutPanel1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->BeginInit();
this->SuspendLayout();
//
// tableLayoutPanel1
//
this->tableLayoutPanel1->ColumnCount = 2;
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
66.66666F)));
this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
33.33333F)));
this->tableLayoutPanel1->Controls->Add(this->pictureBox1, 0, 0);
this->tableLayoutPanel1->Controls->Add(this->pictureBox2, 1, 0);
this->tableLayoutPanel1->Dock = System::Windows::Forms::DockStyle::Fill;
this->tableLayoutPanel1->Location = System::Drawing::Point(0, 0);
this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
this->tableLayoutPanel1->RowCount = 1;
this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 100)));
this->tableLayoutPanel1->Size = System::Drawing::Size(784, 522);
this->tableLayoutPanel1->TabIndex = 0;
//
// pictureBox1
//
this->pictureBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(255)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
this->pictureBox1->Location = System::Drawing::Point(3, 3);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(516, 516);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// pictureBox2
//
this->pictureBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->pictureBox2->Dock = System::Windows::Forms::DockStyle::Fill;
this->pictureBox2->Location = System::Drawing::Point(525, 3);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(256, 516);
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(784, 522);
this->Controls->Add(this->tableLayoutPanel1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->Resize += gcnew System::EventHandler(this, &Form1::Form1_Resize);
this->tableLayoutPanel1->ResumeLayout(false);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox2))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
//确保pictureBox1为正方形的情况下,窗口允许最大尺寸
private: void SetMaxSize()
{
int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
if (maxwidth > ((maxheight - 39) * 3) / 2 + 16)
maxwidth = ((maxheight - 39) * 3) / 2 + 16;
else if (maxheight > ((maxwidth - 16) * 2) / 3 + 39)
maxheight = ((maxwidth - 16) * 2) / 3 + 39;
this->MaximumSize = System::Drawing::Size(maxwidth, maxheight);
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
SetMaxSize(); //设置窗口最大尺寸
}
private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e)
{
Control^ control = dynamic_cast<Control^>(sender);
//拖动窗口比例缩放,确保pictureBox1 Size 为正方形.
int swidth = control->Size.Width;
int sheight = control->Size.Height;
if (sheight > 28) //sheight > 28 窗口不是最小化。
{
int maxwidth = Screen::PrimaryScreen->WorkingArea.Width;
int maxheight = Screen::PrimaryScreen->WorkingArea.Height;
if (swidth > maxwidth)
swidth = maxwidth;
sheight = ((swidth - 16) * 2) / 3 + 39;
if (sheight > maxheight)
{
sheight = maxheight;
swidth = ((sheight - 39) * 3) / 2 + 16;
}
control->Size = System::Drawing::Size(swidth, sheight);
}
}
};
}
[此贴子已经被作者于2022-9-3 00:01编辑过]