Hi :-)
I am trying to write a code which works with SetPixel to put Pixels on a Control.
I draw a picture and everything is ok. but when the area that I drew goes out of the screen it will disapear.
for solve this problem. I override the WndProc function for WM_PAINT and in this function I do the drawing again in the event.
but it is realy funny, if move the form in a way that the area is out of (or semi out of) the screen.
is there any better solution vs WM_PAINT? or maybe the way is true, my use of that maybe incorrect? :-S
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
PaintArea(this, new EventArgs());
}
}
Loading
Sam HobbsPosted Nov 10, 2010, 2:06 PM
One thing you could do is to draw a bitmap in memory instead of on the form. Use a PictureBox in the form and set the image for the PictureBox to the bitmap you drew in memory. I don't understand your code; it appears that you are setting the imge to all red then al blue so the red gets drawn over. Assuming you want to create a rectangle of all red, and you have in your form a PictureBox called pictureBox1 (the default name) then the following in your form load event might be all you need.
Sam HobbsPosted Nov 11, 2010, 1:51 AM
So since I helped, if you could check the checkbox that says "Do you like the answer" or something like that, then that will help me.
loghman dastgheybPosted Nov 11, 2010, 1:29 AM
and thanks so much :D
you solve my problem. the idea to put that graph on the bitmap was so great.
but about that 2 rectangle that I drew on a same position and size. I did that to show if you resize the form the whole operation start from begining. when you resize the form first the red rect draw and after that blue rect will draw. if I didn't that(draw another rect on same position with diffrent color) the person who run the code may not understand that the operation start from begining. becuase the red rect is available and the person who run the program can't see that the graph is overdrawing again. I just do that to show what would happen if I put the drawing in WndProc or OnPaint event. thiss code is not the project that I work and the real one is about the drawing graphs and that is compilicated which I think if I put that here it may confuse others.
so in answering my question the solution is to use bitmap and draw graph on it and assign it to a PicturBox!
Thank you :D
loghman dastgheybPosted Nov 10, 2010, 11:36 AM
sorry. but I have to put a long code.
as you see in the OnPaint I write a time consuling draw. in my project I draw a graph instead of this which calculating that is realy time consuming. now try to resize your form. the process start from begining. and if don't put this on OnPaint or WM_PAINT in WndProc , if you move the form out of the screen I lose my graph. :/
Is there any other better way to draw on Form,Control, etc instead of SetPixel which don't need to call the WM_PAINT?
(I calculate x,y and in some cases x,y,z of a function and draw it on the form to show the graph. I will be happy if there is another way to punch a point on the form which don't disapear after moveing out of screen.)
//==================================================
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.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
static extern int GetPixel(IntPtr hDC, int x, int y);
[DllImport("gdi32.dll")]
static extern int SetPixel(IntPtr hDC, int x, int y, int color);
static public Color GetPixel(Control control, int x, int y)
{
Color color = Color.Empty;
if (control != null)
{
IntPtr hDC = GetDC(control.Handle);
int colorRef = GetPixel(hDC, x, y);
color = Color.FromArgb(
(int)(colorRef & 0x000000FF),
(int)(colorRef & 0x0000FF00) >> 8,
(int)(colorRef & 0x00FF0000) >> 16);
ReleaseDC(control.Handle, hDC);
}
return color;
}
static public void SetPixel(Control control, int x, int y, Color color)
{
if (control != null)
{
IntPtr hDC = GetDC(control.Handle);
int argb = color.ToArgb();
int colorRef =
(int)((argb & 0x00FF0000) >> 16) |
(int)(argb & 0x0000FF00) |
(int)((argb & 0x000000FF) << 16);
SetPixel(hDC, x, y, colorRef);
ReleaseDC(control.Handle, hDC);
}
}
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs args)
{
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
SetPixel(this, i + 10, 10 + j, Color.Red);
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
SetPixel(this, i + 10, 10 + j, Color.Blue);
}
}
}
Mahesh ChandPosted Nov 10, 2010, 10:09 AM
Adding a Paint Event Handler to a Form and Controls in GDI+
Overriding the OnPaint Method of a Form in GDI+ by Dinesh Beniwal on Jul 09, 2010