dポイントプレゼントキャンペーン実施中!

以下のように、アプリ起動時に、フェードインで現れてフェードアウトで消えていくフォームがあります
その後に実際に使用するフォームを表示したいと思っています。
ただwindows8だと問題ないのですがXPやwindows7で下記コードを実行するとちょうど
Thread.Sleep(4000)という命令以降フェードアウトしていくのですが
この時一瞬画像がブチっとコマ落ちしたような現象がおきるのです。
原因がわからず困っております。

どなたか原因がわかるかたいらっしゃいませんでしょうか?

お願いします!

public class MainClass{
public static void Main(string [] args){
try{
FrontForm frontObj = new FrontForm();
frontObj.StartPosition = FormStartPosition.CenterScreen;
frontObj.SetImage("f04.jpg");
frontObj.FormBorderStyle = FormBorderStyle.None;
Application .EnableVisualStyles();
Application.Run(frontObj);
Application.Run(new Form());
}catch(Exception e){
MessageBox.Show(e.ToString());
}
}
}

public class FrontForm : Form{

//フェードさせるためのタイマーオブジェクト
public System.Windows.Forms.Timer TimerObj ;
//画面にフェードインアウトで表示させる画像用ラベル
public Label labelObj ;
//実際に表示させるための画像パス
public string imagePath;
//フェードインとアウトの境目フラグ
public int fadeFlag;
//コンストラクタ
public FrontForm(){
this.Width = 900;
this.Height = 400;
this.labelObj = new Label();
this.labelObj.Width = this.ClientSize.Width;
this.labelObj.Height = this.ClientSize.Height;
this.Controls.Add(this.labelObj);
this.Opacity = 0.0F;
this.Visible = false;
this.fadeFlag = 0;
this.Load += new EventHandler(this.RenderFade);
}

//フェード用画像をセットする
public void SetImage (string path){
Bitmap bit = new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
Graphics gr = Graphics.FromImage(bit);
this.imagePath = path;
Image imageObj = Image.FromFile(this.imagePath);
gr.DrawImage(imageObj,0,0,this.ClientSize.Width,this.ClientSize.Height);
this.labelObj.BackgroundImage =bit;
}

public void RenderFade(Object sender ,EventArgs e){
//タイマーイベントの作成
this.TimerObj = new System.Windows.Forms.Timer();
this.TimerObj.Tick += new EventHandler(this.RenderMethod);
this.TimerObj.Interval = 30;
this.TimerObj.Start();
}

public void RenderMethod(Object sender ,EventArgs e){
if(this.fadeFlag == 0){
Console.WriteLine(this.Opacity.ToString());
this.Opacity += 0.01F;
if(this.Opacity == 1){
this.fadeFlag = 1;
Thread.Sleep(4000);
}
}
if(this.fadeFlag == 1){
Console.WriteLine("====" + this.Opacity.ToString() + "====");
this.Opacity = this.Opacity - 0.01F;
if(this.Opacity ==0){
this.Close();
}
}
}
}

A 回答 (2件)

うーん、Refreshを入れて変わらないですか。


Sleep(4000);
を外すと、間断なくフェードイン・フェードアウトされますか?
Sleepの前後で、デバッガで実行を止めた場合の画面の変化を見てみるとか。
Sleepのより前で「ブチッ」が起きていたら、そもそも、Sleepが原因ではないのでしょうし。
コマ落ちの原因がわかってから、修正を加えたほうがいいと思います。
それで治らなかったり、症状が変わったりしたら、より悩みが深まります。
    • good
    • 0

System.Windows.Forms.Timerを使っているので、ウィンドウの描画は、


Sleepが行われている間、メッセージが処理されません。
Sleepの4秒間、Formはフリーズ状態になっていると思います。

フェードインで、Opacityを+0.01ずつして行き、1になったら、Sleep(4000);が実行されますが、
この時点では、画面は+0.99の状態で止まります。
そして、4秒経過後、保留が解除されて再描画されると思います。
この時に、Opacityの値がどのように変化するかは不明ですが、
値が規則的に変化していないので、描画が乱れるのではないかと思います。

Windowsタイマを利用するときの注意点
http://www.atmarkit.co.jp/fdotnet/dotnettips/372 …

マルチスレッドのタイマーを使うのが、最良の解決策でしょうが、
ひとまず、Sleep(4000);の前の行に、
this.Refresh();
と書いてみて、変化が見られるかどうか、確認してみて下さい。

Refresh、Update、Invalidateメソッドの違い
http://dobon.net/vb/dotnet/control/refreshupdate …
    • good
    • 0
この回答へのお礼

回答ありがとうございます。
this.Refreshを加えても変化はありませんでした。・・・。

ちょっといまからスレッドタイマーで試みてみます。

お礼日時:2012/11/07 20:29

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!