using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace QQFrm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

#region  公共变量
        IntPtr Tem_Handle;//获取控件及窗体的句柄
        Point CPoint;//获取控件中鼠标的坐标
        static int Tem_place = 0;
        int Frm_Height = 0;
        int FrmHeight = 0;

#endregion

#region  API声明
        //获取当前鼠标下可视化控件的句柄
        [DllImport("user32.dll")]
        public static extern int WindowFromPoint(int xPoint, int yPoint);
        //获取指定句柄的父级句柄
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetParent(IntPtr hWnd);
        //获取屏幕的大小
        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
        private static extern int GetSystemMetrics(int mVal);
        #endregion

#region  获取当前鼠标下可视化控件的句柄
        /// <summary>
        /// 获取当前鼠标下可视化控件的句柄
        /// </summary>
        /// <param x="int">当前鼠标的X坐标</param>
        /// <param y="int">当前鼠标的Y坐标</param>
        public IntPtr FormNameAt(int x, int y)
        {
            IntPtr Tem_hWnd;//设置存储句柄的变量
            Tem_Handle = (IntPtr)(WindowFromPoint(x, y));//获取当前鼠标下可视化控件的句柄
            Tem_hWnd = Tem_Handle;//记录原始句柄
            while (Tem_hWnd != ((IntPtr)0))//遍历该句柄的父级句柄
            {
                Tem_Handle = Tem_hWnd;//记录当前句柄
                Tem_hWnd = GetParent(Tem_hWnd);//获取父级句柄
            }
            return Tem_Handle;//返回最底层的父级句柄
        }
        #endregion

private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Top < 3 && Tem_place==0)//如果窗体被移到屏幕的顶部
                {
                    if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                    {
                        panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                        timer2.Enabled = false;//不对窗体进行拉伸操作
                        this.Top = 0;//使窗体致顶
                    }
                    else
                    {
                        panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                        timer2.Enabled = true;//将窗体在顶部进行隐藏
                    }
                }
                else
                {
                    if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的左端或右端
                    {
                        if (this.Left < 3)//如果窗体被移到屏幕的左端
                        {
                            if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                            {
                                panel_Title.Tag = 2;//设置标识,用于判断窗体在屏幕左端
                                timer2.Enabled = false;
                                Frm_Height = this.Height;
                                this.Left = 0;//使窗体致左
                                this.Top = 0;
                                this.Height = Screen.AllScreens[0].Bounds.Height;
                                Tem_place = 1;
                            }
                            else
                            {
                                panel_Title.Tag = 2;
                                timer2.Enabled = true;//将窗体在左端进行隐藏
                            }
                        }
                        if (this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的右端
                        {
                            if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                            {
                                panel_Title.Tag = 3;//设置标识,用于判断窗体在屏幕右端
                                timer2.Enabled = false;
                                Frm_Height = this.Height;
                                this.Left = GetSystemMetrics(0) - this.Width;//使窗体致右
                                this.Top = 0;
                                this.Height = Screen.AllScreens[0].Bounds.Height;
                                Tem_place = 1;
                            }
                            else
                            {
                                panel_Title.Tag = 3;
                                timer2.Enabled = true;//将窗体在右端进行隐藏
                            }
                        }

}
                }

}

private void timer2_Tick(object sender, EventArgs e)
        {
            switch (Convert.ToInt16(panel_Title.Tag.ToString()))//对标识进行判断
            {
                case 1://顶端隐藏
                    {
                        if (this.Top < 5)
                            this.Top = -(this.Height - 2);
                        break;
                    }
                case 2://左端隐藏
                    {
                        if (this.Left < 5)
                            this.Left = -(this.Width - 2);
                        break;
                    }
                case 3://右端隐藏
                    {
                        if ((this.Left + this.Width) > (GetSystemMetrics(0) - 5))
                            this.Left = GetSystemMetrics(0) - 2;
                        break;
                    }
            }
        }

private void panel1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

#region  利用窗体上的控件移动窗体
        /// <summary>
        /// 利用控件移动窗体
        /// </summary>
        /// <param Frm="Form">窗体</param>
        /// <param e="MouseEventArgs">控件的移动事件</param>
        public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空间using System.Windows.Forms;
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
                Tem_place = 0;
                this.Height = FrmHeight;
            }
        }
        #endregion

private void panel_Title_MouseDown(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            CPoint = new Point(-e.X, -e.Y);
        }

private void panel_Title_MouseMove(object sender, MouseEventArgs e)
        {
            FrmMove(this, e);
        }

private void panel_Title_MouseUp(object sender, MouseEventArgs e)
        {
            timer1.Enabled = true;
        }

private void Form1_Load(object sender, EventArgs e)
        {
            Frm_Height = this.Height;
            FrmHeight = this.Height;
            this.TopMost = true;
        }
    }
}

C#窗体布局技巧的更多相关文章

  1. Android学习笔记之布局技巧以及布局中的细节介绍....

    PS:休息两天,放一放手上的东西,做做总结... 学习内容: 1.Android中LinearLayout布局技巧... 2.layout中drawable属性的区别...   先简单的介绍一下dra ...

  2. DIV+CSS常用网页布局技巧!

    以下是我整理的DIV+CSS常用网页布局技巧,仅供学习与参考! 第一种布局:左边固定宽度,右边自适应宽度 HTML Markup <div id="left">Left ...

  3. 总结与学习DIV+CSS网页布局技巧

    以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实也很简单,只不过方式不同而已. <style> ...

  4. 移动平台3G手机网站前端开发布局技巧

    本文转载至:移动平台3G手机网站前端开发布局技巧汇总 - 前端开发-武方博 您或许正在或准备参与一个WepApp项目,您或许正在Google搜索mobile development相 关的文章,您或许 ...

  5. pyqt5 窗体布局

    窗体布局 1使用qtdesigner新建一个对话框,然后拖放几个按钮和文本框,按钮使用水平布局,效果如下: 鼠标选中水平布局再选中文本框,进行垂直布局,如下: 垂直布局后的效果如下: 然后,如何让窗体 ...

  6. 移动平台3G手机网站前端开发布局技巧汇总

    移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博   发布:2011-05-10 09:11   分类:移动开发   阅读:120,618 views   7条评论     您或许正在 ...

  7. C#入门经典(2-重置窗体布局,界面介绍,错误列表)

  8. CSS之定位布局(position,定位布局技巧)

    css之定位 1.什么是定位:css中的position属性,position有四个值:absolute/relative/fixed/static(绝对/相对/固定/静态(默认))通过定位属性可以设 ...

  9. UWP开发入门(九)——简单界面的布局技巧及屏幕适应

    嘿嘿嘿,题目比较绕哈.本篇主要讨论一般情况下,页面的布局技巧,怎么将元素的展现尽量做到分辨率无关.基本的思路仍然是尽量少的标定具体的数字,而是用比列来标注各元素占据的空间. 这里我打算用易信的名片页来 ...

随机推荐

  1. 报错 hint: Updates were rejected because the remote contains work that you do 解决方法

    1. git pull origin master --allow-unrelated-histories 2.git pull origin master 3.git init 4.git remo ...

  2. SP10707 COT2 - Count on a tree II (树上莫队)

    大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...

  3. 外网teamview连接不上服务器teamview的错误: 一直显示正在连接,正在初始化显示参数的

    一.错误 错误:  服务器的TV已打开,外网连接服务器TV,一直显示正在连接,正在初始化显示参数的原因. 二. 分析原因: 1. teamview简介TV 2. 服务器必须要开一个远程端口,意思是必须 ...

  4. 精华 selenium_webdriver(python)调用js脚本

    #coding=utf-8 from selenium import webdriver import time driver = webdriver.Firefox() driver.get(&qu ...

  5. HomeBrew的安装和简单使用

    homebrew 官网 https://brew.sh/ 转自:http://blog.csdn.NET/maojudong/article/details/7918291 1.  前言 作为Linu ...

  6. 实现qq登录

    //html页面   引入qq图标 <td> <a href="#" onclick="toLogin()"><img src=& ...

  7. git commit -am "remark" 提交

    一.前言 假如你昨晚把本地文件a.html提交到远程库,今早发现还有补充的内容,于是添加了新的内容到a.html,并且还在本地还多添加了“几个文件”,那么怎么使用git来把这些文件一并提交到远程库呢? ...

  8. swagger访问api, TypeError: Failed to fetch

    用swagger访问https://localhost:44360/api/ads/1, 得到的结果是 TypeError: Failed to fetch.一开始以为是后端代码问题,检查了好久,才发 ...

  9. python 转换代码格式

    import os dirname="C:\\Users\\haier\\Desktop\\new" def walk(path): for item in os.listdir( ...

  10. global 全局变量 nonlocal 局部变量

    # x= # def func(): # x= # # func() # print(x) # x=[] # def func(): # x.append() # x.append() # x.app ...