Рисование в GDI - Assembler

Узнай цену своей работы

Формулировка задачи:

Всем привет! Изучаю ассемблер по книге Владислава Пирогова "Ассемблер для WINDOWS 4-е издание". Там есть пример с рисованием различных объектов по щелчкам мыши. Код я переписал из книжки (скорее всего есть ошибка, которую я не нашел). Процентов 20 из кода мне не понятно, поэтому логически вычислить место с ошибкой не могу. Предполагаю, что в WMCREATE, там много открытий всяких контекстов и создание сомнительных кистей и прочих вещей, которые хз зачем... Ну да ладно - код тут:
.586p
.model flat, stdcall
 
WM_CREATE           equ 1
WM_DESTROY          equ 2
WM_LBUTTONDOWN      equ 201h
WM_PAINT            equ 0Fh
 
CS_VREDRAW          equ 1
CS_HREDRAW          equ 2
 
WS_OVERLAPPEDWINDOW equ 0CF0000h
 
WndStyle            equ CS_VREDRAW + CS_HREDRAW ;WndStyle equ 3
DX0                 equ 600
DY0                 equ 400 ;Window size
 
RGBW                equ (50 or (50 shl 8)) or (255 shl 16) ;window
RGBR                equ 150       ;region
RGBL                equ 0         ;line
RGBP                equ 255 or (100 shl 8)    ;point
 
IDI_APPLICATION     equ 32512
IDC_CROSS           equ 32515
 
SW_SHOWNORMAL       equ 1
 
extern CreateWindowExA@48:near
extern DefWindowProcA@16:near
extern DispatchMessageA@4:near
extern ExitProcess@4:near
extern GetMessageA@16:near
extern GetModuleHandleA@4:near
extern LoadCursorA@8:near
extern LoadIconA@8:near
extern PostQuitMessage@4:near
extern RegisterClassA@4:near
extern ShowWindow@8:near
extern TranslateMessage@4:near
extern UpdateWindow@4:near
extern BeginPaint@8:near
extern EndPaint@8:near
extern GetStockObject@4:near
extern CreateSolidBrush@4:near
extern GetSystemMetrics@4:near
extern GetDC@4:near
extern CreateCompatibleDC@4:near
extern SelectObject@8:near
extern CreateCompatibleBitmap@12:near
extern PatBlt@24:near
extern BitBlt@36:near
extern ReleaseDC@8:near
extern DeleteObject@4:near
extern InvalidateRect@12:near
extern DeleteDC@4:near
extern CreatePen@12:near
extern SetPixel@16:near
extern LineTo@12:near
extern MoveToEx@16:near
extern Rectangle@20:near
 
msgstruct struc
    hWnd            dd ?
    message         dd ?
    wParam          dd ?
    lParam          dd ?
    time            dd ?
    pt              dd ?
msgstruct ends
 
wndclass struc
    style           dd ?
    wndproc         dd ?
    clsextra        dd ?
    wndextra        dd ?
    hInstance       dd ?
    hIcon           dd ?
    hCursor         dd ?
    hBrush          dd ?
    menuname        dd ?
    classname       dd ?
wndclass ends
 
paintstruct struc
    hdc             dd 0 ;Why they all initialized?
    fErase          dd 0
    left            dd 0
    top             dd 0
    right           dd 0
    bottom          dd 0
    fRes            dd 0
    fIncUp          dd 0
    Reserv          db 32 dup(0)
paintstruct ends
 
rect struc
    l               dd ?
    t               dd ?
    r               dd ?
    b               dd ?
rect ends
 
includelib D:\masm32\lib\user32.lib
includelib D:\masm32\lib\kernel32.lib
includelib D:\masm32\lib\gdi32.lib
 
_data segment
    hWnd            dd ?
    msg             msgstruct <?>
    wc              wndclass <?>
    pnt             paintstruct <?>
    hInst           dd ?
    titlename       db 'Графика в окне',0
    nam             db 'closs32',0
    xt              dd 30
    yt              dd 30
    xm              dd ? ;X-Metrics
    ym              dd ? ;Y-Metrics
    hdc             dd ?
    memdc           dd ?
    hPen            dd ?
    hBrush          dd ?
    p               dd 0
    xp              dd ?
    yp              dd ?
_data ends
 
_text segment
start:
    push 0
    call GetModuleHandleA@4
    mov hInst,eax
    
    mov wc.style,WndStyle
    mov wc.wndproc,offset wndproc
    mov wc.clsextra,0
    mov wc.wndextra,0
    mov wc.hInstance,eax
    push IDI_APPLICATION
    push 0
    call LoadIconA@8
    mov wc.hIcon,eax
    push IDC_CROSS
    push 0
    call LoadCursorA@8
    mov wc.hCursor,eax
    push RGBW
    call CreateSolidBrush@4
    mov wc.hBrush,eax
    mov wc.menuname,0
    mov wc.classname,offset nam
    
    push offset wc
    call RegisterClassA@4
    
    push 0
    push hInst
    push 0
    push 0
    push DY0
    push DX0
    push 100
    push 100
    push WS_OVERLAPPEDWINDOW
    push offset titlename
    push offset nam
    push 0
    call CreateWindowExA@48
    cmp eax,0
    je end_loop
    mov hWnd,eax
    
    push SW_SHOWNORMAL
    push eax
    call ShowWindow@8
    
    push eax
    call UpdateWindow@4
    
msg_loop:
    push 0
    push 0
    push 0
    push offset msg
    call GetMessageA@16
    cmp ax,0
    je end_loop
    push offset msg
    call TranslateMessage@4
    push offset msg
    call DispatchMessageA@4
    jmp msg_loop
    
end_loop:
    push msg.wParam
    call ExitProcess@4
    
wndproc proc
    push ebp
    mov ebp,esp
    push ebx
    push esi
    push edi
    
    cmp dword ptr [ebp + 12],WM_DESTROY
    je wmdestroy
    cmp dword ptr [ebp + 12],WM_CREATE
    je wmcreate
    cmp dword ptr [ebp + 12],WM_PAINT
    je wmpaint
    cmp dword ptr [ebp + 12],WM_LBUTTONDOWN
    je wmlbuttondown
    jmp defwndproc
 
wmlbuttondown:
    cmp p,0
    jne f1
;so line (point by point) horizontal
    mov yp,50
    mov xp,10
    mov ecx,200
ll:
    push ecx
    push RGBP
    push yp
    push xp
    push memdc
    call SetPixel@16
    inc xp
    pop ecx
    loop ll
    inc p
    jmp f3
    
f1:
    cmp p,1
    jne f2
    push 0
    push yp
    push xp
    push memdc
    call MoveToEx@16
    
    push 300
    push 550
    push memdc
    call LineTo@12
    inc p
    jmp f3
    
f2:
    cmp p,2
    jne fin
    push hBrush
    push memdc
    call SelectObject@8
    
    push 350
    push 400
    push 200
    push 200
    push memdc
    call Rectangle@20
    inc p
    
f3:
    push 0
    push offset rect
    push dword ptr [ebp + 12]
    call InvalidateRect@12
    
fin:
    mov eax,0
    jmp finish
    
wmpaint:
    push offset pnt
    push dword ptr [ebp + 8]
    call BeginPaint@8
    mov hdc,eax
    
    push 0CC020h    ;SRCCOPY
    push 0
    push 0
    push memdc
    push ym
    push xm
    push 0
    push 0
    push hdc
    call BitBlt@36
    
    push offset pnt
    push dword ptr [ebp + 8]
    call EndPaint@8
    mov eax,0
    jmp finish
    
wmcreate:
    push 0 ;X
    call GetSystemMetrics@4
    mov xm,eax
    push 1 ;Y
    call GetSystemMetrics@4
    mov ym,eax
    
    push dword ptr [ebp + 8]
    call GetDC@4
    mov hdc,eax
    
    push eax
    call CreateCompatibleDC@4
    mov memdc,eax
    
    push ym
    push xm
    push hdc
    call CreateCompatibleBitmap@12
    
    push eax
    push memdc
    call SelectObject@8
    
    push RGBW
    call CreateSolidBrush@4
    
    push eax
    push memdc
    call SelectObject@8
    
    push 0F00021h   ;PATCOPY
    push ym
    push xm
    push 0
    push 0
    push memdc
    call PatBlt@24
    
    push RGBR
    call CreateSolidBrush@4
    mov hBrush,eax
    
    push RGBR
    push 0
    push 0
    call CreatePen@12
    mov hPen,eax
    
    push hdc
    push dword ptr [ebp + 8]
    call ReleaseDC@8
    mov eax,0
    jmp finish
    
defwndproc:
    push dword ptr [ebp + 20]
    push dword ptr [ebp + 16]
    push dword ptr [ebp + 12]
    push dword ptr [ebp + 8]
    call DefWindowProcA@16
    jmp finish
    
wmdestroy:
    push hPen
    call DeleteDC@4
    
    push hBrush
    call DeleteDC@4
    
    push memdc
    call DeleteDC@4
    
    push 0
    call PostQuitMessage@4
    mov eax,0
    
finish:
    pop edi
    pop esi
    pop ebx
    pop ebp
    
    ret 16
wndproc endp
    
_text ends
end start
Компилируется, запускается хорошо. Выводит просто черный экран. Поидее должно по WM_LBUTTONDOWN рисовать рисунки)

Решение задачи: «Рисование в GDI»

textual
Листинг программы
push hWnd

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 4.067 из 5