中央のMDIフォームの背景画像を正しく処理する方法
-
07-07-2019 - |
質問
背景画像が中央に配置されたMDIフォームがあります。
ユーザーがフォームのサイズまたは状態を変更するたびに、画像はまったく更新されません。これは古い場所に残り(中央に配置されなくなり)、フォームを小さくしすぎると失われます。
この状況を正しく処理するにはどうすればよいですか?
本当にthis.Refresh()"を呼び出す必要がありますかフォームのサイズと状態に関連するすべてのイベントハンドラーで
アプリケーションは、Windows.Formsを使用した.net 3.5SP1 C#で実現されます。
解決
残念ながら、これを行うための非常に迅速な方法はないようですが、以下は私の解決策であり、少なくとも偶然の一致に依存していないようです。
mdiコンストラクターで、サイズ変更を処理します。
this.ResizeEnd += delegate { this.Refresh(); };
そして、このオーバーライドにより、最大化/復元イベントを処理します
protected override void WndProc(ref Message m)
{
if (m.Msg == Win32.WM_SYSCOMMAND)
{
int test = m.WParam.ToInt32() & 0xFFF0;
switch (test)
{
case Win32.SC_MAXIMIZE:
case Win32.SC_RESTORE:
this.Invalidate(); // used to keep background image centered
break;
}
}
base.WndProc(ref m);
}
定数値は次のように定義されます:
public const int WM_SYSCOMMAND = 0x0112;
//wparam for WM_SYSCOMMAND should be one of these after masking with 0xFFF0:
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;
他のヒント
すべてを実行することも、MDIの resize
イベントに me.refresh
を挿入することもできます。
MDIフォームのResizeイベントでPositionContainersToParentMiddleメソッドを呼び出します。 私はそれをテストしていませんが、動作するはずです。サイズ変更のたびに画像の場所の変更を停止するには、サイズ変更イベントに条件を設定する必要がある場合があります。
private void YourMDI_Resize(object sender, EventArgs e)
{
PositionContainersToParentMiddle();
}
private void PositionContainersToParentMiddle()
{
int iInitX = (ParentOfImage.Size.Width - YourImage.Size.Width) / 2;
int iInitY = ( ParentOfImage.Location.Y + YourImage.Size.Height ) / 2;
YourImage.Location = new Point( iInitX, iInitY ) ;
}
Private Sub YourMDIFormName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Resize
Me.BackgroundImage = My.Resources.YourBackgroundImageName
Me.Refresh()
End Sub
private void Change_BackgroundImage(string _path)
{
string imagepath = _path;
System.IO.FileStream fs;
// MDI Form image background layout change her`enter code here`e
//(Remember control imagebakground layout take default form background layount )
this.BackgroundImageLayout = ImageLayout.Center;
// Checking File exists if yes go --->
if (System.IO.File.Exists(imagepath))
{
// Read Image file
fs = System.IO.File.OpenRead(imagepath);
fs.Position = 0;
// Change MDI From back ground picture
foreach (Control ctl in this.Controls)
{
if (ctl is MdiClient)
{
//ctl.BackColor = Color.AntiqueWhite;
ctl.BackColor = Color.FromArgb(31, 26, 23);
ctl.BackgroundImage = System.Drawing.Image.FromStream(fs);
break;
}
}
}
}
所属していません StackOverflow