mardi 28 janvier 2014

[Help] Transparent window returns no drawing topic




Hi Friends!

As the title Suggests, I have a problem with the output of my drawings and my written text. In another project, in Which I practiced drawing a line and write a text. Since I Could See the results. But Because The background what not transparent!

Can someone tell me what I'm doing wrong here?

Thank you!


Here is my exercise code:

PHP Code:







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.Security.Permissions;

using System.Runtime.InteropServices;

using System.Threading;

using Microsoft.DirectX;

using Microsoft.DirectX.Direct3D;


namespace 
DX3D_First

{

    public 
partial class frmOverlay : Form

    
{

        
#region DLL-IMPORT


        
internal struct Margins

        
{

            public 
int Left, Right, Top, Bottom;

        }


        [
DllImport( "user32.dll", SetLastError = true )]

        public static 
extern UInt32 GetWindowLong( IntPtr hWnd, int nIndex );


        [
DllImport( "user32.dll" )]

        static 
extern int SetWindowLong( IntPtr hWnd, int nIndex, IntPtr dwNewLong );


        [
DllImport( "user32.dll" )]

        static 
extern bool SetLayeredWindowAttributes( IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags );


        [
DllImport( "dwmapi.dll" )]

        static 
extern void DwmExtendFrameIntoClientArea( IntPtr hWnd, ref Margins pMargins );


        public const 
int GWL_EXSTYLE = -20;


        public const 
int WS_EX_LAYERED = 0x80000;


        public const 
int WS_EX_TRANSPARENT = 0x20;


        public const 
int LWA_ALPHA = 0x2;


        public const 
int LWA_COLORKEY = 0x1;


        
#endregion

        

        
Device device = null;


        
Microsoft.DirectX.Direct3D.Font dxText;

        
Microsoft.DirectX.Direct3D.Line dxLine;


        
Margins marg;

        
bool bStopThread = false;


        protected 
override CreateParams CreateParams

        
{

            
get

            
{

                new 
SecurityPermission( SecurityPermissionFlag.UnmanagedCode ).Demand();

                
CreateParams CP = base.CreateParams;


                
CP.ExStyle = CP.ExStyle | 0x00000020;


                return 
CP;

            }

        }


        public 
frmOverlay()

        {

            
InitializeComponent();


            
Gfx();


            
Thread dx = new Thread( new ThreadStart( dxThread ) );

            
dx.IsBackground = true;

            
dx.Start();

        }



        protected 
override void OnPaint( PaintEventArgs e )

        {

            
marg.Left = 0;

            
marg.Top = 0;

            
marg.Right = this.Width;

            
marg.Bottom = this.Height;


            
DwmExtendFrameIntoClientArea( this.Handle, ref marg );

        }


        public 
bool Gfx()

        {

            try

            {

                
SetWindowLong( this.Handle, GWL_EXSTYLE, ( IntPtr )( GetWindowLong( this.Handle, GWL_EXSTYLE ) | WS_EX_LAYERED | WS_EX_TRANSPARENT ) );

                
SetLayeredWindowAttributes( this.Handle, 0, 255, LWA_ALPHA );


                
PresentParameters pp = new PresentParameters();


                
pp.Windowed = true;

                
pp.SwapEffect = SwapEffect.Discard;

                
pp.BackBufferFormat = Format.A8B8G8R8;


                
device = new Device( 0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp );


                
dxLine = new Microsoft.DirectX.Direct3D.Line( device );

                
dxText = new Microsoft.DirectX.Direct3D.Font( device, new System.Drawing.Font( "Arial", 12 ) );


                return 
true;

            }

            catch (
DirectXException)

            {

                return 
false;

            }

        }


        public 
void DrawRect( int _X, int _Y, int _Width, int _Height, Color _Color )

        {

            
Vector2[] Lines = new Vector2[ 5 ];


            
Lines[ 0 ].X = _X;

            
Lines[ 0 ].Y = _Y;


            
Lines[ 1 ].X = _X + _Width;

            
Lines[ 1 ].Y = _Y;


            
Lines[ 2 ].X = _X + _Width;

            
Lines[ 2 ].Y = _Y + _Height;


            
Lines[ 3 ].X = _X;

            
Lines[ 3 ].Y = _Y + _Height;


            
Lines[ 4 ].X = _X;

            
Lines[ 4 ].Y = _Y;


            
dxLine.Draw( Lines, _Color );

        }


        public 
void dxThread()

        {

            while(!
bStopThread)

            {

                if( 
device == null ) return;


                
device.Clear( ClearFlags.Target, Color.FromArgb( 0, 0, 0, 0 ), 1.0f, 0 );

                
device.RenderState.ZBufferEnable = false;

                
device.RenderState.Lighting = false;

                
device.RenderState.CullMode = Cull.None;

                
device.Transform.Projection = Matrix.OrthoOffCenterLH( 0, this.Width, this.Height, 0, 0, 1 );

                
device.BeginScene();


                
// Rendering

                
DrawRect( 10, 20, 100, 150, Color.Blue );


                
dxText.DrawText( null, "Hello World!", 100, 100, Color.Red );


                
device.EndScene();

                
device.Present();


                
Application.DoEvents();

            }


            
device.Dispose();


            
Application.Exit();

        }






        public 
void FillRect( float x, float y, float w, float h, int r, int g, int b )

        {

            
CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[ 4 ];


            
verts[ 0 ].Position = new Vector4( x, y + h, 0, 0.5f );

            
verts[ 1 ].Position = new Vector4( x, y, 0, 0.5f );

            
verts[ 2 ].Position = new Vector4( x + w, y + h, 0, 0.5f );

            
verts[ 3 ].Position = new Vector4( x + w, y, 0, 0.5f );

            
//or make it with for(int i = 0; i < 4; i++) to write less code

            
verts[ 0 ].Color = System.Drawing.Color.FromArgb( r, g, b ).ToArgb();

            
verts[ 1 ].Color = System.Drawing.Color.FromArgb( r, g, b ).ToArgb();

            
verts[ 2 ].Color = System.Drawing.Color.FromArgb( r, g, b ).ToArgb();

            
verts[ 3 ].Color = System.Drawing.Color.FromArgb( r, g, b ).ToArgb();


            
device.VertexFormat = CustomVertex.TransformedColored.Format;

            
device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, verts );

        }




        public 
void DrawRectangle( float x, float y, float w, float h, Color color )

        {

            
float thickness = ( w / 25 );

            
//Vertical bar 1

            
FillRect( x - ( w / 2 ), y - ( h / 2 ), thickness/*just to keep our width consistent*/, h, color.R, color.G, color.B );

            
////Horiz 1

            
FillRect( x - ( w / 2 ), y - ( h / 2 ), w, thickness, color.R, color.G, color.B );

            
////Horizontal bar 2, -50 because we want the exact center of the crosshair to be 

            ////at our X,Y coords and not the far left on crosshair on them

            
FillRect( x - ( w / 2 ), y + ( h / 2 ), w, thickness, color.R, color.G, color.B );

            
////Vertical bar 2

            
FillRect( x + ( w / 2 ), y - ( h / 2 ), thickness, h, color.R, color.G, color.B );

        }

    }

} 














Aucun commentaire:

Enregistrer un commentaire