如何使用 WinAPI 在 FASM 中绘制彩色线条?

How to draw a colored line in FASM with WinAPI?

我目前正在尝试学习如何使用 FASM,并且我一直在尝试制作一个仅使用 WinAPI 和 GDI 绘制彩色线条的应用程序,但我不知道该怎么做。我找到了这个画线的模板,但是它们总是黑色的。

format PE GUI 4.0

include 'win32wx.inc'

.data

_class TCHAR 'FASMWIN32',0
_title TCHAR 'Win32 program template',0
_error TCHAR 'Startup failed.',0

wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BACKGROUND,NULL,_class

msg MSG

hdc dd ?
paintstruct PAINTSTRUCT

.code

start:
    invoke  GetModuleHandle,0
    mov [wc.hInstance],eax
    invoke  LoadIcon,0,IDI_APPLICATION
    mov [wc.hIcon],eax
    invoke  LoadCursor,0,IDC_ARROW
    mov [wc.hCursor],eax
    invoke CreateSolidBrush, 0xFFFFFF  
    mov [wc.hbrBackground], eax
    invoke  RegisterClass,wc
    test    eax,eax
    jz  error

    invoke  CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_OVERLAPPEDWINDOW,128,128,256,192,NULL,NULL,[wc.hInstance],NULL
    test    eax,eax
    jz  error
   
msg_loop:
    invoke  GetMessage,msg,NULL,0,0
    cmp eax,1
    jb  end_loop
    jne msg_loop
    invoke  TranslateMessage,msg
    invoke  DispatchMessage,msg
    jmp msg_loop

error:
    invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

end_loop:
    invoke  ExitProcess,[msg.wParam]

proc WindowProc uses ebx esi edi, hwnd,wmsg,wparam,lparam
    cmp [wmsg], WM_DESTROY
    je  .wmdestroy
    cmp [wmsg], WM_PAINT
    je .wmpaint

.defwndproc:
    invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
    jmp .finish

.wmdestroy:
    invoke  PostQuitMessage,0
    xor eax,eax
    jmp .finish

.wmpaint:
    invoke BeginPaint, [hwnd], paintstruct 
    mov [hdc], eax  
    invoke MoveToEx, [hdc], 10, 15, 0  
    invoke LineTo, [hdc], 200, 100  
    invoke EndPaint, [hwnd], paintstruct  

.finish:
    ret
endp

.end start

这似乎也不起作用,除非我试图将“hBrush HBRUSH”放在错误的位置(在“.data”中不起作用,'illegal instruction';“HBRUSH hBrush”不起作用也能正常工作)。

hBrush HBRUSH
invoke CreateSolidBrush, 0xFFFFFF
invoke SelectObject [hdc], hBrush

谁能给我一个用 WinAPI 和 GDI 绘制彩色线条的例子吗?

我的错误是我试图用画笔而不是钢笔画线。如果有人有同样的问题,代码是:

    invoke CreatePen,PS_SOLID, 1, 0x000000 ; PS_SOLID is the pen style and 0x000000 is the color
    invoke SelectObject, [hdc], eax
    invoke MoveToEx, [hdc], 0, 32, 0
    invoke LineTo, [hdc], 256, 32   

完整示例:

format PE GUI 4.0

include 'win32wx.inc'

.data

_class TCHAR 'FASMWIN32',0
_title TCHAR 'Win32 program template',0
_error TCHAR 'Startup failed.',0

wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BACKGROUND,NULL,_class

msg MSG

hdc dd ?
paintstruct PAINTSTRUCT

.code

start:
    invoke  GetModuleHandle,0
    mov [wc.hInstance],eax
    invoke  LoadIcon,0,IDI_APPLICATION
    mov [wc.hIcon],eax
    invoke  LoadCursor,0,IDC_ARROW
    mov [wc.hCursor],eax
    invoke CreateSolidBrush, 0xA0A0A0
    mov [wc.hbrBackground], eax
    invoke  RegisterClass,wc
    test    eax,eax
    jz  error
    invoke  CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_OVERLAPPEDWINDOW,128,128,256,192,NULL,NULL,[wc.hInstance],NULL
    test    eax,eax
    jz  error
msg_loop:
    invoke  GetMessage,msg,NULL,0,0
    cmp eax,1
    jb  end_loop
    jne msg_loop
    invoke  TranslateMessage,msg
    invoke  DispatchMessage,msg
    jmp msg_loop

error:
    invoke  MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

end_loop:
    invoke  ExitProcess,[msg.wParam]

proc WindowProc uses ebx esi edi, hwnd,wmsg,wparam,lparam
    cmp [wmsg], WM_DESTROY
    je  .wmdestroy
    cmp [wmsg], WM_PAINT
    je .wmpaint

.defwndproc:
    invoke  DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
    jmp .finish

.wmdestroy:
    invoke  PostQuitMessage,0
    xor eax,eax
    jmp .finish

.wmpaint:
    invoke BeginPaint, [hwnd], paintstruct
    mov [hdc], eax
    invoke CreatePen,PS_SOLID, 1, 0x000000
    invoke SelectObject, [hdc], eax
    invoke MoveToEx, [hdc], 0, 32, 0
    invoke LineTo, [hdc], 256, 32
    invoke EndPaint, [hwnd], paintstruct  

.finish:
    ret
endp

.end start