2013-02-07 3 views
1

저는 C 날카로운 프로그래밍에 매우 익숙합니다. 아래 그림과 같이 디자인되어 있습니다.일부 백분율로 채워진 그림 상자

내 개념은 "볼륨 전송"텍스트 상자 (예 : 100)에 볼륨을 설정하고 "설정"버튼을 눌러야한다는 것입니다. 그것은 자동으로 그림 상자의 크기를 설정, 잘 작동합니다.

이제 "재생성"버튼을 클릭하면 색상으로 그림 상자를 채 웁니다. 그림 상자에 채워지는 색의 백분율은 색이나 액체에 관한 텍스트 상자의 숫자 여야합니다.

예 : GasPhase = 5로 설정하면; 탄화수소 액체 = 5; 물 = 5; 유성 진흙 = 5; 수성 진흙 = 5; 식별되지 않음 = 75.

그림은 식별되지 않은 색이 75 %이고 GasPhase 색이 5 % e.t.c입니다.

아래와 같이 코드를 작성했습니다.

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) 
    { 

    } 

    } 
} 

내가 원하는대로 작성하는 방법을 알려주세요.

답변

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 가지 값을 사용하여 그것을 수행하는 방법을 보여 드리겠습니다. 당신의 비율은 항상 100 합계하지 않는 경우

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); 
} 

, 당신은 합을 지정하는 세 번째 매개 변수를 사용하고 DrawPercentages 방법이 값으로 값 100f을 변경할 수 있습니다.

+0

저는 예제로 그리기 개념을 소개합니다. 그러나 마음껏 해석 할 수는 있지만 데이터를 수집하고 배열로 전달하는 것이 객관적 일 수는 없습니다. Graphics 객체를 사용하여 각 사각형을 자체 선에 그릴 수도 있지만 항상 확장 성을 유지하려고합니다. 데이터의 모든 추가 항목은 데이터의 각 항목에 대한 줄을 쓰는 데 추가 코드가 필요할 수도있는 내 메서드에서는 문제가되지 않습니다. –

+0

네, 맞아요, 나는 또한 (텍스트 상자와 레이블)에서 데이터를 수집하고 배열을 통해 전달하는 방법을 생각하고 있습니다. – PRV

+0

입력 된 값을 백분율 배열로 전달하는 방법을 알고 계십니까? – PRV