質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

Q&A

解決済

1回答

2361閲覧

autoscrollを使いつつ固定座標に表示したい

izmktr

総合スコア2856

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

Windows Forms

Windows Forms(WinForms)はMicrosoft .NET フレームワークに含まれる視覚的なアプリケーションのプログラミングインターフェイス(API)です。WinFormsは管理されているコードの既存のWindowsのAPIをラップすることで元のMicrosoft Windowsのインターフェイスのエレメントにアクセスすることができます。

0グッド

0クリップ

投稿2019/04/02 02:47

編集2019/04/02 04:48

イメージ説明
こんな画像を表示するアプリがあります
左上の画像サイズ情報はスクロールバーを動かしても常に左上に表示されます

このとき、スクロールバーを動かすと、
左上に表示されてサイズ情報が一緒に動いてしまいます
Form1_ScrollにInvalidateを呼び出せば一応は直るものの、
一瞬だけ一緒に動いてしまう描画が入ってしまうのは止められません

これを抑制したいんですが、なにかいい方法はありますか

csharp

1 public partial class Form1 : Form 2 { 3 Image image; 4 5 public Form1() 6 { 7 this.AutoSize = true; 8 this.DoubleBuffered = true; 9 10 InitializeComponent(); 11 } 12 13 private void Form1_Load(object sender, EventArgs e) 14 { 15 image = new Bitmap("image.jpg"); 16 17 AutoScrollMinSize = image.Size; 18 } 19 20 private void Form1_Paint(object sender, PaintEventArgs e) 21 { 22 var g = e.Graphics; 23 g.DrawImage(image, AutoScrollPosition); 24 25 g.FillRectangle(Brushes.White, new Rectangle(0, 0, 160, 16)); 26 g.DrawString($"{image.Width}x{image.Height}", Font, Brushes.Black, new Point(0, 0)); 27 } 28 29 private void Form1_Scroll(object sender, ScrollEventArgs e) 30 { 31 Invalidate(); 32 } 33 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Form1 に自力で画像を描画するのではなく、LabelPanelPictureBox を適切に配置して利用したほうがよさそうに思えます。
言葉で大まかに説明すると、

  1. Form1Label を配置する。ここに画像のサイズ情報を表示する。
  2. Form1Panel を配置する。このPanelは最背面に表示する。また、AutoScroll=true,Dock=Fill とする。
  3. 前手順で配置した Panel の上に PictureBox を配置する。このPictureBoxに画像を表示する。また、SizeMode=AutoSize とする。

となります。
ソースはこんな感じになりました。

csharp

1// Form1.cs 2 public partial class Form1 : Form 3 { 4 public Form1() 5 { 6 InitializeComponent(); 7 } 8 9 private void Form1_Load(object sender, EventArgs e) 10 { 11 pictureBox1.Image = Image.FromFile("image.jpg"); 12 label1.Text = $"{pictureBox1.Image.Width}x{pictureBox1.Image.Height}"; 13 } 14 }

csharp

1// Form1.Designer.cs 2 partial class Form1 3 { 4 /// <summary> 5 /// Required designer variable. 6 /// </summary> 7 private System.ComponentModel.IContainer components = null; 8 9 /// <summary> 10 /// Clean up any resources being used. 11 /// </summary> 12 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 13 protected override void Dispose(bool disposing) 14 { 15 if (disposing && (components != null)) 16 { 17 components.Dispose(); 18 } 19 base.Dispose(disposing); 20 } 21 22 #region Windows Form Designer generated code 23 24 /// <summary> 25 /// Required method for Designer support - do not modify 26 /// the contents of this method with the code editor. 27 /// </summary> 28 private void InitializeComponent() 29 { 30 this.label1 = new System.Windows.Forms.Label(); 31 this.pictureBox1 = new System.Windows.Forms.PictureBox(); 32 this.panel1 = new System.Windows.Forms.Panel(); 33 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 34 this.panel1.SuspendLayout(); 35 this.SuspendLayout(); 36 // 37 // label1 38 // 39 this.label1.AutoSize = true; 40 this.label1.BackColor = System.Drawing.Color.White; 41 this.label1.Location = new System.Drawing.Point(0, 0); 42 this.label1.Name = "label1"; 43 this.label1.Size = new System.Drawing.Size(35, 12); 44 this.label1.TabIndex = 0; 45 this.label1.Text = "label1"; 46 // 47 // pictureBox1 48 // 49 this.pictureBox1.Location = new System.Drawing.Point(0, 0); 50 this.pictureBox1.Name = "pictureBox1"; 51 this.pictureBox1.Size = new System.Drawing.Size(284, 261); 52 this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; 53 this.pictureBox1.TabIndex = 1; 54 this.pictureBox1.TabStop = false; 55 // 56 // panel1 57 // 58 this.panel1.AutoScroll = true; 59 this.panel1.Controls.Add(this.pictureBox1); 60 this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; 61 this.panel1.Location = new System.Drawing.Point(0, 0); 62 this.panel1.Name = "panel1"; 63 this.panel1.Size = new System.Drawing.Size(284, 261); 64 this.panel1.TabIndex = 2; 65 // 66 // Form1 67 // 68 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 69 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 70 this.ClientSize = new System.Drawing.Size(284, 261); 71 this.Controls.Add(this.label1); 72 this.Controls.Add(this.panel1); 73 this.Name = "Form1"; 74 this.Text = "Form1"; 75 this.Load += new System.EventHandler(this.Form1_Load); 76 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 77 this.panel1.ResumeLayout(false); 78 this.panel1.PerformLayout(); 79 this.ResumeLayout(false); 80 this.PerformLayout(); 81 82 } 83 84 #endregion 85 86 private System.Windows.Forms.Label label1; 87 private System.Windows.Forms.PictureBox pictureBox1; 88 private System.Windows.Forms.Panel panel1; 89 }

投稿2019/04/02 05:01

alg

総合スコア2019

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

izmktr

2019/04/02 08:18

返答ありがとうございます ただ、実際にやりたいのはエクセルの行と列のようなものもつけたいです (上なら左右スクロールに同期して表示内容が変わるが、上下スクロールには固定位置を保つ) パネルを貼り付ける場合だと、結局OnScrollに同期処理を書くので、一瞬遅れて動くというのを回避できそうにないです
alg

2019/04/02 09:01

それでしたら「スクロールにより動かしたいものはPanelの上にのせる&動かしたくないものはFormの上にのせる」とするとよいかもしれません。
izmktr

2019/04/02 10:11

ありがとうございます、一旦その方針でやってみたいと思います
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問