 |
|
C# WinApp จะใช้ DrawString ยังไงให้ตัวหนังสือ ขยายออก แล้วก็ชิดขวา ด้วยครับ |
|
 |
|
|
 |
 |
|
ทำไมมันหายไปส่วนหนึ่งกันรึครับ

เหมือนเลขท้ายมันหายไป ครับ
Code (C#)
public static string SpacedString(string myOldString)
{
//ref
//https://stackoverflow.com/questions/2969143/c-sharp-drawstring-letter-spacing
System.Text.StringBuilder newStringBuilder = new System.Text.StringBuilder("");
foreach (char c in myOldString.ToCharArray())
{
newStringBuilder.Append(c.ToString() + " ");
}
string MyNewString = "";
if (newStringBuilder.Length > 0)
{
// remember to trim off the last inserted space
MyNewString = newStringBuilder.ToString().Substring(0, newStringBuilder.Length - 1).Trim();
//MyNewString = newStringBuilder.ToString();
}
// no else needed if the StringBuilder's length is <= 0... The resultant string would just be "", which is what it was intitialized to when declared.
return MyNewString;
}
Code (C#)
public static void DrawNumPositive(this Graphics e, Font fontDetail, int min, int max, int x, int y, string opr, int CountNum= 6)
{
var num = new Classed.Exten.RandomNumber(min, max);
string _a = TextStringExtension.SpacedString(num.MinValue.ToString());
string _b = TextStringExtension.SpacedString(num.MaxValue.ToString());
System.Windows.Forms.MessageBox.Show(num.MinValue + "\n" + num.MaxValue + "\n" + _a + "\n" + _b);
int w = Convert.ToInt32(e.MeasureString("0 ", fontDetail).Width);
int h = Convert.ToInt32(e.MeasureString("0 ", fontDetail).Height);
//https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-align-drawn-text?view=netframeworkdesktop-4.8
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
// https://stackoverflow.com/questions/11451001/why-isnt-my-text-right-aligned-when-i-custom-draw-my-strings
//https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-align-drawn-text?view=netframeworkdesktop-4.8&redirectedfrom=MSDN
StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Far} ;
e.DrawString(_b, fontDetail, new SolidBrush(Color.Black), new Rectangle(x, y, w*CountNum, h), stringFormat);
e.DrawString(_a, fontDetail, new SolidBrush(Color.Black), new Rectangle(x, y+h+10, w * CountNum, h), stringFormat);
e.DrawString(opr, fontDetail, new SolidBrush(Color.Black), x + (CountNum ) * w, y + 10 + h / 2);
e.DrawLine(new Pen(Color.Black, 3), x, y + h * 3 + 15, x + (CountNum) * w, y + h * 3 + 15);
e.DrawLine(new Pen(Color.Black, 3), x, y + h * 3 + 20, x + (CountNum ) * w, y + h * 3 + 20);
}
|
 |
 |
 |
 |
Date :
2022-08-19 17:19:20 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มข้อสงสัย ครับ
คือสุ่มค่าตั้งแต่ 1 ถึง 1000000 แต่ส่วนใหญ่เหมือนจะได้แค่ค่าสูง ค่าต่ำไม่ค่อยได้ เพราะอะไร ครบั
Code (C#)
Task.Factory.StartNew(() =>
{
// RandomNumberGenerator.GetInt32(1, 10000)
// random.Next(1,1000000)
int c = 0;
for (int x = 1; x <= 1000; x++)
{
richTextBox1.Invoke(new Action(() => richTextBox1.Text +="/" + RandomNumberGenerator.GetInt32(1, 1000000)));
if (c >= 15)
{
richTextBox1.Invoke(new Action(() => richTextBox1.Text += "\n"));
c= 0;
}
c++;
}
});

มันเป็นทั้ง RandomNumberGenerator.GetInt32 และ random.Next
|
 |
 |
 |
 |
Date :
2022-08-19 21:40:48 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|