GVKun编程网logo

Java编写实现窗体程序显示日历(用java写一个带有窗体的日历)

6

对于Java编写实现窗体程序显示日历感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解用java写一个带有窗体的日历,并且为您提供关于c#–显示日历值、C#实现计算器窗体程序、C#用记事本编写

对于Java编写实现窗体程序显示日历感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解用java写一个带有窗体的日历,并且为您提供关于c# – 显示日历值、C#实现计算器窗体程序、C#用记事本编写简单WinForm窗体程序、C#窗体程序实现全屏及取消全屏步骤的宝贵知识。

本文目录一览:

Java编写实现窗体程序显示日历(用java写一个带有窗体的日历)

Java编写实现窗体程序显示日历(用java写一个带有窗体的日历)

本文实例为大家分享了Java实现窗体程序显示日历的具体代码,供大家参考,具体内容如下

实训要求:

代码:

Test类:

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
  
public class Test extends JFrame {  
    JButton week1, week2, week3, week4, week5, week6, week7, next, pro;  
    CalendaBean cb = new CalendaBean();  
    JLabel[] label;  
    JLabel now;  
  
    public static void main(String[] args) {  
        Test frame = new Test();  
        frame.setBounds(650,300,550,550); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setTitle("日历");  
        frame.setVisible(true);  
  
    }  
  
    public Test() {  
        int year, month;  
        setLayout(new BorderLayout());  
        JPanel pNorth = new JPanel();  
        cb = new CalendaBean();  
        cb.setYear(2017);  
        cb.setMonth(11);  
        String[] a = cb.getCalendar();  
        next = new JButton("上月");  
        pro = new JButton("下月");  
        next.setActionCommand("lastmonth");  
        pro.setActionCommand("nextmonth");  
        next.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                cb.actionPerformed(e);  
            }  
        });  
        pro.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                cb.actionPerformed(e);  
            }  
        });  
        pNorth.add(next);  
        pNorth.add(pro);  
        add(pNorth, BorderLayout.NORTH);  
        GridLayout grid = new GridLayout(8, 7);  
        JPanel pCenter = new JPanel();  
        week1 = new JButton("日");  
        week2 = new JButton("一");  
        week3 = new JButton("二");  
        week4 = new JButton("三");  
        week5 = new JButton("四");  
        week6 = new JButton("五");  
        week7 = new JButton("六");  
        pCenter.add(week1);  
        pCenter.add(week2);  
        pCenter.add(week3);  
        pCenter.add(week4);  
        pCenter.add(week5);  
        pCenter.add(week6);  
        pCenter.add(week7);  
        label = new JLabel[42];  
        for (int i = 0; i < 42; i++) {  
            label[i] = new JLabel();  
            pCenter.add(label[i]);  
        }  
        cb.label = this.label;  
        for (int i = 0; i < a.length; i++) {  
            if (i % 7 == 0) {  
                label[i].setText("");  
            }  
            label[i].setText("          "+a[i]);  
        }  
        pCenter.setLayout(grid);  
        add(pCenter, BorderLayout.CENTER);  
        JPanel pSouth = new JPanel();  
        now = new JLabel();  
        now.setText("日历:" + cb.year + "年" + cb.month + "月");  
        cb.now = now;  
        pSouth.add(now);  
        add(pSouth, BorderLayout.SOUTH);  
    }  
  
} 

CalendaBean类:

import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.Calendar;  
  
import javax.swing.*;  
public class CalendaBean implements ActionListener {  
    JLabel[] label;  
    JLabel now;  
    String[] day;  
    int year = 0, month = 0;  
    public void setYear(int year) {  
        this.year = year;  
    }  
  
    public void setMonth(int month) {  
        this.month = month;  
    }  
  
    public void actionPerformed(ActionEvent e) {  
        String str = e.getActionCommand();  
        if (str.equals("lastmonth")) {  
            month--;  
            if (month == 0) {  
                month = 12;  
                year--;  
            }  
        }  
        else if (str.equals("nextmonth")) {  
            month++;  
            if (month == 13) {  
                month = 1;  
                year++;  
            }  
        }  
        now.setText("日历:" + year + "年" + month + "月");  
        String[] a = getCalendar();  
        for (int i = 0; i < a.length; i++) {  
            if (i % 7 == 0) {  
                label[i].setText("");  
            }  
            label[i].setText("          "+a[i]);  
        }  
          
    }  
  
    public String[] getCalendar() {  
        String[] a = new String[42];  
        Calendar rili = Calendar.getInstance();  
        rili.set(year, month - 1, 1);  
        int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1;  
        int day = 0;  
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8  
                || month == 10 || month == 12) {  
            day = 31;  
        }  
        if (month == 4 || month == 6 || month == 9 || month == 11) {  
            day = 30;  
        }  
        if (month == 2) {  
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))  
                day = 29;  
            else  
                day = 28;  
        }  
        for (int i = 0; i < weekDay; i++)  
            a[i] = "";  
        for (int i = weekDay, n = 1; i < weekDay + day; i++) {  
            a[i] = String.valueOf(n);  
            n++;  
        }  
        for (int i = weekDay + day; i < a.length; i++)  
            a[i] = "";  
        return a;  
    }  
} 

运行结果:

小结:

学习了Calendar类,其他的,界面的话顺着来就好。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • Java实现简单的日历界面
  • Java实现桌面日历
  • Java实现窗体程序显示日历表
  • Java实现窗体程序显示日历
  • java实现日历窗口小程序
  • 日历显示读出输入的年月的java代码
  • Java中的Calendar日历API用法完全解析
  • Java实现的日历功能完整示例
  • Java实现简单日历小程序 Java图形界面小日历开发
  • Java实现图形化界面的日历

c# – 显示日历值

c# – 显示日历值

如何在文本框中显示选定的日历值?我想在asp.net或C#中得到答案.

解决方法

对于WinForms(我使用过DateTimePicker),您可以处理ValueChanged事件……

private void dateTimePicker1_ValueChanged(object sender,EventArgs e)
{
    textBox1.Text = dateTimePicker1.Text;
}

对于ASP.NET控件(我使用了Calendar控件),您可以处理SelectionChanged事件…

[Markup]
<asp:Calendar ID="Calendar1" runat="server" 
    onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



[CodeBehind]
protected void Calendar1_SelectionChanged(object sender,EventArgs e)
{
    TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
}

希望能帮助到你 :)

C#实现计算器窗体程序

C#实现计算器窗体程序

本文实例为大家分享了C#实现计算器窗体程序的具体代码,供大家参考,具体内容如下

功能设计

1、计算器中,添加 0-9 共十个数字键。

2、计算器中,增添 加、减、乘、除、等于五个功能键。

3、计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方。

实现代码

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 test3_1
{
    public partial class Form1 : Form
    {
        double result = 0;              //存储计算结果
        double number = 0;              //存储输入的数字
        bool exist_value = false;       //判断文本框中是否有值
        string operation;               //存储输入的运算符

        /*
         * 初始化
         */

        public Form1()
        {
            InitializeComponent();
        }

        /*
         * 数字键触发事件实现
         */
        private void Seven_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "7";
        }

        private void Eight_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "8";
        }

        private void Nine_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "9";
        }

        private void Four_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "4";
        }

        private void Five_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "5";
        }

        private void Six_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "6";
        }

        private void One_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "1";
        }

        private void Two_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "2";
        }

        private void Three_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "3";
        }

        private void Zero_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "0";
        }

        /*
         * 功能键触发事件
         */
        private void Add_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "+";
            }
        }

        private void Sub_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "-";
            }
        }

        private void Mul_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "*";
            }
        }

        private void Div_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "/";
            }
        }

        private void Squ_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "x^2";
            }
               
        }

        private void Sqrt_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "sqrt";
            }
        }

        private void Log_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "log";
            }
        }

        private void Ln_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "ln";
            }
        }

        private void Del_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void Equ_Click(object sender, EventArgs e)
        {
            switch (operation)
            {
                case "+": result = number + double.Parse(textBox1.Text); break;
                case "-": result = number - double.Parse(textBox1.Text); break;
                case "*": result = number * double.Parse(textBox1.Text); break;
                case "/":
                    {
                        double temp=double.Parse(textBox1.Text);
                        if (temp != 0)
                            result = number / temp;
                        else
                            MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    }
                case "x^2": result = number * number; break;
                case "sqrt": result = Math.Sqrt(number); break;
                case "log": result = Math.Log10(number); break;
                case "ln": result = Math.Log(number); break;
            }
            textBox1.Text = result + "";
            exist_value = true;
        }
    }
}

界面设计

运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • C#计算器编写代码
  • C#编写的windows计算器的实例代码
  • C#开发简易winform计算器程序
  • C#实现简单的计算器功能完整实例
  • C#实现简单计算器功能
  • C#实现简单加减乘除计算器
  • C#实现Winform版计算器
  • C#实现的简单整数四则运算计算器功能示例
  • c#入门之实现简易存款利息计算器示例
  • C# WinForm程序设计简单计算器

C#用记事本编写简单WinForm窗体程序

C#用记事本编写简单WinForm窗体程序

平时我们编写WinForm程序经常使用VS进行拖控件的方式,这样做虽然简单,但是无法深入了解WinForm程序的本质。其实,用记事本也可以编写出VS编写的WinForm程序。还是直接看代码吧:

1、打开记事本,写入以下代码,另存为hello.cs文件

using System;
using System.Windows.Forms;

namespace Hello
{
 public class Form1:Form
 {
  private System.Windows.Forms.Button btnClose;
  public Form1()
  {
    this.Text = "Form1窗体";
    btnClose = new System.Windows.Forms.Button();
    //将窗体挂起
    this.SuspendLayout();

    //设置按钮属性
    this.btnClose.Location = new System.Drawing.Point(20,20);
    this.btnClose.Size = new System.Drawing.Size(100,25);
    this.btnClose.Name = "btnClose";
    this.btnClose.Text = "按钮";

    this.btnClose.UseVisualStyleBackColor = true;

    //设置按钮控件点击事件
    this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
    //将构造的控件添加到窗体Controls控件集合
    this.Controls.Add(btnClose);
  }
  //按钮点击事件
   private void btnClose_Click(object sender,EventArgs e)
   {
   this.Close();
   }
 }
 public class Program
 {
  //程序入口
  public static void Main()
  {
   Application.Run(new Form1());
  }
 }
}

2、在Windows搜索框输入 cmd,打开控制台,输入以下代码切换目录

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

3、目录切换完毕后,输入以下代码运行

csc.exe /out:e:\hello.exe e:\hello.cs

/out:e:\hello.exe用于指定可执行文件存放的目录和名称
e:\hello.cs用于指定源文件的文件路径


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • C#在Winform开发中使用Grid++报表
  • C# Winform实现石头剪刀布游戏
  • C# Winform实现导入和导出Excel文件
  • C#Winform窗口移动方法
  • C# winform程序读取文本中的值实例讲解

C#窗体程序实现全屏及取消全屏步骤

C#窗体程序实现全屏及取消全屏步骤

由于项目需要,需要用vs窗体程序实现播放视频的窗口的全屏和取消全屏。

具体实现界面如图:

这是初始状态,视频框的右上角就是控制全屏的按钮

这是全屏后的状态,此时全屏按钮变成了取消全屏的样式

注:为了界面的美观我的全屏并没有把左边的那些控件也盖住,但是是可以设置的,下边代码部分我会进行讲解。

1、首先说明一下我所用的控件及我的项目中控件的名称,以便大家理解。

显示视频的黑框是一个picturebox即代码中的VideoPlayWnd,全屏/取消全屏是一个button即代码中的button4

2、具体代码如下:

全屏和取消全屏是一个按钮即button4

private Int16 zoom = -1;//用来控制点击此button4的时候,是需要全屏还是取消全屏
 //视频窗口的缩放代码
  private void button4_Click(object sender, EventArgs e)
  {
   if(zoom<0)
   {
    float w = this.Width - 210; //为了保证左边的控件不被挡上减了一个210
    float h = this.Height - 50;//为了底下的按钮不被挡上,因为还要用到,因此减了50
    this.VideoPlayWnd.Size = Size.Ceiling(new SizeF(w, h));//重新部署视频框即这个picturebox空间的长宽
    VideoPlayWnd.Location = new System.Drawing.Point(210, 0);//视频框的左上角坐标,会发现与我上边减去的210一致(大家肯定都理解为什么我就不说了)
    button4.Location = new System.Drawing.Point(795, 0);//不能只有picturebox变了,这个button的位置也要保证和视频框同步,所以需要重设坐标
    // button4.BackgroundImage = Image.FromFile(@"E:\lwj\addpersoninfo\addpersoninfo\Resources\退出全屏.png");//绝对路径(这是我测试的时候用的)
    button4.BackgroundImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"退出全屏.png");//AppDomain.CurrentDomain.BaseDirectory语句可以获取到你当前项目的bin目录,所以需要把图片放到对应目录下
    zoom = 1;//全屏之后,再点击需要取消全屏,因此要改变zoom的值
   }
   else
   { //以下为取消全屏,即还原自己的视频框和按钮,具体操作类似全屏,就不细致注释了
    VideoPlayWnd.Location = new System.Drawing.Point(495, 360);
    this.VideoPlayWnd.Size = Size.Ceiling(new SizeF(323, 271));
    button4.Location = new System.Drawing.Point(795, 360);
    button4.BackgroundImage = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"全屏.png");
    zoom = -1;//取消全屏后再点击就要进行全屏,因此继续设为-1(保证小于0,以满足全屏操作的if条件)
   }
  }

以上代码中的按钮是给它加了一个全屏样式的背景图片,并在点击时切换背景图片。

补充知识:C# 窗体视频控件进入全屏模式和退出全屏模式

窗体控件进入全屏模式和退出全屏模式,视频播放的时候用到此功能。

工具类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace CvNetVideo.Play
{
 class FullScreenHelper
 {
  bool m_bFullScreen = false;
  IntPtr m_OldWndParent = IntPtr.Zero;
  WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();
  Control m_control = null;
 
  public FullScreenHelper(Control c)
  {
   m_control = c;
  }
  struct POINT
  {
   int x;
   int y;
  };
  struct RECT
  {
   public int left;
   public int top;
   public int right;
   public int bottom;
  };
  //锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态。锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态
  [DllImport("User32.dll")]
  public static extern bool LockWindowUpdate(IntPtr hWndLock);
 
  //函数来设置弹出式窗口,层叠窗口或子窗口的父窗口。新的窗口与窗口必须属于同一应用程序
  [DllImport("User32.dll")]
  public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
  //函数设置指定窗口的显示状态和恢复,最大化,最小化位置。函数功能: 函及原型 
  [DllImport("User32.dll")]
  public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 
  //函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号
  [DllImport("User32.dll")]
  public static extern bool SetForegroundWindow(IntPtr hWnd);
 
  //该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域
  [DllImport("User32.dll")]
  public static extern IntPtr GetDesktopWindow();
 
  //函数名。该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置
  [DllImport("User32.dll")]
  public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 
  //是用于得到被定义的系统数据或者系统配置信息的一个专有名词 
  [DllImport("User32.dll")]
  public static extern int GetSystemMetrics(int nIndex); 
 
  [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
  public static extern IntPtr GetParent(IntPtr hWnd);
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern System.IntPtr GetForegroundWindow();
  [DllImport("user32")]
  public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  [DllImport("user32.dll")]
  public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p);
   public void FullScreen(bool flag)
  {
   m_bFullScreen = flag;
   if (!m_bFullScreen)
   {
    LockWindowUpdate(m_control.Handle);
    SetParent(m_control.Handle, m_OldWndParent);
    SetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
    SetForegroundWindow(m_OldWndParent);
    LockWindowUpdate(IntPtr.Zero);
   }
   else
   {
    GetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
    int nScreenWidth = GetSystemMetrics(0);
    int nScreenHeight = GetSystemMetrics(1);
    m_OldWndParent = GetParent(m_control.Parent.Handle);
    SetParent(m_control.Handle, GetDesktopWindow());
    WINDOWPLACEMENT wp1 = new WINDOWPLACEMENT();
    wp1.length = (uint)Marshal.SizeOf(wp1);
    wp1.showCmd = 1;
    wp1.rcNormalPosition.left = 0;
    wp1.rcNormalPosition.top = 0;
    wp1.rcNormalPosition.right = nScreenWidth;
    wp1.rcNormalPosition.bottom = nScreenHeight;
    SetWindowPlacement(m_control.Handle, ref wp1);
    SetForegroundWindow(GetDesktopWindow());
    SetForegroundWindow(m_control.Handle);
   }
   m_bFullScreen = !m_bFullScreen;
  }
  struct WINDOWPLACEMENT
  {
   public uint length;
   public uint flags;
   public uint showCmd;
   public POINT ptMinPosition;
   public POINT ptMaxPosition;
   public RECT rcNormalPosition;
  };
 }
}

调用方式

 /// <summary>
  /// 全屏事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void UCVideo_DoubleClick(object sender, EventArgs e)
  {
   //全屏设置
   //sdlVideo.SDL_MaximizeWindow();
   fullScreenHelper = new CvNetVideo.Play.FullScreenHelper(this);
   fullScreenHelper.FullScreen(true);
   Console.WriteLine("Entrance FullScreen Mode");
  }
  /// <summary>
  /// 按键弹起事件
  /// </summary>
  private void UCVideo_KeyUp(object sender, KeyEventArgs e)
  {
   // ESC 退出全屏
   if (e.KeyCode == Keys.Escape&& fullScreenHelper!=null)
   {
    fullScreenHelper.FullScreen(false);
    fullScreenHelper = null;
    Console.WriteLine("Exit FullScreen Mode");
   }   
  }

测试效果图

注意:在使用SDL的全屏操作过程中设置是无效的,播放视频过程中不能实现修改。代码如下:

public void SDL_MaximizeWindow()
  {
   Console.WriteLine("设置全屏");
 
   SDL.SDL_MaximizeWindow(screen);
   SDL.SDL_SetWindowFullscreen(screen, SDL.SDL_GetWindowFlags(screen));
   SDL.SDL_ShowWindow(screen);
 
   //int width, height;
    获取最大窗口值
   //SDL2.SDL.SDL_GetWindowMaximumSize(screen, out width, out height);
    设置最大窗口值
   //if (width>0&&height>0)
   //{
   // SDL2.SDL.SDL_SetWindowMaximumSize(screen, width, height);
   // Console.WriteLine("设置全屏......成功!width=" + width + ",height=" + height);
   //}
   //else
   //{
   // Console.WriteLine("设置全屏......失败!width=" + width + ",height=" + height);
   // SDL2.SDL.SDL_SetWindowMaximumSize(screen, w, h);
   //}
  }

工具代码功能改进

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace CvNetVideo.Play
{
 /// <summary>
 /// 定义全屏抽象类
 /// </summary>
 public abstract class FullScreenObject
 {
  public abstract void FullScreen(bool flag);
 }
 /// <summary>
 /// 桌面全屏
 /// </summary>
 public unsafe class FullScreenHelper: FullScreenObject
 {
  bool m_bFullScreen = false;
 
  WINDOWPLACEMENT m_OldWndPlacement = new WINDOWPLACEMENT();
 
  UCVideo m_control = null;
 
  public FullScreenHelper(UCVideo control)
  {
   m_control = control;
  }
 
  private IntPtr m_OldWndParent = IntPtr.Zero;
 
  DockStyle old_docker_style;
  int old_left;
  int old_width;
  int old_height;
  int old_top;
 
  public override void FullScreen(bool flag)
  {
   m_bFullScreen = flag;
   if (!m_bFullScreen)
   {
    #region 方式一:窗体容积改变时不能全屏,未能解决IE全屏显示不全问题
    //ShellSDK.LockWindowUpdate(m_control.Handle);
    //ShellSDK.SetParent(m_control.Handle, m_OldWndParent);
    //ShellSDK.SetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
    //ShellSDK.SetForegroundWindow(m_OldWndParent);
    //ShellSDK.LockWindowUpdate(IntPtr.Zero);
    #endregion
 
    #region 方式二:在容器改变时可以实现全屏,未能解决IE全屏显示不全问题
    // 取消全屏设置
    m_control.Dock = old_docker_style;
    m_control.Left = old_left;
    m_control.Top = old_top;
    m_control.Width = old_width;
    m_control.Height = old_height;
    ShellSDK.SetParent(m_control.Handle, m_OldWndParent);
    #endregion
   }
   else
   {
    #region 方式一:窗体容积改变时不能全屏,未能解决IE全屏显示不全问题
    //ShellSDK.GetWindowPlacement(m_control.Handle, ref m_OldWndPlacement);
    //int nScreenWidth = ShellSDK.GetSystemMetrics(0);
    //int nScreenHeight = ShellSDK.GetSystemMetrics(1);
    //m_OldWndParent = ShellSDK.GetParent(m_control.Handle);
    //ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());
    //WINDOWPLACEMENT wp1 = new WINDOWPLACEMENT();
    //wp1.length = (uint)Marshal.SizeOf(wp1);
    //wp1.showCmd = 1;
    //wp1.rcNormalPosition.left = 0;
    //wp1.rcNormalPosition.top = 0;
    //wp1.rcNormalPosition.right = Screen.PrimaryScreen.Bounds.Width/*nScreenWidth*/;
    //wp1.rcNormalPosition.bottom = Screen.PrimaryScreen.WorkingArea.Height/* nScreenHeight*/;
    //ShellSDK.SetWindowPlacement(m_control.Handle, ref wp1);
    //ShellSDK.SetForegroundWindow(ShellSDK.GetDesktopWindow());
    //ShellSDK.SetForegroundWindow(m_control.Handle);
    #endregion
 
    #region 方式二:在容器改变时可以实现全屏,未能解决IE全屏显示不全问题
    // 记录原来的数据
    old_docker_style = m_control.Dock;
    old_left = m_control.Left;
    old_width = m_control.Width;
    old_height = m_control.Height;
    old_top = m_control.Top;
    m_OldWndParent = ShellSDK.GetParent(m_control.Handle);
    // 设置全屏数据
    int nScreenWidth = ShellSDK.GetSystemMetrics(0);
    int nScreenHeight = ShellSDK.GetSystemMetrics(1);
    m_control.Dock = DockStyle.None;
    m_control.Left = 0;
    m_control.Top = 0;
    m_control.Width = nScreenWidth;
    m_control.Height = nScreenHeight;
    ShellSDK.SetParent(m_control.Handle, ShellSDK.GetDesktopWindow());
    ShellSDK.SetWindowPos(m_control.Handle, -1, 0, 0, m_control.Right - m_control.Left, m_control.Bottom - m_control.Top, 0);
    #endregion
   }
   m_bFullScreen = !m_bFullScreen;
  }
 
 }
 
 /// <summary>
 /// 在容器内部全屏
 /// </summary>
 public class FullScreenInContainerHelper : FullScreenObject
 {
  bool m_bFullScreen = false;
 
  Control m_control = null;
 
  public FullScreenInContainerHelper(Control control)
  {
   m_control = control;
  }
 
  private IntPtr m_OldWndParent = IntPtr.Zero;
  private IntPtr m_father_hwnd;
  private RECT m_rect = new RECT();
 
  public override void FullScreen(bool flag)
  {
   m_bFullScreen = flag;
   if (!m_bFullScreen)
   {
    ShellSDK.SetParent(m_control.Handle, m_father_hwnd);
    ShellSDK.SetWindowPos(m_control.Handle, 0, m_rect.left, m_rect.top, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top, 0);
    ShellSDK.SetForegroundWindow(m_father_hwnd);
   }
   else
   {
    m_father_hwnd = ShellSDK.GetParent(m_control.Handle);
    ShellSDK.GetWindowRect(m_control.Handle, out RECT rect);
    POINT pt = new POINT();
    pt.x = rect.left;
    pt.y = rect.top;
    ShellSDK.ScreenToClient(m_father_hwnd, ref pt);
    rect.right = rect.right - rect.left + pt.x;
    rect.bottom = rect.bottom - rect.top + pt.y;
    rect.left = pt.x;
    rect.top = pt.y;
    m_rect = rect;
    ShellSDK.GetWindowRect(m_father_hwnd, out RECT rect_fature);
    ShellSDK.SetWindowPos(m_control.Handle, 0, 0, 0, rect_fature.right - rect_fature.left, rect_fature.bottom - rect_fature.top, 0);
   }
   m_bFullScreen = !m_bFullScreen;
  }
 }
 
 /// <summary>
 /// Windows系统API-SDK
 /// </summary>
 public class ShellSDK
 {
  //锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态。锁定指定窗口,禁止它更新。同时只能有一个窗口处于锁定状态
  [DllImport("User32.dll")]
  public static extern bool LockWindowUpdate(IntPtr hWndLock);
 
  //函数来设置弹出式窗口,层叠窗口或子窗口的父窗口。新的窗口与窗口必须属于同一应用程序
  [DllImport("User32.dll")]
  public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
  //函数设置指定窗口的显示状态和恢复,最大化,最小化位置。函数功能: 函及原型 
  [DllImport("User32.dll")]
  public static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 
  //函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号
  [DllImport("User32.dll")]
  public static extern bool SetForegroundWindow(IntPtr hWnd);
 
  //该函数返回桌面窗口的句柄。桌面窗口覆盖整个屏幕。桌面窗口是一个要在其上绘制所有的图标和其他窗口的区域
  [DllImport("User32.dll")]
  public static extern IntPtr GetDesktopWindow();
 
  //函数名。该函数返回指定窗口的显示状态以及被恢复的、最大化的和最小化的窗口位置
  [DllImport("User32.dll")]
  public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 
  //是用于得到被定义的系统数据或者系统配置信息的一个专有名词 
  [DllImport("User32.dll")]
  public static extern int GetSystemMetrics(int nIndex); 
 
  [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
  public static extern IntPtr GetParent(IntPtr hWnd);
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  public static extern System.IntPtr GetForegroundWindow();
  [DllImport("user32")]
  public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  [DllImport("user32.dll")]
  public static extern uint ScreenToClient(IntPtr hwnd, ref POINT p);
 }
 
 /// <summary>
 /// 图像区域对象
 /// </summary>
 public struct RECT
 {
  public int left;
  public int top;
  public int right;
  public int bottom;
 }
 
 /// <summary>
 /// 图像点位位置
 /// </summary>
 public struct POINT
 {
  public int x;
  public int y;
 }
 
 /// <summary>
 /// 图像窗口对象
 /// </summary>
 public struct WINDOWPLACEMENT
 {
  public uint length;
  public uint flags;
  public uint showCmd;
  public POINT ptMinPosition;
  public POINT ptMaxPosition;
  public RECT rcNormalPosition;
 }
}

以上这篇C#窗体程序实现全屏及取消全屏步骤就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:
  • C#实现窗体抖动的两种方法
  • C# winform中窗口关闭按钮的隐藏与禁用详解

关于Java编写实现窗体程序显示日历用java写一个带有窗体的日历的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c# – 显示日历值、C#实现计算器窗体程序、C#用记事本编写简单WinForm窗体程序、C#窗体程序实现全屏及取消全屏步骤的相关知识,请在本站寻找。

本文标签: