The MASM Forum

General => The Campus => Topic started by: zedd151 on September 21, 2022, 01:48:18 AM

Title: Drawing lines?
Post by: zedd151 on September 21, 2022, 01:48:18 AM
Yes I know I can search MSDN but need to hear from someone with practical experience drawing lines.  :smiley:


If I understand correctly:
Call CreatePen to create pen object for the drawing of lines.
Then use MoveToEx for the start position of a line?
Then use LineTo for the end position?
For right angle, use that LineTo position as position for next MoveToEx call? Or does x/y have to be incremented?
Seems simple enough if not very verbose coding. I've used these functions a long time ago, but don't remember if their are any quirks with drawing lines. Iirc there were some oddities.
Title: Re: Drawing lines?
Post by: hutch-- on September 21, 2022, 01:53:25 AM
This is also vague memory, MoveToEx followed by more than one LineTo. All X Y coordinates.
Title: Re: Drawing lines?
Post by: jj2007 on September 21, 2022, 01:55:26 AM
Call CreatePen to create pen object for the drawing of lines.
Use invoke SelectObject, hDC, hPen
Then use MoveToEx for the start position of a line?
Then use LineTo for the end position?
For right angle, use that LineTo position as position for next MoveToEx call? Or does x/y have to be incremented?

For a right angle (and any other angle), you need to provide the next point.

If you have many lines, you might use a macro, used as _LineTo x, y:

Code: [Select]
_LineTo MACRO argX, argY
push argX
push argY
call _xLineTo
endm
_xLineTo:
invoke LineTo, hDC, [esp+12], [esp+4]
retn 8

Check also PolyLine
Title: Re: Drawing lines?
Post by: zedd151 on September 21, 2022, 02:05:15 AM
Okay. So, the end point of a line wouldn't be the exact starting point for next line at right angle then. (would be missing a pixel)
I think I'll just stick with drawing a rectangle for a black background then and let the drawing of the cells take care of making it look like a grid. Otherwise way to much code involved in drawing lines to create a grid. Would be a nice coding exercise, but too much code for the purpose that I had intended. Thanks.

Quote from: hutch--
This is also vague memory, MoveToEx followed by more than one LineTo. All X Y coordinates.
So for a right angle:
MoveToEx --> LineTo  --> LineTo (after x, y adjustment)

for a box:
MoveToEx --> LineTo --> LineTo --> LineTo --> LineTo?  (after x, y adjustment between each call)

x,y coordinates not shown, but inferred above. :tongue: I think I've got the basic gist of it.
Title: Re: Drawing lines?
Post by: TimoVJL on September 21, 2022, 02:33:51 AM
It's easy to make grid with x and y in loops.
Title: Re: Drawing lines?
Post by: Vortex on September 21, 2022, 03:31:12 AM
Hi swordfish,

The masm32 library provides a function to draw lines :

Quote
line proc hndl:DWORD,colr:DWORD,x1:DWORD,y1:DWORD,x2:DWORD,y2:DWORD

Description

line will display a line between the first X,Y co-ordinate and the second X,Y co-ordinate.

Parameters

1. hndl The handle of the window where the line will be displayed.
2. colr The RGB COLORREF value for the colour of the line.
3/4 x1, y1 The X, Y co-ordinates of the start of the line.
5/6 x2, y2 The X, Y co-ordinates of the end of the line.

A quick example :

Code: [Select]
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc
include     \masm32\include\gdi32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib
includelib  \masm32\lib\gdi32.lib

DlgProc     PROTO :DWORD,:DWORD,:DWORD,:DWORD

.data

DlgBox      db 1,0,255,255,0,0,0,0,0,0,0,0,64,0,207,16
            db 0,0,6,0,6,0,147,0,136,0,0,0,0,0,71,0
            db 114,0,97,0,112,0,104,0,0,0,8,0,0,0,0,0
            db 77,0,83,0,32,0,83,0,65,0,78,0,83,0,32,0
            db 83,0,69,0,82,0,73,0,70,0,0,0

.data?

hInstance   dd ?
hDC         dd ?

.code

start:

    invoke  GetModuleHandle,NULL
    mov     hInstance,eax
   
    xor     ecx,ecx
    invoke  DialogBoxIndirectParam,hInstance,\
            ADDR DlgBox,ecx,ADDR DlgProc,ecx
           
    invoke  ExitProcess,0

DlgProc PROC uses esi ebx hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

LOCAL ps:PAINTSTRUCT

    .IF uMsg==WM_CLOSE

        invoke  EndDialog,hWnd,NULL

    .ELSEIF uMsg==WM_PAINT

        lea     eax,ps
        invoke  BeginPaint,hWnd,eax
        mov     hDC,eax

        xor     esi,esi
        mov     ebx,200
@@:       
        invoke  line,hWnd,esi,\
                esi,esi,ebx,200
               
        sub     ebx,2
        jnz     @b
        invoke  EndPaint,hWnd,ADDR ps
       
    .ELSE
   
        xor     eax,eax
        ret
       
    .ENDIF
   
    mov eax,TRUE
    ret

DlgProc ENDP

END start
Title: Re: Drawing lines?
Post by: NoCforMe on September 21, 2022, 04:26:22 AM
Okay. So, the end point of a line wouldn't be the exact starting point for next line at right angle then. (would be missing a pixel)

No, that's not correct: if you keep on doing LineTo()s, you'll draw a contiguous line with no breaks.

You might be thinking of the "fencepost problem" with RECTs and other objects which don't include the right and bottom borders (width-1/height-1). But LineTo() will always join up perfectly from the previous position.

Try it.
Title: Re: Drawing lines?
Post by: zedd151 on September 21, 2022, 05:08:03 AM
Ok guys, thanks for the replies. I'll keep these hints for future use, if it ever comes up.
Title: Re: Drawing lines?
Post by: NoCforMe on September 21, 2022, 05:18:49 AM
Oh, it'll come up. Stuff always comes up. (Hopefully not your last meal ...)