1

私はCシャーププログラミングに非常に慣れていません。下の添付画像のようなデザインになっています。

私のコンセプトは、「音量を転送」テキストボックスに音量を設定してから(たとえば100)、「設定」ボタンを押す必要があるというものです。画像ボックスのスケールを自動的に設定し、正常に動作しています。

「再生成」ボタンをクリックすると、画像ボックスを色で塗りつぶしたいと思います。画像ボックスに入力される色の割合は、テキストボックス内の色または液体に関する数値である必要があります。

たとえば、GasPhase=5に設定した場合; 炭化水素液=5; 水=5; 油性泥=5; 水ベースの泥=5; 識別されない=75。

次に、画像は「識別されていない」色で75%で塗りつぶされ、5%で気相色で塗りつぶされる必要があります。

以下に示すようなコードを書きました。

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 test
  {
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void txtTransferVolume_TextChanged(object sender, EventArgs e)
    {

    }

    private void txtTransferSet_Click(object sender, EventArgs e)
    {
        string value = txtTransferVolume.Text;
        double midvalue = Convert.ToDouble(value);
        lblTransferBottleMax.Text = value;
        lblTransferBottleMid.Text = (midvalue / 2).ToString();

    }

    private void chkTransferManual_CheckedChanged(object sender, EventArgs e)
    {


    }

    private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
    {

    }

   }
 }

好きなように記入する方法を教えてください。

4

1 に答える 1

1

これは、画像ボックスコントロールに直接描画するか、メモリにビットマップを作成して画像ボックスに表示することで、非常に簡単に実現できます。

例:

private void DrawPercentages(int[] percentages, Color[] colorsToUse)
{
   // Create a Graphics object to draw on the picturebox
   Graphics G = pictureBox1.CreateGraphics();

   // Calculate the number of pixels per 1 percent
   float pixelsPerPercent = pictureBox1.Height / 100f;

   // Keep track of the height at which to start drawing (starting from the bottom going up)
   int drawHeight = pictureBox1.Height;

   // Loop through all percentages and draw a rectangle for each
   for (int i = 0; i < percentages.Length; i++)
   {
       // Create a brush with the current color
       SolidBrush brush = new SolidBrush(colorsToUse[i]);
       // Update the height at which the next rectangle is drawn.
       drawHeight -= (int)(pixelsPerPercent * percentages[i]);
       // Draw a filled rectangle
       G.FillRectangle(brush, 0, drawHeight, pictureBox1.Width, pixelsPerPercent * percentages[i]);
    }    
}

もちろん、両方の配列が同じ長さであることなどを確認する必要があります。これを行う方法の基本的な考え方を説明したいと思います。

データを配列で取得して関数に渡す方法の概念は次のとおりです。値ごとに異なるテキストボックスを使用しているため、それらを繰り返すのは難しいので、今は、現在の6つの値を使用してそれを行う方法を示しています。

private void btnTransferBottleRegenerate_Click(object sender, EventArgs e)
{
  int[] percentages = new int[6];
  percentages[0] = int.Parse(txtTransferNotIdentified.Text);
  percentages[1] = int.Parse(txtTransferWater.Text);
  // And so on for every textbox

  Color[] colors = new Color[6];
  colors[0] = Color.Red;
  colors[1] = Color.Yellow;
  // And so on for every color

  // Finally, call the method in my example above
  DrawPercentages(percentages, colors);
} 

パーセンテージの合計が常に100になるとは限らない場合は、合計を指定する3番目のパラメーターを使用して100f、メソッドで値をこの値に変更できますDrawPercentages

于 2013-02-07T12:36:38.123 に答える