ที่มาของปัญหาของฉัน
ฉันสร้างแอปพลิเคชันจำลองที่เรียบง่ายมากใน C # โดยใช้ winforms มีเพียง ToolStrip และ TrackBar ภายใน ToolStrip นั้น TrackBar ควรมี BackColor เหมือนกับ ToolStrip แต่ดูเหมือนว่าคุณสมบัติ ToolStrip.BackColor จะไม่ส่งคืนสีพื้นหลังตามจริง
public Form1()
{
//// Here the ToolStrip is created.
//// This code is generated by the designer.
this.InitializeComponent();
//// Here the the TrackBar is created.
//// I think this is impossible to do via the Designer.
this.CreateSpeedSlider();
}
private void CreateSpeedSlider()
{
this.toolStrip.SuspendLayout();
this.speedSlider = new TrackBar
{
TickStyle = TickStyle.None,
AutoSize = false,
Height = this.toolStrip.Height,
Minimum = 0,
Maximum = 10,
BackColor = this.toolStrip.BackColor,
};
this.toolStrip.Items.Add(new ToolStripControlHost(this.speedSlider));
this.toolStrip.ResumeLayout();
}
ผลลัพธ์ที่ได้คือ TrackBar สีขาวบน ToolStrip สีเทาอ่อน ดูเหมือนว่า
this.toolStrip.BackColor
ไม่ส่งคืนสีด้านหลังของ ToolStrip ที่ใช้ในการวาดจริง
อย่างไรก็ตาม...
ถ้าฉันเปลี่ยนฟังก์ชั่นของฉันเป็น
private void CreateSpeedSlider()
{
this.toolStrip.BackColor = this.toolStrip.BackColor;
...
}
ฉันได้รับแถบเลื่อนสีขาวบน ToolStrip สีขาว แล้ว...การกำหนดมูลค่าทรัพย์สินให้ตัวเอง เปลี่ยนสีจากสีเทาอ่อนเป็นสีขาวได้จริงเหรอ???
ดังนั้นคำถามคือ...
มีวิธีที่ดีในการดึงสีพื้นหลังที่ถูกต้องของ ToolStrip โดยไม่มี 'แฮ็ก' หรือไม่
และพฤติกรรมนี้เกิดขึ้นโดยเจตนาหรือฉันพลาดอะไรไป?
Color.FromArgb(248, 248, 248)
) ดังนั้น เมื่อคุณเพิ่ม TrackBar มันจะสืบทอด ToolStrip BackColor ซึ่งเท่ากับ BackColor ของแบบฟอร์ม แต่จะตั้งค่าไว้แทน ดังนั้น หากคุณต้องการโฮสต์ส่วนควบคุม เอเลี่ยน แบบกึ่งโปร่งใส ให้ตั้งค่า BackColor ของ TooStrip ก่อนที่จะโฮสต์ส่วนควบคุม หรือตั้งค่า BackColor ของส่วนควบคุมให้เป็นสีจริงของ ToolStrip - person Jimi   schedule 14.06.2019