Search code examples
windowswinapiassemblymasm

masm assembly how to use getpixel to build a color picker


I would like to build a color picker. I have tried this code

invoke GetDC,NULL
mov esi,eax
invoke GetPixel,esi,400,400
invoke lstrcpy,string ,eax
invoke SetDlgItemText,hWin,textbox1,string
invoke ReleaseDC,NULL,esi

but it returns P»© and things like that. how do I get it to return things like 00F0F0F0h


Solution

  • You are not trying to format a string but a number, you need to pass the correct flags and specifier to wsprintf. What *printf does is format whatever you pass to it, and it will convert to a string according to your format specifier and put that string into the address passed to lpOut.

    The %s specifier is for formatting strings. Lets say I wanted to display the return value of GetPixel as an 8 digit hex number with 0x in front of the number.

    .data
    szFmt       db  "%#08x", 0
    
    .data?
    Buf         db  12 dup (?)
    
    .code
        invoke  GetDC, NULL
        invoke  GetPixel, eax, 200, 200
    
        invoke  wsprintf, offset Buf, offset szFmt, eax
        invoke  MessageBox, NULL, offset Buf, NULL, MB_OK
    

    Instead of calling MessageBox, you can do:

    invoke  SetDlgItemText, hWin, textbox1, offset Buf
    

    Try that and see what MessageBox displays

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms647550(v=vs.85).aspx