How to render a control to look like ComboBox with Visual Styles enabled?

后端 未结 2 1368
日久生厌
日久生厌 2021-02-11 14:58

I have a control that is modelled on a ComboBox. I want to render the control so that the control border looks like that of a standard

相关标签:
2条回答
  • I'm not 100% sure if this is what you are looking for but you should check out the VisualStyleRenderer in the System.Windows.Forms.VisualStyles-namespace.

    1. VisualStyleRenderer class (MSDN)
    2. How to: Render a Visual Style Element (MSDN)
    3. VisualStyleElement.ComboBox.DropDownButton.Disabled (MSDN)

    Since VisualStyleRenderer won't work if the user don't have visual styles enabled (he/she might be running 'classic mode' or an operative system prior to Windows XP) you should always have a fallback to the ControlPaint class.

    // Create the renderer.
    if (VisualStyleInformation.IsSupportedByOS 
        && VisualStyleInformation.IsEnabledByUser) 
    {
        renderer = new VisualStyleRenderer(
            VisualStyleElement.ComboBox.DropDownButton.Disabled);
    }
    

    and then do like this when drawing:

    if(renderer != null)
    {
        // Use visual style renderer.
    }
    else
    {
        // Use ControlPaint renderer.
    }
    

    Hope it helps!

    0 讨论(0)
  • 2021-02-11 15:51

    Are any of the ControlPaint methods useful for this? That's what I usually use for custom-rendered controls.

    0 讨论(0)
提交回复
热议问题