2014-04-09 12 views
4

우리 프로그램에서 몇 가지 사용자 지정 '창'을 만들고 VisualStyles을 사용하면 창의 각 요소와 크기를 찾고 최소화 및 닫기 단추를 포함하여 직접 그릴 수 있습니다 적절한 렌더러를 사용하여.Windows 클래식 스타일 창 요소 그리는 방법

우리는 VisualStyles이 비활성화되어 있고 현재 우리 자신의 창을 그렸지만 똑같은 것을하고 싶습니다. 그러나 그들은 꽤 추합니다. WinForms C#에서 Windows Classic 스타일 윈도우를 그릴 수 있습니까? ClassicBorderDecorator을 찾았지만 WPF 용입니다. 그 실패

또는, 우리는 다음과 같은 방법으로 할 창 장식의 픽셀 크기를 얻을 수있는 방법 :

// Get the height of the window caption. 
if (SetRenderer(windowElements["windowCaption"])) 
{ 
    captionHeight = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Height; 
} 

// Get the thickness of the left, bottom, 
// and right window frame. 
if (SetRenderer(windowElements["windowLeft"])) 
{ 
    frameThickness = renderer.GetPartSize(graphics.Graphics, ThemeSizeType.True).Width; 
} 

답변

8

Windows에서 고전적인 스타일의 렌더러를 제공하지 않습니다, 당신은거야 너 자신을 요리해야 해. SystemInformation 클래스를 사용하여 색상을 가져 오는 Color.FromKnownColor() 메트릭을 가져옵니다.

유일한 까다로운 부분은 프레임 단추가 잘 보이게하는 것입니다. 가장 좋은 방법은 직접 그리는 대신 글꼴의 글리프를 사용하는 것입니다. Webdings 글꼴은 이에 이상적입니다.

내 컴퓨터가 얼마나 일치하는지 확인할 수 없으며 컴퓨터가 Windows 8을 부팅하고 더 이상 클래식 스타일을 지원하지 않습니다. 완전히 추한하지

protected override void OnPaintBackground(PaintEventArgs e) { 
    base.OnPaintBackground(e); 
    var captionHeight = SystemInformation.CaptionHeight; 
    int border = SystemInformation.Border3DSize.Width; 
    var color1 = Color.FromKnownColor(activated ? KnownColor.ActiveCaption : KnownColor.InactiveCaption); 
    var color2 = Color.FromKnownColor(activated ? KnownColor.GradientActiveCaption : KnownColor.GradientInactiveCaption); 
    var captionrc = new Rectangle(0, 0, this.ClientSize.Width, captionHeight); 
    using (var brush = new LinearGradientBrush(captionrc, color1, color2, 0, false)) { 
     e.Graphics.FillRectangle(brush, captionrc); 
    } 
    int textx = border; 
    if (this.Icon != null) { 
     int height = SystemInformation.SmallIconSize.Height; 
     var iconrc = new Rectangle(border, (captionHeight - height)/2, height, height); 
     textx += height + border; 
     e.Graphics.DrawIcon(this.Icon, iconrc); 
    } 
    var color = Color.FromKnownColor(activated ? KnownColor.ActiveCaptionText : KnownColor.InactiveCaptionText); 
    using (var font = new Font(this.Font.FontFamily, SystemInformation.CaptionHeight - 4 * border, GraphicsUnit.Pixel)) { 
     TextRenderer.DrawText(e.Graphics, this.Text, font, new Point(textx, border), color); 
    } 
    using (var font = new Font(new FontFamily("Webdings"), captionHeight - 4 * border, GraphicsUnit.Pixel)) { 
     var glyphs = this.WindowState == FormWindowState.Maximized ? "\u0030\u0031\u0072" : "\u0030\u0031\u0072"; 
     var width = TextRenderer.MeasureText(glyphs, font).Width; 
     TextRenderer.DrawText(e.Graphics, glyphs, font, 
      new Point(this.ClientSize.Width - width, border), 
      Color.FromKnownColor(KnownColor.WindowFrame)); 
    } 
} 

내 컴퓨터에 다음과 같습니다 :

enter image description here

: 그렇지 않으면 당신은 아마이에 너무 많은 시간을 투자하지해야한다는 강한 힌트를 몇 가지 예제 코드를 :)