Howdy, Stranger!

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

Categories

MoveToEx() and LineTo() functions

Sonx_hvn7Sonx_hvn7 Member Posts: 54
Hi there..

Trying to draw a line on a frame using the MoveToEx and LineTo functions with no success.. Here's my code

[code]

void DrawOnFrame()
{
hwnd = GetFrameWindow();
hdc = GetDC(hwnd);
MoveToEx(hdc, 150, 150, NULL);
LineTo(hdc, 150, 250);
ReleaseDC (hwnd, hdc);
}

// Call the function
DrawOnFrame();
[/code]

Nothing shows on the frame...

Is the a way to change the color of the line? Maybe it defaults to the same color as the frame background

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Hi there..
    :
    : Trying to draw a line on a frame using the MoveToEx and LineTo
    : functions with no success.. Here's my code
    :
    : [code]:
    :
    : void DrawOnFrame()
    : {
    : hwnd = GetFrameWindow();
    : hdc = GetDC(hwnd);
    : MoveToEx(hdc, 150, 150, NULL);
    : LineTo(hdc, 150, 250);
    : ReleaseDC (hwnd, hdc);
    : }
    :
    : // Call the function
    : DrawOnFrame();
    : [/code]:
    :
    : Nothing shows on the frame...
    :
    : Is the a way to change the color of the line? Maybe it defaults to
    : the same color as the frame background
    :
    [color=Blue]
    The drawing should be done only in response to WM_PAINT message and nowhere else. Also, make sure frame does not have a child window covering the whole surface.
    [/color]
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    : : Hi there..
    : :
    : : Trying to draw a line on a frame using the MoveToEx and LineTo
    : : functions with no success.. Here's my code
    : :
    : : [code]: :
    : :
    : : void DrawOnFrame()
    : : {
    : : hwnd = GetFrameWindow();
    : : hdc = GetDC(hwnd);
    : : MoveToEx(hdc, 150, 150, NULL);
    : : LineTo(hdc, 150, 250);
    : : ReleaseDC (hwnd, hdc);
    : : }
    : :
    : : // Call the function
    : : DrawOnFrame();
    : : [/code]: :
    : :
    : : Nothing shows on the frame...
    : :
    : : Is the a way to change the color of the line? Maybe it defaults to
    : : the same color as the frame background
    : :
    : [color=Blue]
    : The drawing should be done only in response to WM_PAINT message and
    : nowhere else. Also, make sure frame does not have a child window
    : covering the whole surface.
    : [/color]


    Does this mean i can't draw a line on the frame without input messages (mouse clicks)? I just want to draw a line from XY point to X1Y1 point, with no use interaction... Can't MoveToEx() & LineTo() do that simple stuff for me?


  • LundinLundin Member Posts: 3,711
    You can do like this example:

    - The user clicks the mouse at coordinates (x, y). You get a WM_LBUTTONDOWN message and from there you save these coordinates in variables.
    - You tell the window that a certain area needs to be redrawn. This can be done with the functions InvalidateRect, InvalidateRgn etc, followed by the function UpdateWindow().
    - This will trigger a WM_PAINT message.
    - From WM_PAINT, draw the lines with MoveToEx and LineTo, using the coordinates x and y as input.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : You can do like this example:
    :
    : - The user clicks the mouse at coordinates (x, y). You get a
    : WM_LBUTTONDOWN message and from there you save these coordinates in
    : variables.
    : - You tell the window that a certain area needs to be redrawn. This
    : can be done with the functions InvalidateRect, InvalidateRgn etc,
    : followed by the function UpdateWindow().
    : - This will trigger a WM_PAINT message.
    : - From WM_PAINT, draw the lines with MoveToEx and LineTo, using the
    : coordinates x and y as input.

    [color=Blue]That is exactly as it should be done. The whole Windows programming is based on the fact, that to draw on the window you must have a data object - a container, which has your data (in this case a bunch of line coordinates). To add lines on screen - simply change your data contents (add or remove lines or change their color) and then trigger WM_PAINT message.

    Drawing (or it can be another type of processing, say dragging a shape) with a mouse involves triggering the same WM_PAINT message, but to draw immediately you must call UpdateWindow() AFTER call to InvalidateRect().

    If drawing lines is not happening for you - maybe you can show us the whole code... maybe some other issue is at fault here?
    [/color]
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    I think i'm not being understood well... Let me explain...

    I would like to draw a line from the center of the circle and then rotate it (A radar is an example... So my first task is to first see if i can at least draw a line, which i'm not winning yet. I don't want to use a mouse to do that but draw in using a function and two points (without listening to messages or mouse events)... Part of the code below is an example i got from the tool i'm using (help file) and i have expanded by using functions and drawing four lines, which all it does is to draw a line on a image (earth).. I'm happy to do that but want the line to be independence of the image DC (drawn on the frame itself)..

    What would i need to change from this code?
    [code]

    [color=Green]// THIS CODE DRAWS FOUR LINES AROUND THE IMAGE[/color]

    void DrawOnImage()
    {
    hBmp = RadarBgrnd.GetHandle();
    hMemDC = CreateCompatibleDC(0);
    hOldBmp = SelectObject(hMemDC, hBmp);
    MoveToEx(hMemDC, coord.x,coord.y, &myPoint);
    LineTo(hMemDC, coord.x+factor.x,coord.y+factor.y);
    SelectObject (hMemDC, hOldBmp);
    DeleteDC(hMemDC);
    RadarBgrnd.Repaint ();
    }

    [color=Green]// Initialize coordinates variables[/color]
    void init_coords(unsigned int x, unsigned int y, unsigned int xf, unsigned int yf)
    {
    coord.x = x;
    coord.y = y;
    factor.x = xf;
    factor.y = yf;
    }

    [color=Green]// Insert four lines to form a border around the image[color]
    for(i=0; i<4; i++)
    {
    [color=Green]// use the "? :" operator to initialize coordinates around the image[/color]
    i<2
    ?
    i%2 ? init_coords(0,0, size,0) : init_coords(0,0, 0, size)
    :
    !(i%2) ? init_coords(399,0,0,size) : init_coords(0,399, size, 0);

    DrawOnImage();
    }
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Well, that's different - you need the action, which involves no human interaction, like a running PC game. This can be done with timers or threads. Simply install a timer for your window, say 250 milliseconds and then change your coordinates and fire InvalidateRect() + UpdateWindow() inside response to a timer. To process it faster - make hMemDC global and call CreateCompatibleDC()/DeleteDC() once outside of DrawImage(), because this function will be called 4 times a second now (250 ms interval for timer). DrawImage() should be called in a response to a WM_PAINT message. To reduce flicker draw in memory and disable erasing the background of the window where drawing is performed.
    [/color]
Sign In or Register to comment.