winform、无边框拖动大小、上有一层panel

loyal_to 2017-12-19 06:12:02

如图,我有一个无边框窗体,上面有2个控件,我重写了WndProc,但是鼠标在panel和tabcontrol上移动,窗体的WndProc捕获不到,也就不能改变和拖动大小,这是我的窗体代码

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case ImportFromDLL.WM_COPYDATA:
var copyData = (ImportFromDLL.COPYDATASTRUCT)m.GetLParam(typeof(ImportFromDLL.COPYDATASTRUCT)); //获取数据
//MessageBox.Show(copyData.lpData);
base.WndProc(ref m); //todo 接受到消息后做一些事情
break;
case 0x0084:
base.WndProc(ref m);
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)Win32.Guying_HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)Win32.Guying_HTBOTTOMLEFT;
else m.Result = (IntPtr)Win32.Guying_HTLEFT;
else if (vPoint.X >= ClientSize.Width - 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)Win32.Guying_HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)Win32.Guying_HTBOTTOMRIGHT;
else m.Result = (IntPtr)Win32.Guying_HTRIGHT;
else if (vPoint.Y <= 5)
m.Result = (IntPtr)Win32.Guying_HTTOP;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)Win32.Guying_HTBOTTOM;
break;
case 0x0201: //鼠标左键按下的消息
m.Msg = 0x00A1; //更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero; //默认值
m.WParam = new IntPtr(2);//鼠标放在标题栏内
base.WndProc(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
...全文
561 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
引用 11 楼 xuzuning 的回复:
你最好贴出布局代码,以便重现你的问题

就像这个我有一个无边框窗体,上面有个panel,docker=fill,现在我需要加上阴影效果和允许拖动。下面使我代码

public partial class Form3 : Form
{

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);

[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

[DllImport("dwmapi.dll")]
public static extern int DwmIsCompositionEnabled(ref int pfEnabled);

private bool m_aeroEnabled; // variables for box shadow
private const int CS_DROPSHADOW = 0x00020000;
private const int WM_NCPAINT = 0x0085;
private const int WM_ACTIVATEAPP = 0x001C;

public struct MARGINS // struct for box shadow
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}

private const int WM_NCHITTEST = 0x84; // variables for dragging the form
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

protected override CreateParams CreateParams
{
get
{
m_aeroEnabled = CheckAeroEnabled();

CreateParams cp = base.CreateParams;
if (!m_aeroEnabled)
cp.ClassStyle |= CS_DROPSHADOW;

return cp;
}
}

// protected override CreateParams CreateParams
// {
// get
// {
// const int CS_DROPSHADOW = 0x20000;
// CreateParams cp = base.CreateParams;
// cp.ClassStyle |= CS_DROPSHADOW;
// return cp;
// }
// }

private bool CheckAeroEnabled()
{
if (Environment.OSVersion.Version.Major >= 6)
{
int enabled = 0;
DwmIsCompositionEnabled(ref enabled);
return (enabled == 1) ? true : false;
}
return false;
}

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCPAINT: // box shadow
if (m_aeroEnabled)
{
var v = 2;
DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
MARGINS margins = new MARGINS()
{
bottomHeight = 1,
leftWidth = 1,
rightWidth = 1,
topHeight = 1
};
DwmExtendFrameIntoClientArea(this.Handle, ref margins);

}
break;
default:
break;
}
base.WndProc(ref m);

if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT) // drag the form
m.Result = (IntPtr)HTCAPTION;

}

public Form3()
{
m_aeroEnabled = false;

this.FormBorderStyle = FormBorderStyle.None;

InitializeComponent();
this.Padding = new Padding(5);
}


因为panel在上面窗体的WndProc捕获不到鼠标移动,也就不能拖动大小,所以用padding,让能捕获到鼠标。
然而调用了api画阴影,有padding时会显示一条线,那条线很难看
我的问题是,现在的代码怎么去掉显示那条难看的线,而又不影响其他。
或者有其他方式可以达到有阴影,能拖动窗体大小

xuzuning 2017-12-22
  • 打赏
  • 举报
回复
你最好贴出布局代码,以便重现你的问题
yzf86211861 2017-12-22
  • 打赏
  • 举报
回复
好像挺 难弄的 等高手
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
引用 7 楼 qq_35898357 的回复:
[quote=引用 5 楼 loyal_to 的回复:]
算了,在找不到就用padding,有线就有线把



哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote]
关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
因为上面有个tabcontrol,属性是fill上层按界面要求肯定是要有panel的
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
引用 17 楼 qq_35898357 的回复:
[quote=引用 15 楼 loyal_to 的回复:] [quote=引用 13 楼 qq_35898357 的回复:] [quote=引用 9 楼 loyal_to 的回复:] [quote=引用 7 楼 qq_35898357 的回复:] [quote=引用 5 楼 loyal_to 的回复:] 算了,在找不到就用padding,有线就有线把
哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote] 关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看[/quote] 我刚创建了个窗口来使用你的代码,设置了属性 TransparencyKEY = Transparent;就行了,你试一下。其实窗口阴影和拖动的实现方式有很多中。[/quote] 窗体上的控件全透明了。。。。我不要透明啊[/quote] 那你就别用透明色啊.............................................................[/quote] 我瓜皮了。。。。
捞鬼然然 2017-12-22
  • 打赏
  • 举报
回复
引用 15 楼 loyal_to 的回复:
[quote=引用 13 楼 qq_35898357 的回复:] [quote=引用 9 楼 loyal_to 的回复:] [quote=引用 7 楼 qq_35898357 的回复:] [quote=引用 5 楼 loyal_to 的回复:] 算了,在找不到就用padding,有线就有线把
哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote] 关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看[/quote] 我刚创建了个窗口来使用你的代码,设置了属性 TransparencyKEY = Transparent;就行了,你试一下。其实窗口阴影和拖动的实现方式有很多中。[/quote] 窗体上的控件全透明了。。。。我不要透明啊[/quote] 那你就别用透明色啊.............................................................
xuzuning 2017-12-22
  • 打赏
  • 举报
回复
其实你是可以在 panel 的 MouseDown 事件中发送窗体移动消息的
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x112;
        public const int HTCAPTION = 0x0002;
        public const int SC_MOVE = 0xF010; //移动 
        void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
引用 13 楼 qq_35898357 的回复:
[quote=引用 9 楼 loyal_to 的回复:] [quote=引用 7 楼 qq_35898357 的回复:] [quote=引用 5 楼 loyal_to 的回复:] 算了,在找不到就用padding,有线就有线把
哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote] 关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看[/quote] 我刚创建了个窗口来使用你的代码,设置了属性 TransparencyKEY = Transparent;就行了,你试一下。其实窗口阴影和拖动的实现方式有很多中。[/quote] 窗体上的控件全透明了。。。。我不要透明啊
loyal_to 2017-12-22
  • 打赏
  • 举报
回复
引用 13 楼 qq_35898357 的回复:
[quote=引用 9 楼 loyal_to 的回复:] [quote=引用 7 楼 qq_35898357 的回复:] [quote=引用 5 楼 loyal_to 的回复:] 算了,在找不到就用padding,有线就有线把
哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote] 关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看[/quote] 我刚创建了个窗口来使用你的代码,设置了属性 TransparencyKEY = Transparent;就行了,你试一下。其实窗口阴影和拖动的实现方式有很多中。[/quote] 我试了,可以,谢谢,唉,对这方面不怎么熟
捞鬼然然 2017-12-22
  • 打赏
  • 举报
回复
引用 9 楼 loyal_to 的回复:
[quote=引用 7 楼 qq_35898357 的回复:] [quote=引用 5 楼 loyal_to 的回复:] 算了,在找不到就用padding,有线就有线把
哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。[/quote] 关键是这个窗体我还加了阴影的,调用的DwmExtendFrameIntoClientArea,会导致有一条106,238,255颜色的线,贼难看[/quote] 我刚创建了个窗口来使用你的代码,设置了属性 TransparencyKEY = Transparent;就行了,你试一下。其实窗口阴影和拖动的实现方式有很多中。
捞鬼然然 2017-12-21
  • 打赏
  • 举报
回复
引用 5 楼 loyal_to 的回复:
算了,在找不到就用padding,有线就有线把



哎呀,服了你了。把背景色调成阴影色不就行了,还能加重阴影。
xuzuning 2017-12-21
  • 打赏
  • 举报
回复
为什么要有个 panel 呢?
loyal_to 2017-12-21
  • 打赏
  • 举报
回复
算了,在找不到就用padding,有线就有线把
loyal_to 2017-12-21
  • 打赏
  • 举报
回复
有人知道吗。。。查资料查了半条找不到
assky124 2017-12-20
  • 打赏
  • 举报
回复
http://download.csdn.net/download/assky124/7989969 我原来写的仿Ribbon样式的窗体。坑爹的C币,我原来是免费的资源...
捞鬼然然 2017-12-19
  • 打赏
  • 举报
回复
选择你的form,在属性里把padding设置为1吧.这样就可以拉了。
loyal_to 2017-12-19
  • 打赏
  • 举报
回复
这种我如何才能拖动改变大小,尝试过panel的mouse_down、mouse_up、mouse_move自己去计算改变尺寸,效果很不好

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧