Issues with Graphics in WindowsForm

Graphics not displaying correctly or at all.

Page 1 of 1

6 Replies - 3885 Views - Last Post: 07 July 2009 - 06:35 AM Rate Topic: -----

#1 brennydoogles   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Issues with Graphics in WindowsForm

Posted 23 June 2009 - 12:36 PM

Hello, I am working on a project for school where I have to create a Tic-Tac-Toe game, and I am having some issues with Graphics. My program starts with a splash screen (a windows form with no borders and a large banner for the background image), and I would like to be able to display versioning information on this form. I initially added a label to the form and set the text in the SplashForm_Load() function, but the entire label area was just a blank white box. I decided to just add the text using graphics with this code:
  private void SplashForm_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			SolidBrush brush = new SolidBrush(System.Drawing.Color.Orange);
			g.DrawString(
				String.Format("Version {0} by {1}.", Application.ProductVersion, Application.CompanyName),
				new System.Drawing.Font("Verdana", 10F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0))),
				brush,
				new System.Drawing.Point(478, 180));
			//this.Invalidate();
		}

, but now nothing shows up in that area. I suspect that it could be some kind of issue with Z-ordering, but I can't find a way to edit Z-order in C#. My second problem which is related has to do with allowing items painted to the screen to overlap (in front of) several windows forms buttons. Here is the code I am attempting to use to draw the tic-tac-toe board overlapping the buttons:
private void MainForm_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			Pen board = new Pen(new SolidBrush(Color.Black), 20);
			board.StartCap = LineCap.Round;
			board.EndCap = LineCap.Round;
			if (turnCounter % 2 == 0)
			{
				currentPlayer = 'o';
				this.Cursor = oCursor;
			}
			else
			{
				currentPlayer = 'x';
				this.Cursor = xCursor;
			}
			int thirdOfWidth = ClientRectangle.Width / 3;
			int thirdOfHeight = ClientRectangle.Height / 3;
			g.DrawLine(board, new Point(thirdOfWidth , 5), new Point(thirdOfWidth, ClientRectangle.Height - 5));
			g.DrawLine(board, new Point(thirdOfWidth*2, 5), new Point(thirdOfWidth*2, ClientRectangle.Height - 5));
			g.DrawLine(board, new Point(5, thirdOfHeight), new Point(ClientRectangle.Width - 5, thirdOfHeight));
			g.DrawLine(board, new Point(5, thirdOfHeight * 2), new Point(ClientRectangle.Width - 5, thirdOfHeight*2 ));
		}

Am I doing something wrong, or is there something I'm missing? Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with Graphics in WindowsForm

#2 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Issues with Graphics in WindowsForm

Posted 23 June 2009 - 01:19 PM

I just tested the code for your splash screen by creating a new windows form project, and adding the code to the default form's paint event. When I tested it, using your code exactly, it worked fine.

Now, I know this may sound like a silly question, but...have you made sure that the text is actually being drawn on your form?

Are there any other objects on the form, such as an image? If so, make sure that the text is not behind it.

Also, what IDE are you using? If you are using Visual Studio, why not just setup the form in the designer?

*EDIT*As far as drawing graphics on top of controls...I'm not sure this can be done. There is no z-order on graphics, it is drawn on the "canvas" of the control. You may be able to simulate this by drawing the board onto a custom control, and sending that control to the front of the z-order. *EDIT*

This post has been edited by lesPaul456: 23 June 2009 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 brennydoogles   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: Issues with Graphics in WindowsForm

Posted 23 June 2009 - 04:06 PM

View PostlesPaul456, on 23 Jun, 2009 - 12:19 PM, said:

Now, I know this may sound like a silly question, but...have you made sure that the text is actually being drawn on your form?


Well, that's not a silly question. I moved from Java to C# about two weeks ago for a class, so I am still a little clumsy in my use of Visual Studio. I am not sure exactly how to go about doing that, and the answer to the next question is why:

View PostlesPaul456, on 23 Jun, 2009 - 12:19 PM, said:

Are there any other objects on the form, such as an image? If so, make sure that the text is not behind it.

The SplashForm has an image as the background of the form, and a progress bar on the very bottom. Since the image is set to the backgroundImage attribute of the form, I have no idea how to see if the text is behind it or even being drawn (note, my professor couldn't figure out what was going on either).

View PostlesPaul456, on 23 Jun, 2009 - 12:19 PM, said:

Also, what IDE are you using? If you are using Visual Studio, why not just setup the form in the designer?

I am using Microsoft Visual Studio Team System 2008 Team Suite (woohoo for the MSDNA!). As for doing it in the form designer, initially I did. When I initially set up the form in the form designer I used a label and positioned it as I wanted, and then set the text to correct version information through the AssemblyInfo.cs file. When I did that, the label showed up as a white box in the exact dimensions of the text. I figured it was some issue with transparency, so I changed the background color to no avail. When that didn't work I turned to the Graphics class.

View PostlesPaul456, on 23 Jun, 2009 - 12:19 PM, said:

*EDIT*As far as drawing graphics on top of controls...I'm not sure this can be done. There is no z-order on graphics, it is drawn on the "canvas" of the control. You may be able to simulate this by drawing the board onto a custom control, and sending that control to the front of the z-order. *EDIT*

If that's what I have to do I am all for it, but I may need some guidance as I am still very new to the specifics of C#.


I have attached the entire project folder from visual studio in order to give you all the opportunity to review my code and see if there is a simple fix. Don't be harsh on my code, it is far from complete, not well commented, and was written by someone who didn't know C# two weeks ago. Thanks in advance for your help!

:EDIT: Ok, so I can't upload a .rar file to this site, but I uploaded it to this folder of my mediafire account http://www.mediafire...271ce49816d50b1 , it's called DuganTicTacToe.rar :EDIT:
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Issues with Graphics in WindowsForm

Posted 23 June 2009 - 05:19 PM

Okay, I looked at your code and I found the problem.

In your SplashForm_Shown event, you show your code, update the progess bar and then close the form. Here's the problem, each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue.

Basically, your form will never repaint.

Here's the solution. At the top of the SplashForm_Shown event, you need to add this line of code:

Application.DoEvents();



When you call this, you are allowing your form to handle other events.

If you add this code, you do not need to draw the text. Instead you can use a label (or labels).

Hope this helps! Feel free to ask any other questions you may have.

This post has been edited by lesPaul456: 23 June 2009 - 05:20 PM

Was This Post Helpful? 1
  • +
  • -

#5 brennydoogles   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: Issues with Graphics in WindowsForm

Posted 23 June 2009 - 06:46 PM

Thank you! That was very helpful! Since one of the things we are demonstrating in this assignment is the ability to work with the Graphics class I went ahead and kept the text the way it was rather than making it a label, just for brownie points from the professor. By the way lesPaul456, I thought this was fair to put in my code for my professor:
Application.DoEvents(); /* I posted about the problem I was having at dreamincode.net,
						 * and this solution was posted by the user lesPaul456. Just wanted to give credit where it is due! */
progressBar.Maximum = 6;
...




The only issue I am still having is trying to figure out how to draw graphics overlapping some buttons on the form. You had mentioned earlier that it might be possible to create a custom control to draw onto, and then place that in front. Which property do I need to set in order to change the Z-index of an element? If I could find this information I could probably come up with some usable hack. Any ideas?

This post has been edited by brennydoogles: 23 June 2009 - 06:48 PM

Was This Post Helpful? 0
  • +
  • -

#6 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Issues with Graphics in WindowsForm

Posted 23 June 2009 - 07:09 PM

Since you are using Visual Studio, you can create a a UserControl and then add your code to that controls Paint event.
Then, add the control to your main form.

To send the comtrol to the front of the z-order, just add this code:

userControl1.BringToFront();



Hope this helps!

This post has been edited by lesPaul456: 23 June 2009 - 07:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 brennydoogles   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 59
  • Joined: 23-June 09

Re: Issues with Graphics in WindowsForm

Posted 07 July 2009 - 06:35 AM

View PostlesPaul456, on 23 Jun, 2009 - 06:09 PM, said:

Since you are using Visual Studio, you can create a a UserControl and then add your code to that controls Paint event.
Then, add the control to your main form.


That didn't end up solving the problem, but I was able to find a slightly different solution to the problem. By expanding the spacing between the buttons and then widening the drop shadow on the background, I was able to create a greater depth to the form, which (kinda) made the drawing look like it was above the buttons. Thanks so much for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1