无边距和可调整大小的表格(C#) [英] Borderless and Resizable Form (C#)

查看:214
本文介绍了无边距和可调整大小的表格(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些代码,在网上,并复制它,到目前为止,我已经能够得到的一切权利,除了一件事就是我想要的形式(窗口)完全无边框。

I found some code online and copied it, so far I have been able to get everything right except for one thing which is I want to make the form (window) completely borderless.

我在使用Visual Studio 2013年这个问题很简单大约是使窗体(窗口)无边框的代码。问题是,当你把它无国界,它不再调整大小,但是当它有一个边界,它可以调整。

I'm using Visual Studio 2013 and this question is simply about the code that is needed to make the form (window) borderless. The problem is that when you make it borderless, it's no longer resizable but when it has a border, it can be resized.

我知道使用一些代码,它可能覆盖并实现两者。这是我迄今(从其他网站复制)。这种摆脱它具有程序名称,通过点击和拖动窗体使窗体移动顶栏的,它是可以调整大小。

I know using some code it's possible to override and achieve both. This is what I have so far (copied from another website). This gets rid of the top bar which has the program name, makes the form movable by clicking and dragging the form, and it's resizable.

唯一的问题是,边界还在那儿。我可以添加哪些代码,这使边框将会消失?我想保持这个当前的代码,因为它提供了几个特点,我需要了。

Only problem is that the border is still there. What code can I add to this so the border will be gone? I want to keep this current code because it's serving a couple of features I need already.

顺便说一句,我看着主题类似一些老的问题,但没有。找到合适的代码,我需要

By the way, I looked at some older questions with similar topics but didn't find the right code I need.

对于谁指示我到另一个线程mod:我试过代码,而这不正是我M努力实现虽然这是一个类似的问题。当我试图代码,我不能单击窗体(窗口)上的任意位置移动。另外,它在其中是不是我要找的右下角有一个可调整大小角落。我需要在所有边角就像一个普通的窗口大小调整功能

For the mod who directed me to another thread: I tried the code there and it's not exactly what I'm trying to achieve although it's a similar problem. When I tried that code, I couldn't click anywhere on the form (window) to move it. Plus, it had one resizable corner at the bottom right which is not what I'm looking for. I need the resize function at all corners and sides just like a normal window.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BoxHider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //Next line doesn't seem to be working
            this.FormBorderStyle = FormBorderStyle.None;
        }
        const int WM_NCHITTEST = 0x0084;
        const int HTCLIENT = 1;
        const int HTCAPTION = 2;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    if (m.Result == (IntPtr)HTCLIENT)
                    {
                        m.Result = (IntPtr)HTCAPTION;
                    }
                    break;
            }
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= 0x40000;
                return cp;
            }
        }               
    }
}

< IMG SRC =http://i.stack.imgur.com/9YNqv.jpgALT =我需要什么>

推荐答案

试试这个:

public Form1()
{
    InitializeComponent();
    this.FormBorderStyle = FormBorderStyle.None;
}

protected override void WndProc(ref Message m)
{
    const int RESIZE_HANDLE_SIZE = 10;

    switch (m.Msg)
    {
        case 0x0084/*NCHITTEST*/ :
            base.WndProc(ref m);

            if ((int)m.Result == 0x01/*HTCLIENT*/)
            {
                Point screenPoint = new Point(m.LParam.ToInt32());
                Point clientPoint = this.PointToClient(screenPoint);                        
                if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 13/*HTTOPLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 12/*HTTOP*/ ;
                    else
                        m.Result = (IntPtr) 14/*HTTOPRIGHT*/ ;
                }
                else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 10/*HTLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 2/*HTCAPTION*/ ;
                    else
                        m.Result = (IntPtr) 11/*HTRIGHT*/ ;
                }
                else
                {
                    if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                        m.Result = (IntPtr) 16/*HTBOTTOMLEFT*/ ;
                    else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                        m.Result = (IntPtr) 15/*HTBOTTOM*/ ;
                    else
                        m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                }
            }
            return;
    }
    base.WndProc(ref m);
}

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style |= 0x20000; // <--- use 0x20000
        return cp;
    }
}



信息来源:

informational sources:

  • http://stackoverflow.com/a/19670447/2352507
  • Custom Resize Handle in Border-less Form C#
  • http://www.csharp411.com/add-drop-shadow-to-borderless-form/

这篇关于无边距和可调整大小的表格(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆