本文介绍了如何在运行时调整没有boder的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我有这段代码在运行时调整表单大小:



hiI have this code to resize a form at runtime:

private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HTBORDER = 18;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int HTCAPTION = 2;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;

[DllImport("user32.dll")]
public extern static bool ReleaseCapture();

[DllImport("user32.dll")]
public extern static bool SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

private enum ResizeDirection
{
    None,
    Left,
    TopLeft,
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft
}

private ResizeDirection resizeDir;

private ResizeDirection ResizeDir
{
    get { return resizeDir; }
    set
    {
        resizeDir = value;

        switch (resizeDir)
        {
            case ResizeDirection.Left:
                Cursor = Cursors.SizeWE;
                break;

            case ResizeDirection.Right:
                Cursor = Cursors.SizeWE;
                break;

            case ResizeDirection.Top:
                Cursor = Cursors.SizeNS;
                break;

            case ResizeDirection.Bottom:
                Cursor = Cursors.SizeNS;
                break;

            case ResizeDirection.BottomLeft:
                Cursor = Cursors.SizeNESW;
                break;

            case ResizeDirection.TopRight:
                Cursor = Cursors.SizeNESW;
                break;

            case ResizeDirection.BottomRight:
                Cursor = Cursors.SizeNWSE;
                break;

            case ResizeDirection.TopLeft:
                Cursor = Cursors.SizeNWSE;
                break;

            default:
                Cursor = Cursors.Default;
                break;
        }
    }
}

private void ResizeForm(ResizeDirection direction)
{
    int dir = -1;

    switch (direction)
    {
        case ResizeDirection.Left:
            dir = HTLEFT;
            break;

        case ResizeDirection.TopLeft:
            dir = HTTOPLEFT;
            break;

        case ResizeDirection.Top:
            dir = HTTOP;
            break;

        case ResizeDirection.TopRight:
            dir = HTTOPRIGHT;
            break;

        case ResizeDirection.Right:
            dir = HTRIGHT;
            break;

        case ResizeDirection.BottomRight:
            dir = HTBOTTOMRIGHT;
            break;

        case ResizeDirection.Bottom:
            dir = HTBOTTOM;
            break;

        case ResizeDirection.BottomLeft:
            dir = HTBOTTOMLEFT;
            break;
    }

    if (dir != -1)
    {
        ReleaseCapture();
        SendMessage(Handle, WM_NCLBUTTONDOWN, dir, 0);
    }
}

protected override void OnMouseDown(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && WindowState != FormWindowState.Maximized)
        ResizeForm(ResizeDir);

    base.OnMouseDown(e);
}

private const int BorderWidth = 6;

protected override void OnMouseMove(MouseEventArgs e)
{
    if (e.Location.X < BorderWidth && e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.TopLeft;

    else if (e.Location.X < BorderWidth && e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.BottomLeft;

    else if (e.Location.X > Width - BorderWidth && e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.BottomRight;

    else if (e.Location.X > Width - BorderWidth && e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.TopRight;

    else if (e.Location.X < BorderWidth)
        ResizeDir = ResizeDirection.Left;

    else if (e.Location.X > Width - BorderWidth)
        ResizeDir = ResizeDirection.Right;

    else if (e.Location.Y < BorderWidth)
        ResizeDir = ResizeDirection.Top;

    else if (e.Location.Y > Height - BorderWidth)
        ResizeDir = ResizeDirection.Bottom;

    else ResizeDir = ResizeDirection.None;


    base.OnMouseMove(e);
}





代码工作正常没有问题,但我也希望在表单上显示系统菜单我也使用此代码:





the Code works fine without problems, but I also want to show the system menu on the form also I use this code:

private const int WS_CLIPCHILDREN = 0x2000000;
private const int WS_MINIMIZEBOX = 0x20000;
private const int WS_MAXIMIZEBOX = 0x10000;
private const int WS_SYSMENU = 0x80000;
private const int CS_DBLCLKS = 0x8;

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
        cp.ClassStyle = CS_DBLCLKS;

        return cp;
    }
}





当我打下最后一个代码时,缩放器代码停止工作,当我更改此代码时代码:





When I lay the last code, the resizer code stops working and when I change this code:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;

        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
        cp.Style |= WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; //| WS_SYSMENU;
        cp.ClassStyle = CS_DBLCLKS;

        return cp;
    }
}





resizer代码重新开始工作,但系统菜单不再出现。

什么是解决方案

谢谢



the resizer code returns to work again, but the system menu does not longer appear.
What is the solution
Thanks

推荐答案


这篇关于如何在运行时调整没有boder的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:56