I am inserting advertising on a windows phone 7 app.
this is where i create the drawable add:
AdComponent.Initialize("test_client");
Microsoft.Xna.Framework.Rectangle rect = new Microsoft.Xna.Framework.Rectangle(0, 0, 480, 80);
//rect.Location= (20,20);
drawableAd = AdComponent.Current.CreateAd("Image480_80", rect, true);
drawableAd.AdRefreshed += new EventHandler(drawableAd_AdRefreshed);
drawableAd.ErrorOccurred += new EventHandler<Microsoft.Advertising.AdErrorEventArgs>(drawableAd_ErrorOccurred);
and this is where i draw it on the OnDraw method:
AdComponent.Current.Draw();
what i am trying to do is make it come to the front, because i draw other components on the OnDraw method and even though i draw the add in the end, it still goes behind all the other components
i figured it out, i was drawing it inside the spriteBatch together with the other components when in fact you have to draw it after the spriteBatch.End(); thanks for the help anyway
Related
What I would like to do is to simply display a Texture2D on the screen, the screen needs to be transparent as well. I already have a section of code I found which can make the window borderless.
IntPtr hWnd = this.Window.Handle;
var control = System.Windows.Forms.Control.FromHandle(hWnd);
var form = control.FindForm();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
When put into the Initialize() method, this successfully makes the window borderless.
however, when I use a GraphicsDevice.Clear(Color.Transparent); in my Draw all it results in is a black square in the middle of my screen.
Is there a way to change the opacity of the background at all? Is there anything else I can try to fix this?
Some things I heard of were to take a screenshot of the background and display it on the screen via backbuffer, however I can't find any solutions that successfully implement that.
EDIT: Here is the draw method, while rather simple
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin();
{
spriteBatch.Draw(tex, parameters, Color.White );
}
spriteBatch.End();
base.Draw(gameTime);
}
I also found I can change the forms opacity via form.opacity = 0; but that makes the texture transparent as well.
i have a class that draws waveforms of audio. I'm drawing it in OnPaint function. Now i need to draw a line that shows where on the waveform we are at current moment. I can calculate the position but when i try to draw it i need to call Invalidate() and that forces form to redraw all that waveform chart data (a lot of points).
So is there a way to put some transparent element over this one and then call Invalidate() only on that element? i was trying with picture box but no sucess...
//main form
private void timer100ms_Tick(object sender, EventArgs e)
{
customWaveViewer1.currentPosition = (int)((stream.Position / (float)stream.Length) * customWaveViewer1.Width);
customWaveViewer1.overlayLabel.Invalidate(false);
}
//drawing function in my class
private void overlayLabelInvalidate(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
e.Graphics.DrawLine(pen, currentPosition, 0, currentPosition, this.Height);
}
//constructor
public CustomWaveViewer()
{
InitializeComponent();
this.DoubleBuffered = true;
this.PenColor = Color.DodgerBlue;
this.PenWidth = 1;
overlayLabel = new PictureBox();
overlayLabel.Size = new System.Drawing.Size(this.Width, this.Height);
overlayLabel.Location = new Point(this.Left, this.Top);
overlayLabel.Visible = true;
overlayLabel.BackColor=Color.FromArgb(0,0,0,0);
overlayLabel.Paint += new System.Windows.Forms.PaintEventHandler(this.overlayLabelInvalidate);
Controls.Add(overlayLabel);
}
Actually what you are saying is not exactly true.
In the painteventargs there is a rectangle indicating the small portion of the window that needs to be repainted.
Also when you invalidate, you don't necessarily need to invalidate the whole form.
In your case you might want to invalidate only the old and new position of the marker that indicates where you are in the waveform.
So in your algorithm of your paint method, it is really up to you to make it efficient and only paint that part of the window that really needs repainting and to skip the part that does not need repainting.
It really can make a huge difference.
To make it even more look professional, set double buffering on.
No need to hastle with bitmaps of the whole image you have yourself, that is just what double buffering is all about, and forms can do it for you.
I copied following excerpt from https://msdn.microsoft.com/en-us/library/windows/desktop/dd145137(v=vs.85).aspx
BeginPaint fills a PAINTSTRUCT structure with information such as the dimensions of the portion of the window to be updated and a flag indicating whether the window background has been drawn. The application can use this information to optimize drawing. For example, it can use the dimensions of the update region, specified by the rcPaint member, to limit drawing to only those portions of the window that need updating. If an application has very simple output, it can ignore the update region and draw in the entire window, relying on the system to discard (clip) any unneeded output. Because the system clips drawing that extends outside the clipping region, only drawing that is in the update region is visible.
In this case there is no simple output and taking this into account is adviced.
I am not saying that creating a bitmap will not work. I am saying that optimizing your drawing logic will solve it just as well.
The information above still stands as windows forms is built on top of the old win32 api.
I’m working on a little overlay for Diablo 3 (For personal use only!)
I want just to draw a Text string (We’ll see later for font) in the middle of the screen.
But with XNA I cannot find how to put background to transparent…
My code so far is :
GraphicsDevice.Clear(new Color(0, 0, 0, 255));
spriteBatch.Begin();
spriteBatch.DrawString(font, this.TestToShow, new Vector2(23, 23), Color.White);
spriteBatch.End();
So I only need 1 thing: make this black transparent!
You do not seem to understand what GraphicsDevice.Clear(Color) does.
XNA opens a Windows Window and draws with DirectX into it.
GraphicsDevice.Clear(Color) clears the buffer drawn with DirectX, but doesn't have anything to do with the window.
To make the window transparent, you have to modify the underlaying window.
To do that you have to first add references to System.WIndows.Forms and System.Drawing.
In the Constructor of your Game1 class, you do the following:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IntPtr hWnd = Window.Handle;
System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);
System.Windows.Forms.Form form = ctrl.FindForm();
form.TransparencyKey = System.Drawing.Color.Black;
}
Let's go through this line by line:
Well, the first two are auto generated and we don't care about these.
IntPtr hWnd = Window.Handle;
This line gets you the pointer to the underlaying Window that is registered in Windows.
System.Windows.Forms.Control ctrl = System.Windows.Forms.Control.FromHandle(hWnd);
This line gets the WindowsForms-Control in the given Window.
System.Windows.Forms.Form form = ctrl.FindForm();
This line gets you the Form to which the Control belongs to.
form.TransparencyKey = System.Drawing.Color.Black;
This last line sets the key-Color, that identifies one single Color-value to not be drawn at all. I used Black, but you could also choose CornflowerBlue.
This makes your window internally transparent for that Color. I suggest you should choose the same Color as your clear-Color.
Two things to note:
Best practice is to cache your Form so you can set the TransparencyKey from anywhere.
You can also make your Window borderless this way:
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Hope I could help.
Edit:
I just realized this was asked years ago and had no answer. So feel free to use this, if you stumble upon it.
I have been working on creating a program similar to MS Paint. I have several of the features it has down but the one which is currently giving me trouble is the rectangular selection tool. My program currently draws everything on the panel and saves it all in an ArrayList so each shape can be redrawn in Paint().
Like MS paint I would like the user to be able to select a section of the drawing on the panel and either copy it, move it, re-size it, or even delete it. I was thinking about having the user draw a rectangle & saving the information for it. Then taking that information for the rectangle, passing them to create a new Bitmap. I would then paint a new rectangle in the background color to give the appearance that the selected area was "removed" when the selected portion is moved. It sounded okay until I realized that I couldn't use the Graphics.FromImage() on the PaintEventArgs variable passed to Paint() which made my idea useless. Not sure if that makes sense so my apologies if it's a confusing mess.
I've been searching the internet for some assistance and I haven't found much to help so either this is very easy to do, very difficult, or "rectangle selection tool" is not the proper term. Any help or pointers would be greatly appreciated!!! Thank you for your time! :)
I understand that you actually have the Rectangle and now would like to copy an area from your painted Panel.
This is possible, assuming you have, as you should, placed all the painting in the Paint event of the Panel.
Then you can, use DrawToBitmap to ask the Panel to draw itself onto a new Bitmap; from there you can DrawImage the Rectangle onto your Panel.
Note: For this to integrate with your list of 'Paint-Actions' you will have to either now store that Bitmap or store the Rectangle and redo the whole operation.
using (Graphics G = panelCanvas.CreateGraphics() )
{
Rectangle R0 = new Rectangle(22,22,55,55); // your Rectangle!
using (Bitmap bmp = new
Bitmap(panelCanvas.ClientSize.Width, panelCanvas.ClientSize.Height))
{ panelCanvas.DrawToBitmap(bmp, panelCanvas.ClientRectangle);
G.DrawImage(bmp, 111f, 111f, R0, GraphicsUnit.Pixel);
}
}
Aside: Please do replace the ArrayList, which is depracated by the new List<T>, e.g. a List<PaintAction> or whatever name your class has!
If you simply want to extract a rectanglular area from the Panel Control you can use thsi function:
public Bitmap getAreaFrom(Control ctl, Rectangle area)
{
Bitmap bmp2 = new Bitmap(area.Width, area.Height);
using (Graphics G = ctl.CreateGraphics())
using (Bitmap bmp = new Bitmap(ctl.ClientSize.Width, ctl.ClientSize.Height))
{
ctl.DrawToBitmap(bmp, ctl.ClientRectangle);
using (Graphics G2 = Graphics.FromImage(bmp2))
G2.DrawImage(bmp, 0f, 0f, area, GraphicsUnit.Pixel);
}
return bmp2;
}
I am trying to draw a series of rectangles on my Windows Form application in C#. I am using System.Drawing.Graphics to draw the Rectangles. They work fine, but once I switch to another application on my computer or minimize the form, they just disappear. Does anyone know why this is the case?
System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
25 + (32 * PASS_THROUGH), 190, 32, 32);
graphics.DrawRectangle(System.Drawing.Pens.Green, rectangle);
You're not going about painting the right way. Here's some basic information on how it works:
http://msdn.microsoft.com/en-us/library/kxys6ytf.aspx
You should have code that looks like this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle = new Rectangle(25 + (32 * PASS_THROUGH), 190, 32, 32);
e.Graphics.DrawRectangle(Pens.Green, Rectangle);
}
Windows will call this method whenever it needs to repaint your window.
If you want to be able to change what is painted dynamically, you need to add logic to this method. Such as an if statement that writes, if (drawRectangle) ...
When you want to signal your control to repaint itself after changing a variable like my above example of drawRectangle, you just need to call the Control.Invalidate method on the control in question.
You can manage a lot of different variables and objects to control what is painted, such as a list of shapes. In your paint method you would then loop through those shapes and draw them one by one. I am not sure if this is what you're trying to do, or if you just want to customize the look of your form and you don't need it to change dynamically.