首页 > 学院 > 开发设计 > 正文

WinForm特效:桌面上的遮罩层

2019-11-17 02:52:48
字体:
来源:转载
供稿:网友
WinForm特效:桌面上的遮罩层

一个窗体特效,帮你了解几个windows api函数.效果:windows桌面上增加一个简单的遮罩层,其中WS_EX_TRANSPARENT 比较重要,它实现了鼠标穿透的功能。

[csharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Drawing;
  3. usingSystem.Windows.Forms;
  4. usingSystem.Runtime.InteropServices;
  5. namespaceWindowsapplication40
  6. {
  7. publicpartialclassForm1:Form
  8. {
  9. publicForm1()
  10. {
  11. InitializeComponent();
  12. }
  13. [DllImport("user32.dll",EntryPoint="GetWindowLong")]
  14. publicstaticexternlongGetWindowLong(IntPtrhwnd,intnIndex);
  15. [DllImport("user32.dll",EntryPoint="SetWindowLong")]
  16. publicstaticexternlongSetWindowLong(IntPtrhwnd,intnIndex,longdwNewLong);
  17. [DllImport("user32",EntryPoint="SetLayeredWindowAttributes")]
  18. PRivatestaticexternintSetLayeredWindowAttributes(IntPtrHandle,intcrKey,bytebAlpha,intdwFlags);
  19. constintGWL_EXSTYLE=-20;
  20. constintWS_EX_TRANSPARENT=0x20;
  21. constintWS_EX_LAYERED=0x80000;
  22. constintLWA_ALPHA=2;
  23. privatevoidForm1_Load(objectsender,EventArgse)
  24. {
  25. this.BackColor=Color.Silver;
  26. this.TopMost=true;
  27. this.FormBorderStyle=FormBorderStyle.None;
  28. this.WindowState=FormWindowState.Maximized;
  29. SetWindowLong(Handle,GWL_EXSTYLE,GetWindowLong(Handle,GWL_EXSTYLE)|WS_EX_TRANSPARENT|WS_EX_LAYERED);
  30. SetLayeredWindowAttributes(Handle,0,128,LWA_ALPHA);
  31. }
  32. }
  33. }

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