使ToolBar看起来像Windows Vista / 7而不是经典

时间:2017-04-16 19:06:00

标签: c# winforms pinvoke toolbar

我想让我的应用程序看起来更像是一个本机应用程序,而不是.NET应用程序,因为Visual Designer和C#,我使用.NET。

我看过一些使用工具栏的原生应用,看起来与Vista / 7菜单非常相似。

查看示例:

Windows Vista/7 style

某些原生应用程序(如Notepad ++,Codeblocks等)对工具栏使用相同的Vista / 7样式。我怎样才能在C#中做同样的事情?我知道P / Invoke,所以,我需要知道要使用的方法或示例。

我没有使用ToolBarStrip,因为原生性,我使用ToolBar。我可以使用什么P / Invoke使工具栏看起来像上面的图像(Vista / 7看起来)?

编辑:根据此question,我需要在P / Invoke而不是Win32中执行相同操作。

3 个答案:

答案 0 :(得分:3)

Notepad ++在其源代码中使用两种版本的本机工具栏控件。我假设它根据Windows版本在两者之间进行选择。您已经为遗留的(ToolBar类)尝试了.NET包装器,因此可能不是您喜欢的那个。

另一个是最近的Rebar control,也称为" Coolbar"。请注意,它的外观取决于Windows版本,因此请不要使用链接的MSDN文章中的(过时的)屏幕截图。没有官方的.NET包装器,但程序员已经写了一些。有一个Codeproject.com project提出一个,我通常不推荐任何此类项目,但你听起来很有能力解决这些问题。

答案 1 :(得分:0)

我看到windows vista工具栏中应用了淡入淡出设置,这对于Xaml中的画笔更容易。

然而,这是codeproject中的可下载主题,您可以参考它在该处的完成方式。

https://www.codeproject.com/Articles/18858/Fully-themed-Windows-Vista-Controls

答案 2 :(得分:-1)

这些程序很可能正在使用某种不同的UI框架。 其中一种方法是:  1.删​​除边框  2.在里面绘制自定义边框  3.添加自定义夹点

这是我的图书馆,this snippet有抓握代码,随时可以使用它。(如果你想要整个lib :) :)此外,还有演示应用程序!

该代码段的代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;


public class Form_WOC : Form
{
    public enum LinePositions
    {
        TOP,
        BOTTOM,
        LEFT,
        RIGHT
    }
    private Rectangle TopGrip { get { return new Rectangle(0, 0, this.ClientSize.Width, _gripSize); } }
    private Rectangle LeftGrip { get { return new Rectangle(0, 0, _gripSize, this.ClientSize.Height); } }
    private Rectangle BottomGrip { get { return new Rectangle(0, this.ClientSize.Height - _gripSize, this.ClientSize.Width, _gripSize); } }
    private Rectangle RightGrip { get { return new Rectangle(this.ClientSize.Width - _gripSize, 0, _gripSize, this.ClientSize.Height); } }

    private Rectangle TopLeftGrip { get { return new Rectangle(0, 0, _gripSize, _gripSize); } }
    private Rectangle TopRightGrip { get { return new Rectangle(this.ClientSize.Width - _gripSize, 0, _gripSize, _gripSize); } }
    private Rectangle BottomLeftGrip { get { return new Rectangle(0, this.ClientSize.Height - _gripSize, _gripSize, _gripSize); } }
    private Rectangle BottomRightGrip { get { return new Rectangle(this.ClientSize.Width - _gripSize, this.ClientSize.Height - _gripSize, _gripSize, _gripSize); } }

    private List<Line> _lines = new List<Line>();

    private int _gripSize = 10;
    private const int
        HTLEFT = 10,
        HTRIGHT = 11,
        HTTOP = 12,
        HTTOPLEFT = 13,
        HTTOPRIGHT = 14,
        HTBOTTOM = 15,
        HTBOTTOMLEFT = 16,
        HTBOTTOMRIGHT = 17;

    protected override void WndProc(ref Message message)
    {
        base.WndProc(ref message);
        if (message.Msg == 0x84) // WM_NCHITTEST
        {
            var cursor = this.PointToClient(Cursor.Position);

            if (TopLeftGrip.Contains(cursor)) message.Result = (IntPtr)HTTOPLEFT;
            else if (TopRightGrip.Contains(cursor)) message.Result = (IntPtr)HTTOPRIGHT;
            else if (BottomLeftGrip.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMLEFT;
            else if (BottomRightGrip.Contains(cursor)) message.Result = (IntPtr)HTBOTTOMRIGHT;

            else if (TopGrip.Contains(cursor)) message.Result = (IntPtr)HTTOP;
            else if (LeftGrip.Contains(cursor)) message.Result = (IntPtr)HTLEFT;
            else if (RightGrip.Contains(cursor)) message.Result = (IntPtr)HTRIGHT;
            else if (BottomGrip.Contains(cursor)) message.Result = (IntPtr)HTBOTTOM;
        }
    }
    public void drawLine(LinePositions pos, Color color, int point1, int point2)
    {
        _lines.Add(new Line(pos, color, point1, point2));
    }

    public void clearLines()
    {
        _lines.Clear();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Pen pen = new Pen(Color.Red, 10);
        foreach (Line line in _lines)
        {
            pen.Color = line.Color;

            if (line.LinePosition == LinePositions.BOTTOM)
                e.Graphics.DrawLine(pen, line.X1, Height, line.X2, Height);
            else if (line.LinePosition == LinePositions.TOP)
                e.Graphics.DrawLine(pen, line.X1, 0, line.X2, 0);
            else if (line.LinePosition == LinePositions.RIGHT)       
                e.Graphics.DrawLine(pen, Width, line.Y1, Width, line.Y2);
            else
                e.Graphics.DrawLine(pen, 0, line.Y1, 0, line.Y2);
        }
    }

    public int GripSize
    {
        get { return _gripSize; }
        set { _gripSize = value; }
    }


    class Line
    {
        private int _x1;
        private int _x2;
        private int _y1;
        private int _y2;
        private Color _color;
        private LinePositions _positon;

        public Line(LinePositions position, Color color, int point1, int point2)
        {
            if (position == LinePositions.TOP || position == LinePositions.BOTTOM)
            {
                _x1 = point1;
                _x2 = point2;
            }
            else
            {
                _y1 = point1;
                _y2 = point2;
            }
            _color = color;
            _positon = position;
        }

        public Color Color { get { return _color; } }
        public int X1 { get { return _x1; } }
        public int X2 { get { return _x2; } }
        public int Y1 { get { return _y1; } }
        public int Y2 { get { return _y2; } }
        public LinePositions LinePosition { get { return _positon; } }
    }
}