Cara: Merender Elemen Gaya Visual
Namespace System.Windows.Forms.VisualStyles mengekspos VisualStyleElement objek yang mewakili elemen antarmuka pengguna (UI) Windows yang didukung oleh gaya visual. Topik ini menunjukkan cara menggunakan VisualStyleRenderer kelas untuk merender VisualStyleElement yang mewakili tombol Log Off dan Matikan menu Mulai.
Untuk merender elemen gaya visual
VisualStyleRenderer Buat dan atur ke elemen yang ingin Anda gambar. Perhatikan penggunaan Application.RenderWithVisualStyles properti dan VisualStyleRenderer.IsElementDefined metode ; VisualStyleRenderer konstruktor akan memberikan pengecualian jika gaya visual dinonaktifkan atau elemen tidak ditentukan.
private: VisualStyleRenderer^ renderer; VisualStyleElement^ element; public: CustomControl() { this->Location = Point(50, 50); this->Size = System::Drawing::Size(200, 200); this->BackColor = SystemColors::ActiveBorder; this->element = VisualStyleElement::StartPanel::LogOffButtons::Normal; if (Application::RenderWithVisualStyles && VisualStyleRenderer::IsElementDefined(element)) { renderer = gcnew VisualStyleRenderer(element); } }
private VisualStyleRenderer renderer = null; private readonly VisualStyleElement element = VisualStyleElement.StartPanel.LogOffButtons.Normal; public CustomControl() { this.Location = new Point(50, 50); this.Size = new Size(200, 200); this.BackColor = SystemColors.ActiveBorder; if (Application.RenderWithVisualStyles && VisualStyleRenderer.IsElementDefined(element)) { renderer = new VisualStyleRenderer(element); } }
Private renderer As VisualStyleRenderer = Nothing Private element As VisualStyleElement = _ VisualStyleElement.StartPanel.LogOffButtons.Normal Public Sub New() Me.Location = New Point(50, 50) Me.Size = New Size(200, 200) Me.BackColor = SystemColors.ActiveBorder If Application.RenderWithVisualStyles And _ VisualStyleRenderer.IsElementDefined(element) Then renderer = New VisualStyleRenderer(element) End If End Sub
DrawBackground Panggil metode untuk merender elemen yang saat ini diwakiliVisualStyleRenderer.
protected: virtual void OnPaint(PaintEventArgs^ e) override { // Draw the element if the renderer has been set. if (renderer != nullptr) { renderer->DrawBackground(e->Graphics, this->ClientRectangle); } // Visual styles are disabled or the element is undefined, // so just draw a message. else { this->Text = "Visual styles are disabled."; TextRenderer::DrawText(e->Graphics, this->Text, this->Font, Point(0, 0), this->ForeColor); } }
protected override void OnPaint(PaintEventArgs e) { // Draw the element if the renderer has been set. if (renderer != null) { renderer.DrawBackground(e.Graphics, this.ClientRectangle); } // Visual styles are disabled or the element is undefined, // so just draw a message. else { this.Text = "Visual styles are disabled."; TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Point(0, 0), this.ForeColor); } }
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ' Draw the element if the renderer has been set. If (renderer IsNot Nothing) Then renderer.DrawBackground(e.Graphics, Me.ClientRectangle) ' Visual styles are disabled or the element is undefined, ' so just draw a message. Else Me.Text = "Visual styles are disabled." TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _ New Point(0, 0), Me.ForeColor) End If End Sub
Mengompilasi Kode
Contoh ini membutuhkan:
Kontrol kustom yang berasal dari Control kelas .
Form yang menghosting kontrol kustom.
Referensi ke Systemnamespace , System.Drawing, System.Windows.Forms, dan System.Windows.Forms.VisualStyles .
Baca juga
.NET Desktop feedback
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk