在 C Sharp 中调整自定义表单的大小(具有投影效果)和鼠标拖动事件的控件?

发布于 2024-09-25 19:55:30 字数 90 浏览 3 评论 0原文

在我的应用程序中,我必须调整表单的大小及其对鼠标拖动效果的所有控制,并且表单应该具有阴影效果,问题是我的所有表单都是自定义表单(没有边框)。

提前致谢

In my application I have to resize forms and all its control on mouse drag effect and forms should have drop shadow effect the problem is that all my forms are custom one (with no boarder).

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

输什么也不输骨气 2024-10-02 19:55:30

我认为你必须自己实现

  1. 鼠标按下开始绑定鼠标拖动+更改光标以调整
  2. 鼠标拖动上的图标大小,只需简单地减小
  3. 鼠标向上解除绑定鼠标拖动事件的

表单大小我建议动态事件绑定的原因,以便你可以指定哪个控件或区域应该按下鼠标

i think u have to implement by yourself

  1. on mouse down start bind on mouse drag + change cursor to resize icon
  2. on mouse drag, just simply reduce your form size
  3. on mouse up unbind mouse drag event

the reason i suggest dynamic event binding so u can specified which control or area should have mouse down

初吻给了烟 2024-10-02 19:55:30

我不确定投影效果,但您应该能够通过在右下角放置一个按钮和一些适当的图标来调整表单的大小。当用户单击并拖动此按钮时,它会调整表单的大小。这是一些示例代码:

public partial class Form1 : Form
{
    private int bottomBorder;
    private int rightBorder;
    private Point mouseStart;
    private bool isResizing = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isResizing)
        {
            var newLocation = button1.Location;
            newLocation.Offset(
                e.X - mouseStart.X,
                e.Y - mouseStart.Y);
            button1.Location = newLocation;
            this.Height = button1.Bottom + bottomBorder;
            this.Width = button1.Right + rightBorder;
            button1.Refresh();
        }

    }

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        isResizing = true;
        mouseStart = e.Location;
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        isResizing = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bottomBorder = this.Height - button1.Bottom;
        rightBorder = this.Width - button1.Right;
    }
}

I'm not sure about the drop shadow effect, but you should be able to resize a form by placing a button in the bottom right corner with some appropriate icon. When the user clicks and drags this button, it resizes the form. Here's some example code:

public partial class Form1 : Form
{
    private int bottomBorder;
    private int rightBorder;
    private Point mouseStart;
    private bool isResizing = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isResizing)
        {
            var newLocation = button1.Location;
            newLocation.Offset(
                e.X - mouseStart.X,
                e.Y - mouseStart.Y);
            button1.Location = newLocation;
            this.Height = button1.Bottom + bottomBorder;
            this.Width = button1.Right + rightBorder;
            button1.Refresh();
        }

    }

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        isResizing = true;
        mouseStart = e.Location;
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        isResizing = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bottomBorder = this.Height - button1.Bottom;
        rightBorder = this.Width - button1.Right;
    }
}
狠疯拽 2024-10-02 19:55:30

如果没有边框(或某些控件),您打算如何调整大小?找出该部分,然后在表单中尝试此代码:

public class CustomForm : Form
{
    private const int WmNcLButtonDown = 0xA1;
    private const int HtBottomRight = 17;

    [DllImport("user32.dll")]
    private static extern int ReleaseCapture();

    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

    // elsewhere
    void ResizeForm()
    {
        ReleaseCapture();
        SendMessage(this.Handle, WmNcLButtonDown, HtBottomRight, 0);
    }
}

此代码将调整表单的大小,就像使用了右下角一样。查找 HT_BOTTOMRIGHT 和其他 HT_ 常量以了解不同位置的大小调整。

Without a border (or some control), how do you intend to resize? Figure that part out, then try this code in your form:

public class CustomForm : Form
{
    private const int WmNcLButtonDown = 0xA1;
    private const int HtBottomRight = 17;

    [DllImport("user32.dll")]
    private static extern int ReleaseCapture();

    [DllImport("user32.dll")]
    private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

    // elsewhere
    void ResizeForm()
    {
        ReleaseCapture();
        SendMessage(this.Handle, WmNcLButtonDown, HtBottomRight, 0);
    }
}

This code will resize your form as though the bottom right corner was used. Look up HT_BOTTOMRIGHT and other HT_ constants for different locations for resizing.

献世佛 2024-10-02 19:55:30

我使用了 Don Kirkby 和 Matthew Ferreira 的解决方案,并结合两者创建了我自己的解决方案。我添加了一个名为“resizeHandle”的 StatusStrip,使其大小为 20x20 像素并监听其事件。

public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
private const int wmNcLButtonUp = 0xA2;
private bool isResizing = false;

[DllImport("user32.dll")]
private static extern int ReleaseCapture();

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

private void resizeHandle_MouseDown(object sender, MouseEventArgs e)
{
    isResizing = true;
}

private void resizeHandle_MouseMove(object sender, MouseEventArgs e)
{
    if (isResizing)
    {
        // Check if we have released the Left mouse button
        isResizing = (e.Button == MouseButtons.Left);
        ReleaseCapture();
        if (isResizing)
        {
            SendMessage(Handle, wmNcLButtonDown, HtBottomRight, 0);
        }
        else
        {
            // Left Mouse button was released, end resizing.
            SendMessage(Handle, wmNcLButtonUp, HtBottomRight, 0);
        }
    }
}

I used the solutions by Don Kirkby and Matthew Ferreira and created my own solution combining the two. I added a StatusStrip named "resizeHandle", made it's size 20x20 pixels and listened to it's events.

public class CustomForm : Form
{
private const int WmNcLButtonDown = 0xA1;
private const int HtBottomRight = 17;
private const int wmNcLButtonUp = 0xA2;
private bool isResizing = false;

[DllImport("user32.dll")]
private static extern int ReleaseCapture();

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wparam, int lparam);

private void resizeHandle_MouseDown(object sender, MouseEventArgs e)
{
    isResizing = true;
}

private void resizeHandle_MouseMove(object sender, MouseEventArgs e)
{
    if (isResizing)
    {
        // Check if we have released the Left mouse button
        isResizing = (e.Button == MouseButtons.Left);
        ReleaseCapture();
        if (isResizing)
        {
            SendMessage(Handle, wmNcLButtonDown, HtBottomRight, 0);
        }
        else
        {
            // Left Mouse button was released, end resizing.
            SendMessage(Handle, wmNcLButtonUp, HtBottomRight, 0);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文