Hiding dashed outline around trackbar control when

2019-01-28 08:13发布

问题:

In C# winforms, is there a way to not show the dashed focus outline border that shows around a trackbar control when it is being used?

Details: This outline looks kinda tacky to me, so I'm just shooting for aesthetics to not show it.

Thanks,

Adam

回答1:

ShowFocusCues didn't work for me, but this did:

   internal class NoFocusTrackBar : System.Windows.Forms.TrackBar
   {
      [System.Runtime.InteropServices.DllImport("user32.dll")]
      public extern static int SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

      private static int MakeParam(int loWord, int hiWord)
      {
         return (hiWord << 16) | (loWord & 0xffff);
      }

      protected override void OnGotFocus(EventArgs e)
      {
         base.OnGotFocus(e);
         SendMessage(this.Handle, 0x0128, MakeParam(1, 0x1), 0);
      }
   }

See documentation on WM_UPDATEUISTATE for how this works (basically sending a message to turn the dumb thing off the trackbar gets the focus).



回答2:

I know it's an old question but this is simpler if anyone interested:

public class TrackBarWithoutFocus : TrackBar
{
    private const int WM_SETFOCUS = 0x0007;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_SETFOCUS)
        {
            return;
        }

        base.WndProc(ref m);
    }
}