winform窗体 小程序【移动窗体和阴影】

摘要:
未设置边框后,无法移动表单。引用API以使其能够获得函数。移动//表单移动API[DllImport(“user32.dll”)]publicstaticxternboolReleaseCapture();[DllImport(“user32.dll”)]publicstaticexternboolSendMessage(IntPtrhwnd,intwMsg,intwParam,intIParam);聚氨基甲酸酯

窗体无边框设置后无法移动,引用API 使其获得功能

移动

winform窗体 小程序【移动窗体和阴影】第1张winform窗体 小程序【移动窗体和阴影】第2张
//窗体移动API
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0xB;


//将方法复制到代码中,在将窗体的MouseDown(按下事件)委托此方法
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (this.WindowState == FormWindowState.Normal)
    {
        ReleaseCapture();
        SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
    }
}
窗体移动API

阴影

winform窗体 小程序【移动窗体和阴影】第3张winform窗体 小程序【移动窗体和阴影】第4张
1、添加命名空间:
using System.Runtime.InteropServices;  

2、定义常量值及函数:
private const int CS_DropSHADOW = 0x20000;  
private const int GCL_STYLE = (-26);  
[DllImport("user32.dll", CharSet = CharSet.Auto)]  
public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);  
[DllImport("user32.dll", CharSet = CharSet.Auto)]  
public static extern int GetClassLong(IntPtr hwnd, int nIndex);


3、构造方法下引用:
SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);  
窗体阴影API

案例

winform窗体 小程序【移动窗体和阴影】第5张winform窗体 小程序【移动窗体和阴影】第6张
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        //窗体移动API
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private const int WM_SETREDRAW = 0xB;

        //窗体阴影API
        private const int CS_DropSHADOW = 0x20000;
        private const int GCL_STYLE = (-26);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        public Form1()
        {
            InitializeComponent();

            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); 
        }


        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }

    }
}
View Code

案例、建立一个无边框窗体

winform窗体 小程序【移动窗体和阴影】第7张winform窗体 小程序【移动窗体和阴影】第8张
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form4 : Form
    {
        //窗体移动API
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        [DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private const int WM_SETREDRAW = 0xB;


        public Form4()
        {
            InitializeComponent();
        }


        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }

        //鼠标进入时发生
        private void pictureBox2_MouseEnter(object sender, EventArgs e) 
        {
            pictureBox2.BackgroundImage = Image.FromFile(@"e:/c2.png"); //改变背景图
        }                                               // @  --字符串的原样输出,加不加都像          
                                                        //e:/c2.png --图片的地址 

        //鼠标离开时时发生 -- 刚开始默认图片是c1.png
        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            pictureBox2.BackgroundImage = Image.FromFile(@"e:/c1.png");
        }


        //鼠标按下时发生
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox2.BackgroundImage = Image.FromFile(@"e:/c3.png");
        }

        //点击时发生
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;   //最大化


           // this.Close();      关闭
        }

    }
}
View Code

背景图片的设置

pictureBox1.BackgroundImage = Image.FromFile(" 图片路径地址 " );

免责声明:文章转载自《winform窗体 小程序【移动窗体和阴影】》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇浅谈自动特征构造工具Featuretools中间件简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Winform DataGridView CheckBoxColumn c# 单选 解决方案

这个问题由来已久,我最近在工作中也遇到了这个问题,不过属于这个问题比较简单初级的涉及。 发现网上对这个问题的解决方案很多不对,答非所问。 所以这里将我测试成功的解决方案记录下来。 首先,DataGridView CheckBoxColumn 默认是可以多选,不能单选的。 所以无法通过设置来解决,必须通过代码自己控制。 而具体使用哪个事件,哪种逻辑也有很...

微信小程序开发记录(七)新版授权登录的实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/wh_xmy/article/details/86475859 背景:由于微信官方修改了 getUserInfo、authorize 接口,无法弹出授权窗口,所以现在无法实现一进入微信小程序就弹出授权...

Spread for Windows Forms快速入门(10)绑定到数据库

下面的教程将带你创建一个工程, 并将Spread控件绑定到一个数据库。 在这个教程中,主要的步骤为: 1. 将Spread添加到一个数据绑定工程中 2. 设置数据库连接 3. 指定要使用的数据 4. 创建数据集 5. 把Spread控件绑定到数据库 6. 通过改变单元格类型改善显示效果 将Spread添加到一个数据绑定工程中 打开一个新的Visual St...

引领Boost(五)(Boost::array)

一 Boost::array       在以前,如果我们要处理一组数据,我们可能使用一般数组存储,或者需要许多的对数组的数据的操作的时候,我们使用STL容器存储。但是如果我们的需求是,我们能够提前固定一组数据的大小,或提前知道这组数据的大小,但是我们又想这组数据进行一些操作,比如排序。。。显然对这个情况,上面的使用原始数组或STL容器都有点不太合适,因为...

QT QString类型转换为const char*(toLatin1)

Qstring str = "helloworld"; char *s; QByteArray ba = str.toLatin1(); s = ba.data(); toLatin1、toLocal8Bit都是QString转QByteArray的方法,Latin1代表ASCII,Local8Bit代表unicode。 const char* -- 指...

pc端遇到的知识点

1、封装时间组件(基于 element) commonDate.vue <template> <!-- 年月日 --> <el-date-picker v-model="val" :type="type"...