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

问题描述

我目前正在尝试学习如何使用 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,NULL,COLOR_BACKGROUND,_class

msg MSG

hdc dd ?
paintstruct PAINTSTRUCT

.code

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

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

error:
    invoke  MessageBox,_error,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,paintstruct 
    mov [hdc],eax  
    invoke MoveToEx,[hdc],10,15,0  
    invoke LineTo,200,100  
    invoke EndPaint,paintstruct  

.finish:
    ret
endp

.end start

这似乎也不起作用,除非我试图将“hBrush HBRUSH”放在错误的位置(在“.data”、“非法指令”中不起作用;“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,32,0
    invoke LineTo,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,NULL,COLOR_BACKGROUND,_class

msg MSG

hdc dd ?
paintstruct PAINTSTRUCT

.code

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

error:
    invoke  MessageBox,_error,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,paintstruct
    mov [hdc],eax
    invoke CreatePen,0x000000
    invoke SelectObject,32
    invoke EndPaint,paintstruct  

.finish:
    ret
endp

.end start

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...