首页 > 编程 > C# > 正文

WinForm实现窗体最大化并遮盖任务栏的方法

2019-10-29 21:38:42
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了WinForm实现窗体最大化并遮盖任务栏的方法,涉及C#实现WinForm窗体全屏显示的实现及调用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了WinForm实现窗体最大化并遮盖任务栏的方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. using System; 
  2. using System.Windows.Forms; 
  3. using System.Drawing; 
  4. namespace CSImageFullScreenSlideShow 
  5. { 
  6. public class FullScreen 
  7. { 
  8. private FormWindowState winState; 
  9. private FormBorderStyle brdStyle; 
  10. private bool topMost; 
  11. private Rectangle bounds; 
  12. public FullScreen() 
  13. { 
  14. IsFullScreen = false; 
  15. } 
  16. public bool IsFullScreen 
  17. { 
  18. get; 
  19. set; 
  20. } 
  21. public void EnterFullScreen(Form targetForm) 
  22. { 
  23. if (!IsFullScreen) 
  24. { 
  25. Save(targetForm); // Save the original form state. 
  26. targetForm.WindowState = FormWindowState.Maximized; 
  27. targetForm.FormBorderStyle = FormBorderStyle.None; 
  28. targetForm.TopMost = true; 
  29. targetForm.Bounds = Screen.GetBounds(targetForm); 
  30. IsFullScreen = true; 
  31. } 
  32. } 
  33. /// <summary> 
  34. /// Save the current Window state. 
  35. /// </summary> 
  36. private void Save(Form targetForm) 
  37. { 
  38. winState = targetForm.WindowState; 
  39. brdStyle = targetForm.FormBorderStyle; 
  40. topMost = targetForm.TopMost; 
  41. bounds = targetForm.Bounds; 
  42. } 
  43. /// <summary> 
  44. /// Leave the full screen mode and restore the original window state. 
  45. /// </summary> 
  46. public void LeaveFullScreen(Form targetForm) 
  47. { 
  48. if (IsFullScreen) 
  49. { 
  50. // Restore the original Window state. 
  51. targetForm.WindowState = winState; 
  52. targetForm.FormBorderStyle = brdStyle; 
  53. targetForm.TopMost = topMost; 
  54. targetForm.Bounds = bounds; 
  55. IsFullScreen = false; 
  56. } 
  57. } 
  58. } 
  59. } 

调用:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8. namespace CSImageFullScreenSlideShow 
  9. { 
  10. public partial class Test : Form 
  11. { 
  12. public Test() 
  13. { 
  14. InitializeComponent(); 
  15. } 
  16. private FullScreen fullScreen = new FullScreen(); 
  17. private void button1_Click(object sender, EventArgs e) 
  18. { 
  19. if (fullScreen.IsFullScreen) 
  20. { 
  21. fullScreen.LeaveFullScreen(this); 
  22. } 
  23. else 
  24. { 
  25. fullScreen.EnterFullScreen(this); 
  26. } 
  27. } 
  28. } 
  29. } 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表