Please help

i'm trying to write a small paint program, like mspaint.

all i want is to be able to click and draw pixels, i don't need to draw lines,circles,ect.

Also to be able to zoom in and out to see the pixels easily, and also a grid.

Can anyone please help, or know of any sites?

Comments

  • : i'm trying to write a small paint program, like mspaint.
    :
    : all i want is to be able to click and draw pixels, i don't need to draw lines,circles,ect.
    :
    : Also to be able to zoom in and out to see the pixels easily, and also a grid.
    :
    : Can anyone please help, or know of any sites?
    :
    A non-zooming paint program is quite simple to create. Here is some code to get you started:
    [code]
    procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
    Y: Integer);
    begin
    if ssLeft in Shift then
    PaintBox1.Canvas.Pixels[X, Y] := clWhite;
    end;
    [/code]
    This code is the most basic of all, but it can be expanded to include scrolling of the paint area (adding an off-set to X,Y based on scrollbar values), and of course colors. This code allows you to draw by dragging, instead of clicking each individual pixel.
    Zooming means that you'll have to place an invisible canvas in memory to hold the original image. Then you can use the PaintBox.Canvas to show the zoomed image. That still requires some maths to factor in the zoom. This should also reduce flickering, since only the pixel drawn is changed. You could also use a TBitmap in the memory and call StretchDraw() to handle the zoom, but that might increase the flickering.
    I hope this helps you to get started.
  • I figured out on way of 'Zooming', all i do is increase the height and width by *2 (i used var zoom) everytime the zoom button is pressed.

    I use paintbox.canvas.Pixels[x div zoom,y div zoom]:=clblack;
    to paint a pixel at the cursor.

    I don't know if this is a good way of zooming but it seems to work.

    But i'm trying to make a grid now. I experimented with a loop that paints lines 1 pixels apart, but that only draws a grid on the paintbox, i want the grid to be between the pixels. I hope that makes sense. (open up mspaint zoom right in and turn grid on, see how the grid is always one pixel no matter how far you zoom in, and between the pixels when you draw.)

    Any ideas?

    Is there any property, or function in a tpaintbox,timage for a grid?
  • : I figured out on way of 'Zooming', all i do is increase the height and width by *2 (i used var zoom) everytime the zoom button is pressed.
    :
    : I use paintbox.canvas.Pixels[x div zoom,y div zoom]:=clblack;
    : to paint a pixel at the cursor.
    :
    : I don't know if this is a good way of zooming but it seems to work.
    :
    : But i'm trying to make a grid now. I experimented with a loop that paints lines 1 pixels apart, but that only draws a grid on the paintbox, i want the grid to be between the pixels. I hope that makes sense. (open up mspaint zoom right in and turn grid on, see how the grid is always one pixel no matter how far you zoom in, and between the pixels when you draw.)
    :
    : Any ideas?
    :
    : Is there any property, or function in a tpaintbox,timage for a grid?
    :
    Drawing a grid should be quite simple using a for-do loop:
    [code]
    for i := 0 to PaintBox1.Width div zoom do
    begin
    PaintBox1.Canvas.MoveTo(i*zoom, 0);
    PaintBox1.Canvas.LineTo(i*zoom, PaintBox1.Height);
    end;
    [/code]
    This code should give you a partial grid with draw-pixels of zoom-1 screen-pixels. If you want your zoom 2-pixels to be exactly 2 screen-pixels wide, then you need to make your actual zoom factor 3, to accommodate room for 1 grid pixel. You should then also add 1 to the coords of the pixels, to let them fall between the grid-lines.
    This kind of thing is often a trial-and-error path to get the factors and constants right. Hope you can get it to work based on this help.
  • Ok so far everythings working out perfectly, thanx so much zibadian.

    Now i'm trying to create a bounding box, so that i can select some pixels and move,cut,copy.
  • : Ok so far everythings working out perfectly, thanx so much zibadian.
    :
    : Now i'm trying to create a bounding box, so that i can select some pixels and move,cut,copy.
    :
    You can use a TRect to hold the box-coords. Then you can use FrameRect to visually draw the rect on the drawing. The easiest way to copy/cut is to use another canvas as clipboard, but it's not very professional. Using the TClipboard is better, but harder to code, especially if you want to be able to copy/cut to paint or similar programs.
    Move itself isn't much of a problem using CopyRect() to copy the box, and the Rectangle() to fill the old position with the background color.
  • You can use a TRect to hold the box-coords. Then you can use FrameRect to visually draw the rect on the drawing. The easiest way to copy/cut is to use another canvas as clipboard, but it's not very professional. Using the TClipboard is better, but harder to code, especially if you want to be able to copy/cut to paint or similar programs.
    : Move itself isn't much of a problem using CopyRect() to copy the box, and the Rectangle() to fill the old position with the background color.
    :

    Thanx soo much again, i got it working in no time, didn't think it was that easy.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories