asm中的SetTextColor

时间:2018-12-22 14:15:20

标签: winapi assembly gdi

我是ASM的新手,我尝试过更改外部颜色。.但不知道该怎么做。 在这里我尝试过:

    format PE GUI 4.0

    entry win_main

    include 'win32a.inc'

    ; -------------------------------------------------------------------------------------

    section '.code' code readable executable

    win_main:

        ; initialise the members of the wcex structure

        ; --------------------------------------------

        ; WNDCLASSEX

        ; UINT cbSize
        ; UINT style
        ; WNDPROC lpfnWndProc
        ; int cbClsExtra
        ; int cbWndExtra
        ; HINSTANCE hInstance
        ; HICON hIcon
        ; HCURSOR hCursor
        ; HBRUSH hbrBackground
        ; LPCTSTR lpszMenuName
        ; LPCTSTR lpszClassName
        ; HICON hIconSm

        ; --------------------------------------------

        ; the instance handle

        invoke GetModuleHandle,0

        mov [wcex.hInstance],eax

        ; cbSize

        mov eax,sizeof.WNDCLASSEX

        mov [wcex.cbSize],eax

        ; the windows proc

        mov [wcex.lpfnWndProc],WndProc

        ; the windows style

        mov [wcex.style],CS_HREDRAW+CS_VREDRAW

        ; load the icons

        invoke LoadIcon,[wcex.hInstance],IDI_APPLICATION

        mov [wcex.hIcon],eax

        mov [wcex.hIconSm],eax

        ; load the cursor

        invoke LoadCursor,NULL,IDC_ARROW

        mov [wcex.hCursor],eax

        ; the brush for the background

        mov [wcex.hbrBackground],COLOR_WINDOW+1

        ; the windows class name

        mov dword [wcex.lpszClassName],szClass

        ; set to NULL

        mov [wcex.cbClsExtra],0

        mov [wcex.cbWndExtra],0

        mov dword [wcex.lpszMenuName],NULL

        ; register wcex

        ; RegisterClassEx(&wcex)

        invoke RegisterClassEx,wcex

        test eax,eax

        jz reg_error

        ; create the window

        invoke CreateWindowEx,0,szClass,szTitle,WS_OVERLAPPEDWINDOW,\
                              CW_USEDEFAULT,CW_USEDEFAULT,\
                              1000,500,NULL,NULL,[wcex.hInstance],NULL

        test eax,eax

        jz cr_error

        mov [h_wnd],eax

        ; show and update the window

        ; ShowWindow(hWnd,SW_SHOWNORMAL)

        invoke ShowWindow,[h_wnd],SW_SHOWNORMAL

        ; UpdateWindow(hWnd)

        invoke UpdateWindow,[h_wnd]

    msg_loop:

        ; the main message loop

        ; GetMessage(&msg,NULL,0,0)

        invoke GetMessage,msg,NULL,0,0

        cmp eax,1

        jb exit

        jne msg_loop

        ; TranslateMessage(&msg)

        invoke TranslateMessage,msg

        ; DispatchMessage(&msg)

        invoke DispatchMessage,msg

        jmp msg_loop

  reg_error:

        invoke MessageBox,NULL,szRegError,szTitle,MB_ICONERROR+MB_OK

        jmp exit

  cr_error:

        invoke MessageBox,NULL,szCreateError,szTitle,MB_ICONERROR+MB_OK

  exit:

        ; return msg.wParam

        invoke  ExitProcess,[msg.wParam]

; -------------------------------------------------------------------------------------

proc WndProc uses ebx esi edi,hwnd,wmsg,wparam,lparam

  ; WndProc(hwnd,wmsg,wparam,lparam)

  ; callback function to process messages for the main window

        cmp [wmsg],WM_PAINT

        je .PAINT

        cmp [wmsg],WM_DESTROY

        je .DESTROY

  .DEFAULT:

        ; DefWindowProc(hWnd,message,wParam,lParam)

        invoke DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]

        jmp .DONE

  .PAINT:

        ; hdc = BeginPaint(hWnd,&ps)

        invoke BeginPaint,[hwnd],ps

        mov [hdc],eax

        ; TextOut(hdc,5,5,greeting,len(greeting))

        invoke TextOut,[hdc],7,5,szGreeting,30
        ***SetTextColor,100,100,100***


        ; EndPaint(hWnd,&ps)

        invoke EndPaint,[hwnd],ps

        jmp .DONE

  .DESTROY:

        ; PostQuitMessage(0)

        invoke PostQuitMessage,0

        xor eax,eax

  .DONE:

        ret

endp

; -------------------------------------------------------------------------------------

section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL',\
          gdi,'GDI32.DLL'

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess'

  import user,\
         RegisterClassEx,'RegisterClassExA',\
         CreateWindowEx,'CreateWindowExA',\
         ShowWindow,'ShowWindow',\
         UpdateWindow,'UpdateWindow',\
         GetMessage,'GetMessageA',\
         TranslateMessage,'TranslateMessage',\
         DispatchMessage,'DispatchMessageA',\
         MessageBox,'MessageBoxA',\
         DefWindowProc,'DefWindowProcA',\
         BeginPaint,'BeginPaint',\
         EndPaint,'EndPaint',\
         PostQuitMessage,'PostQuitMessage',\
         LoadIcon,'LoadIconA',\
         LoadCursor,'LoadCursorA'

  import gdi,\
         TextOut,'TextOutA'

; -------------------------------------------------------------------------------------

section '.data' readable writeable

  szClass TCHAR "Win32app",0

  szTitle TCHAR "Head",0 

  szGreeting TCHAR "1.Hello World! 2.Hello World! 3.Hello World! 4.Hello World! 5.Hello World! 6.Hello World! 7.Hello World!",0

  szRegError TCHAR "Call to RegisterClassEx failed!",0

  szCreateError TCHAR "Call to CreateWindowEx failed!",0

  wcex WNDCLASSEX

  ps PAINTSTRUCT

  msg MSG

  h_wnd dd 0

  hdc dd 0

; ------------------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

这是设置文本颜色的方式

.PAINT:
    invoke BeginPaint,[hwnd],ps
    mov [hdc],eax
    invoke SetTextColor, [hdc], 0xff10e0
    invoke TextOut,[hdc],7,5,szGreeting,30
    invoke EndPaint,[hdc],ps
    jmp .DONE

我对 FF10E0 所做的工作是将RGB值传递给函数。这是一个过于简单的示例,我绝不主张这样做是最好的选择,但是它确实解决了您关于如何更改文本颜色的问题。

您的下一个问题是弄清楚为什么文本没有显示在第7行和第5列,但是您可能已经弄清楚了。

答案 1 :(得分:0)

感谢Shift_Left的评论和回答。我忘了申报进口图书馆的事情;

 import gdi,\
     TextOut,'TextOutA', SetTextColor,'SetTextColor'