现在的位置: 首页 > 综合 > 正文

窗体样式使用WS_EX_LAYERED后,无法绘制windows控件的解决办法

2013年04月01日 ⁄ 综合 ⁄ 共 5525字 ⁄ 字号 评论关闭

根据一副png图片绘制半透明窗体时,用了WS_EX_LAYERED后当前窗体再也不会处理paint事件,所以所含的子控件是一辈子也不会画出来的,但是这个控件确实存在,而且可以响应事件 。而此时windows画制窗体是使用UpdateLayeredWindow这个api函数的。

其实这个问题,3年前就在csdn网友miky的"笨笨钟"发布后就讨论过了,后来出了一个叫桌面天气秀
的东东也采用类似的技术。那时候我有幸拿到了miky的delphi下实现gdi+半透明窗体的一段代码,因为无法画出button等控件和
几位高人讨论过,这里是当时的讨论情况

http://borland.mblogger.cn/jinjazz/posts/21093.aspx

最终并没有很好的解决办法,而是通过大概如下的方法解决的

————————————————————————————————————

对于按钮,完全可以自己画两个图片然后盖在button上面,通过处理button的enter和leave消息来切换者两个图片来表达按钮状态

对于输入框..这个可以用一个让任何人看了都生气地办法,那就是....两个窗体
,的确别人就是这么做的

可以用一个空心窗体只显示该显示的控件,然后盖在你的半透明窗体上面,并处理半透明窗体的move事件,让另外一个窗体同步移动或者做其它事情

效果如下:

 

以下是一些C#代码,Delphi的就不贴了

主Form的代码

using
 System;

using
 System.Drawing;

using
 System.Drawing.Imaging;

using
 System.Collections;

using
 System.ComponentModel;

using
 System.Windows.Forms;

using
 System.Data;

using
 System.Runtime.InteropServices;


namespace
 WindowsApplication7


{


    

    

public
 
class
 Form1 : System.Windows.Forms.Form

    


{

        

private
 System.ComponentModel.IContainer components;


        

public
 Form1()

        


{

            

//


            

//
 Windows 窗体设计器支持所必需的

            

//

            InitializeComponent();

            FormBorderStyle 

=
 FormBorderStyle.None;


             

        }





        


///
 
<summary>


        

///
 清理所有正在使用的资源。

        

///
 
</summary>


        
protected
 
override
 
void
 Dispose( 
bool
 disposing )

        


{

            

if
( disposing )

            


{

                

if
 (components 
!=
 
null


                


{

                    components.Dispose();

                }




            }




            

base
.Dispose( disposing );

        }





        

Windows Form Designer generated code




        


///
 
<summary>


        

///
 应用程序的主入口点。

        

///
 
</summary>


        [STAThread]

        

static
 
void
 Main() 

        


{

            

            Application.Run(

new
 Form1());

        }






        Form2 controlFrm 

=
 
new
 Form2();

        

private
 
void
 button1_Click(
object
 sender, System.EventArgs e)

        


{

            MessageBox.Show(controlFrm,controlFrm.textBox1.Text);

            

        }





        

protected
 
override
 CreateParams CreateParams

        


{

            

get


            


{

                CreateParams cp 

=
 
base
.CreateParams;

                cp.ExStyle 

|=
 
0x00080000

//
 This form has to have the WS_EX_LAYERED extended style


                
return
 cp;

            }




        }





        

        

private
 
void
 SetStyle1()

        


{

            Bitmap bm 

=
 Image.FromFile(
@"
Green.png
"

as
 Bitmap;

            Bitmap bm2 

=
 Image.FromFile(
@"
btn.png
"

as
 Bitmap;


            Graphics g 

=
 Graphics.FromImage(bm);

            g.DrawImage(bm2, 

20

20

new
 Rectangle(
0

0

90

50
), GraphicsUnit.Pixel);


            g.DrawString(

"
by jinjazz
"

new
 Font(
"
Ariel
"

9
, FontStyle.Bold), 
new
 SolidBrush(Color.Black), 
new
 PointF(
40

50
));


            

this
.SetBitmap(bm, 
255
);

        }




        

private
 
void
 SetStyle2()

        


{

            Bitmap bm 

=
 Image.FromFile(
@"
Green.png
"

as
 Bitmap;

            Bitmap bm2 

=
 Image.FromFile(
@"
btn.png
"

as
 Bitmap;


            Graphics g 

=
 Graphics.FromImage(bm);

            g.DrawImage(bm2, 

15

15

new
 Rectangle(
7

140

100

50
), GraphicsUnit.Pixel);


            g.DrawString(

"
by jinjazz
"

new
 Font(
"
Ariel
"

9
, FontStyle.Bold), 
new
 SolidBrush(Color.Black), 
new
 PointF(
40

50
));


            

this
.SetBitmap(bm, 
255
);

        }





        

        

private
 
void
 Form1_Load(
object
 sender, System.EventArgs e)

        


{

            

            controlFrm.Show();

            SetStyle1();

           

            

//
this.TopMost = true;


            controlFrm.TopMost 
=
 
true
;

        }





        

private
 
void
 button1_MouseEnter(
object
 sender, EventArgs e)

        


{


            SetStyle2();

        }




        

private
 
void
 button1_MouseLeave(
object
 sender, EventArgs e)

        


{

            SetStyle1();

        }





        

public
 
void
 SetBitmap(Bitmap bitmap, 
byte
 opacity)

        

抱歉!评论已关闭.