.Net Compactでのダブルバッファリング

このQ&Aのポイント
  • .Net Compact Frameworkを利用したC#プログラミングで画面を描画しているが、ちらつきが解消されない。
  • Visual Studio 2005とC#を使用し、.Net Compact Framework 2.0でダブルバッファリングを試みているが、うまくいっていない。
  • 質問者はForm1クラスでオフスクリーンイメージとオフスクリーングラフィックスを使用し、画面を描画しているが、ちらつきが改善されない。
回答を見る
  • ベストアンサー

.Net Compact でのダブルバッファリング

.Net Comapct Frameworkを利用してのC#プログラミングで、 ダブルバッファリングを利用して画面を描画しようとしていますが、 ちらつきが解消されません。 どこがおかしいかご指摘いただけると幸いです。 【環境】 Visual Studio 2005 C# .Net Compact Framework 2.0 【コード(抜粋)】 public partial class Form1 : Form { private Bitmap m_offScr; // オフスクリーンイメージ private Graphics m_offG; // オフスクリーングラフィックス private System.Windows.Forms.Timer timer1; public Form1() { this.timer1.Enabled = true; this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); ~略~ this.m_offScr = new Bitmap(400, 200); this.m_offG = Graphics.FromImage(this.m_offScr); } private void Form1_Paint(object sender, PaintEventArgs e) { m_offG.FillRectangle(new SolidBrush(Color.Red), 10, 10, 100, 100); e.Graphics.DrawImage(this.m_offScr, 0, 0); } // イベントハンドラ private void timer1_Tick(object sender, EventArgs e) { // 秒の更新 this.Refresh(); } } よろしくお願いいたします。

  • darum
  • お礼率38% (7/18)

質問者が選んだベストアンサー

  • ベストアンサー
回答No.1

 こんにちは。  timer1_Tickイベント内のRefresh()メソッドでチラついているのでは。  Refresh()メソッドを呼ぶのではなく、フォームに向かってストレートに絵を描いて見ては如何でしょうか。 // イベントハンドラ private void timer1_Tick(object sender, EventArgs e) { // 秒の更新 m_offG.FillRectangle(new SolidBrush(Color.Red), 10, 10, 100, 100); Graphics gfx = this.CreateGraphics(); gfx.DrawImage(this.m_offScr, 0, 0); gfx.Dispose(); }

関連するQ&A

  • C# 簡単なシューティング 自機移動について

    かなりの初心者で困っています。 簡単なシューティングを作ろうとおもっています。 使用ソフトはVisualC#2005です。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //自機(右移動) private void button3_Click(object sender, EventArgs e) { if (timer1.Enabled == false) { timer2.Enabled = false; timer1.Enabled = true; } else { timer1.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left + 2; } //自機(左) private void button4_Click(object sender, EventArgs e) { if (timer2.Enabled == false) { timer1.Enabled = false; timer2.Enabled = true; } else { timer2.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer2_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left - 2; } //自機(上) private void button1_Click(object sender, EventArgs e) { if (timer3.Enabled == false) { timer4.Enabled = false; timer3.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer3.Enabled = false; } } private void timer3_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top - 2; } //自機(下) private void button2_Click(object sender, EventArgs e) { if (timer4.Enabled == false) { timer3.Enabled = false; timer4.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer4.Enabled = false; } } private void timer4_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top + 2; } } } 自機は画像(PictureBox)でボタンによって上下左右に移動します。 FormのSizeは800,630です。 自機の移動がボタンなのでキー入力によって操作できるようにしたいのですが、 それと、自機の移動範囲を画面からでないようにしたいです。 あまりC#を理解できてない上でつくったのでおかしな点が多々あると思います。 教えていただける方がいると助かります。

  • C# フォーム追加後、旧フォームを閉じたい

    前略 ・C#で教えてください。 ・Form1 からshowDiag()メソッドでForm2をつくります。Form2が表示されたら非アクティブとなったForm1を閉じたいのですが どのようにしたらよいのかおしえてください。  下記は、Close() メソッドで Form2側から閉じようとしたプログラムですがFrom1を閉じることができません。 よろしくお願いします。 //----------------------------------------------- namespace formClose { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); form2.ShowDialog(); } }   public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { Form1 form1 = new Form1(); form1.Close(); //Form1が閉じない!! } private void button1_Click(object sender, EventArgs e) { this.Close(); //Form2を閉じる }    } } 以上

  • listBoxとTimerについて C#

    listBoxとTimerについて C# ヤフーニュースのURLをlistboxへ入れます。 そしてlistBox1の中身を画面表示させたら、次にlistBox2の中身も表示したいと思っています。 最初listBoxが1つだったときはうまく行っていたのですが、listBoxを増やすとうまく行かなくなりました。 この状態だと、listBox2の中身だけ表示させて終わってしまいます。 Timerの使い方が怪しいと思うのですが、どうでしょうか? 些細なことでも何でもいいのでご意見頂ければ助かります。 -----以下コード抜粋------ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int urlindex = 0; private void Form1_Load(object sender, EventArgs e) { listBox1.AllowDrop = true; listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter); listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop); listBox2.AllowDrop = true; listBox2.DragEnter += new DragEventHandler(listBox2_DragEnter); listBox2.DragDrop += new DragEventHandler(listBox2_DragDrop); } private void listBox1_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox2_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox1_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox1.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; //ドロップされたデータ(string型)を取得 string itemText = (string)e.Data.GetData(typeof(string)); //ドロップされたデータをリストボックスに追加する target.Items.Add(url); //MessageBox.Show("表示"); } } private void listBox2_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox2.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url); } } private void goButton_Click(object sender, EventArgs e) { urlindex = 0; timer1.Start(); timer2.Start(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); if (listBox1.Items.Count != 0 && urlindex < listBox1.Items.Count) { string url = (string)listBox1.Items[urlindex]; webBrowser1.Navigate(url); urlindex++; } } private void timer2_Tick_1(object sender, EventArgs e) { timer2.Stop(); if (listBox2.Items.Count != 0 && urlindex < listBox2.Items.Count) { string url = (string)listBox2.Items[urlindex]; } } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { timer1.Start(); timer2.Start(); } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { }

  • C# フォームプログラミングで

    public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { //Graphics grfx = CreateGraphics(); } private void Form2_Paint(object sender, PaintEventArgs e) { Form2 form2; form2.Paint += new PaintEventHandler(PaintHandler1); } static void PaintHandler1(object objSender,PaintEventArgs e) { Graphics grfx = e.Graphics; grfx.DrawString("test",Font font,Brushes.Black,0,0);//←ここでエラー } } このように書くと「引数を2個指定できる、メソッド DrawString のオーバーロードはありません」 というエラーがでてフォームに文字を表示できません 最初に「空のプロジェクト」で作れば普通にDrawStringで文字を表示できるのですが 「Windowsフォーム」を選んでからだと表示できないのはなぜでしょうか? 何がいけないのでしょう。

  • C#について・・・

    次のようなブログラムなのですが、ラジオボタンが上手く切り替わりません。 どこを修正すると上手くいくでしょうか? <<文字数オーバーのため省略>> public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); if(isJapanStyle) { label5.Text=string.Format("0時0分0秒"); label6.Text=string.Format("0時0分0秒"); } else { label5.Text=string.Format("00:00:00"); label6.Text=string.Format("00:00:00"); } // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // <<文字数オーバーのため 省略>> static void Main() { Application.Run(new Form1()); } protected void button1_Click(object sender, System.EventArgs e) { if(isJapanStyle) { label5.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label5.Text=DateTime.Now.ToString("T"); } recTime=dt; timer2.Stop(); timer3.Start(); } private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, System.EventArgs e) { dt=DateTime.Now; if(isJapanStyle) { label4.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label4.Text=DateTime.Now.ToString("T"); } } private void timer3_Tick(object sender, System.EventArgs e) { keika=dt-recTime; if(isJapanStyle) { label6.Text=string.Format("{0}時間{1}分{2}秒",keika.Hours,keika.Minutes,keika.Seconds); } else { label6.Text=string.Format("{0}:{1}:{2}",keika.Hours,keika.Minutes,keika.Seconds); } } private void timer2_Tick(object sender, System.EventArgs e) { timer3.Stop(); } private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=false; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=true; } } }

  • pictureboxの画像を削除するには?

    Visual C# 2008を使用しています。 ボタンを押すとpictureboxに画像が表示されるプログラムを作成しました。 今度は、その表示された画像をクリックし、別のボタンを押すことでその画像が削除できるようにしたいと思っているのですが、うまくプログラムできません。 誰か教えていただけないでしょうか? ソースコードは以下のようになっています。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace algorithm { public partial class Form1 : Form { int space = 10; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void kihonsyori_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\基本処理箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 50; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void groupBox1_Enter(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void hanpuku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\反復箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sentaku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\選択箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sakujyo_Click(object sender, EventArgs e) { pictureBox1.Image = null; space = 0; } } }

  • ダブルバッファリング

    ダブルバッファリングを使ったつもりですが、キー操作するたびに画面がチカチカします。 特に <applet code="test.class" width="1600" height="600"> とした場合、repaint()メソッドを何回も呼んだ時に激しいです。 以下、ソースですが間違っているでしょうか? // ダブルバッファリング private Image imgOff; // オフイメージ private Graphics graOff; // オフグラフィックス public void paint(Graphics g){ if (imgOff == null) { Dimension dim = getSize(); imgOff = createImage( dim.width, dim.height ); graOff = imgOff.getGraphics(); } // ここに何か書く 例) graOff.setColor( new Color(255,255,255) ); graOff.fillRect(0, 0, 1600, 600); // 画面に反映 g.drawImage(imgOff,0,0,this); }

    • ベストアンサー
    • Java
  • C# Formの操作

    Form2 form2 ; // 子 Form Form3 form3 ; // 子 Form private void button1_Click(object sender, System.EventArgs e) { form2 = new Form2() ; this.AddOwnedForm(form2) ; // 親 Form が form2 を所有する form2.Show() ; } private void button2_Click(object sender, System.EventArgs e) { form3 = new Form3() ; this.AddOwnedForm(form3) ; // 親 Form が form3 を所有する form3.Show() ; } 上記のようにボタン一つに対して1つのFormに対する処理をするのではなく,ボタン1つに対して 複数のFormを処理する.つまり 例えば,Form1,2,3とあったとして,Form1のボタンを押してFrom2を開き,そこで何らかの処理を したものをForm1に反映する.または,From3のボタンを押してForm2を開きそこで何らかの処理を 行いForm3に反映する. この場合,Form1とForm3のどちらのFormのボタンが押されたか判断しないといけないと思うのですが,ここの処理がうまくいきません. どちらかのFormのボタンを押してForm2を表示し,どちらかのFormに反映させる所までは出来たのですが,両方の判断材料を入れて,どちらのFormのボタンを押されたか判断する際に,押されなかった方が「null」となりエラーになります. 下記にこの判断部分を掲載いたします.どなたかご教授よろしくお願いいたします. Form1 public int but = 0; public int butt1; public string DB; public string HDB; public Form2 f2 = null; private void mybutton1_Click(object sender, EventArgs e) { butt1 = 1; ・ ・ ・ public int Ye=0; private void button43_Click(object sender, EventArgs e) { butt1 = 0; From2 public partial class Form2 : Form { Form1 f1; Form3 f3; public Form2(Form1 f) { f1 = f; // メイン・フォームへの参照を保存 InitializeComponent(); } public Form2(Form3 f) { f3 = f; // メイン・フォームへの参照を保存 InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { #region int Pin1 = f1.butt1; int Pin3 = f3.butt3; Form3 Form1と基本的には同じです.

  • C# コントロールの移動について

    数年のブランクののち、VB6からの乗り換えで、数日前から、C#を始めたところのまったくの素人ですが、よろしく。 起動時に、フォームは、指定の位置に移動・サイズ変更は反映しているのですが、 ピクチャアボックスの、移動は反映されません。 なぜでしょうか。 お手数ですが、素人にも理解できるように、ご教授ください。 ソース・・・ namespace C_TRY { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Left = 200 ; // X位置 this.Top = 100; // Y位置 this.Width = 640; // 幅 this.Height = 480; // 高さ } private void pictureBox1_Layout(object sender, LayoutEventArgs e) { this.pictureBox1.Location = new Point(1000, 128); } } }

  • C#での画像ファイルをドラッグアンドドロップで描画

    C#で画像ファイルをドラッグアンドドロップで描画させるプログラムを作りたいのですが、うまくできません。 ドラッグしたファイル名を読み取る部分までは動作確認できています。このファイル名の画像ファイルをForm1に描画させる部分でエラーになってしまいます。 どのように修正したらよいのかわからないのでお助けください。 ----- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Drag {   public partial class Form1 : Form   {     public Form1()     {       InitializeComponent();     }     private void Form1_DragEnter(object sender, DragEventArgs e)     {       e.Effect = DragDropEffects.All;     }     private void Form1_DragDrop(object sender, DragEventArgs e)     {       Graphics g = e.Graphics;   // <------ エラーになる。       if (e.Data.GetDataPresent(DataFormats.FileDrop))       {         foreach (string fileName             in (string[])e.Data.GetData(DataFormats.FileDrop))         {           g.DrawImage(new Bitmap(fileName, new PointF(10F, 50F)));           // Console.WriteLine(fileName); // 動作確認         }       }     }   } }

専門家に質問してみよう