Obtenir rgb pixel

Description

Bonsoir,

C'est simplement un tooltip qui 'suit' le curseur de la souris et qui indique la couleur, sous la forme RGB, du pixel se trouvant sous le curseur.
Le projet en C est fourni pour que vous puissiez suivre si vous ne comprenez pas tout.

Source / Exemple :


.386
.model flat, stdcall
option casemap:none

include		\masm32\include\windows.inc
include		\masm32\include\user32.inc
include		\masm32\include\kernel32.inc
;include		\masm32\include\masm32.inc
include 	\masm32\include\comctl32.inc
include 	\masm32\include\gdi32.inc
 
includelib	\masm32\lib\user32.lib
includelib	\masm32\lib\kernel32.lib
;includelib	\masm32\lib\masm32.lib
includelib  \masm32\lib\comctl32.lib
includelib  \masm32\lib\gdi32.lib

.data

hInst               dd  0
htt                 dd  0
iWidthScreen        dd  0
TOOLTIPS_CLASS      db  "tooltips_class32", 0
szRGB               db  256 dup(0)

.code

start:
  call  InitCommonControls
  push  0
  call  GetModuleHandle
  mov   hInst, eax
  push  512
  push  GPTR
  call  GlobalAlloc
  push  eax                       ;Param pour GlobalFree
  mov   edx, WS_POPUP 
  or    edx, WS_CAPTION 
  or    edx, WS_SYSMENU
  xor   ecx, ecx
  mov   [eax], edx                    ; style
  mov   word ptr[eax + 10], 0         ; x et y
  mov   word ptr[eax + 12], 0
  mov   word ptr[eax + 14], 50        ; cx et cy
  mov   word ptr[eax + 16], 50
  mov   edx, hInst
  push  ecx
  push  offset DlgProc
  push  ecx
  push  eax
  push  edx
  call  DialogBoxIndirectParam
  call  GlobalFree
  push  0
  call  ExitProcess
  
DlgProc   PROC
  mov   eax, [esp + 8]
  cmp   eax, WM_INITDIALOG
  je    onDialog
  cmp   eax, WM_COMMAND
  je    onCmd
  cmp   eax, WM_NOTIFY
  je    onNotify
  cmp   eax, WM_TIMER
  je    onTimer
  jmp   Fin  
  ;ESP     => hwnd  (HWND)      4  octets
  ;ESP + 4 => htt   (HWND)      4  octets
  ;ESP + 8 => ti    (TOOLINFO)  44 octets
onDialog:
  mov   eax, offset szRGB
  mov   dword ptr[eax], 28424752h ;'RGB('
  mov   ecx, [esp + 4]    ;hwnd
  lea   eax, [esp - 48]
  mov   esp, eax
  mov   [eax], ecx
  xor   eax, eax
  push  eax
  mov   edx, hInst
  push  edx
  push  eax
  push  ecx
  push  CW_USEDEFAULT
  push  CW_USEDEFAULT
  push  CW_USEDEFAULT
  push  CW_USEDEFAULT
  mov   edx, WS_POPUP
  or    edx, TTS_NOPREFIX
  or    edx, TTS_ALWAYSTIP
  push  edx
  push  eax
  push  offset TOOLTIPS_CLASS
  push  WS_EX_TOPMOST
  call  CreateWindowEx
  test  eax, eax
  je    FinOnDialog
  mov   htt, eax
  lea   ecx, [esp + 4]
  mov   dword ptr[ecx], 44    ;ti.cbSize
  mov   edx, TTF_IDISHWND 
  or    edx, TTF_TRACK 
  or    edx, TTF_ABSOLUTE
  mov   [ecx + 4], edx        ;ti.uFlags
  mov   [ecx + 12], eax       ;ti.uID
  mov   edx, hInst      
  mov   [ecx + 32], edx       ;ti.hInst
  mov   edx, [ecx - 4]        ;hwnd (handle de la fenetre)
  mov   [ecx + 8], edx        ;ti.hwnd
  mov   dword ptr[ecx + 36], LPSTR_TEXTCALLBACK ;ti.lpsztext
  push  0
  push  10
  push  1712
  push  edx               ;SetTimer
  push  ecx       
  push  1
  push  TTM_TRACKACTIVATE
  push  eax               ;2eme SendMessage
  push  ecx
  push  0
  push  TTM_ADDTOOL
  push  eax               ;1er SendMessage
  call  SendMessage       
  call  SendMessage
  call  SetTimer
  push  SM_CXSCREEN
  call  GetSystemMetrics
  mov   iWidthScreen, eax
FinOnDialog:
  add   esp, 48
  mov   eax, 1
  ret   16  
  ;ESP      => hwnd     (HWND)    4 octets
  ;ESP + 4  => pt       (POINT)   8 octets
  ;ESP + 12 => hdcScr   (HDC)     4 octets
  ;ESP + 16 => clrPixel (COLORREF)4 octets
  ;ESP + 20 => szRGB    (char *)  20 octets
onTimer:
  mov   eax, [esp + 4]
  lea   ecx, [esp - 40]
  mov   esp, ecx
  mov   [ecx], eax  ;sauve hwnd
  lea   eax, [ecx + 4]
  push  eax
  call  GetCursorPos
  push  0
  call  GetDC
  lea   ecx, [esp + 4]
  mov   [ecx + 8], eax    ;sauve hdcScr
  push  [ecx + 4]
  push  [ecx]
  push  eax
  call  GetPixel
  lea   ecx, [esp]
  mov   [ecx + 16], eax        ;sauve clrPixel
  mov   edx, [ecx + 12]   ;hdcScr
  mov   eax, [ecx]        ;hwnd
  push  edx
  push  eax
  call  ReleaseDC  
  ;'Creation' chaine szRGB
  lea   edx, [esp + 16]   ;clrPixel
  movzx eax, byte ptr[edx]; GetRValue
  mov   ecx, offset szRGB
  add   ecx, 4            ;saute 'RGB('
  call  ultoa
  mov   byte ptr[eax], ','
  add   eax, 1
  lea   edx, [esp + 16]
  mov   ecx, eax
  movzx eax, byte ptr[edx + 1] ;GetGValue
  call  ultoa
  mov   byte ptr[eax], ','
  add   eax, 1
  lea   edx, [esp + 16]
  mov   ecx, eax
  movzx eax, byte ptr[edx + 2] ;GetBValue
  call  ultoa  
  mov   byte ptr[eax], ')'
  add   eax, 1
  mov   byte ptr[eax], 0
  lea   eax, [esp + 4]
  mov   ecx, [eax]        ;pt.x
  mov   edx, [eax + 4]    ;pt.y
  add   ecx, 110
  cmp   ecx, iWidthScreen
  jg    Sub210
  sub   ecx, 100
  jmp   MakeLParam
Sub210:
  sub   ecx, 210
MakeLParam:
  mov   eax, edx
  shl   eax, 16
  or    eax, ecx
  mov   edx, htt
  push  eax
  push  0
  push  TTM_TRACKPOSITION
  push  edx
  call  SendMessage
  xor   eax, eax
  add   esp, 40
  ret   16
onCmd:
  mov   eax, [esp + 12]
  cmp   eax, IDCANCEL
  jne   Fin
  mov   ecx, [esp + 4]
  push  0
  push  ecx        ;hwnd pour EndDialog
  push  1712
  push  ecx
  call  KillTimer
  call  EndDialog
  xor   eax, eax
  ret   16
onNotify:
  mov   eax, [esp + 16]   ; eax == lparam
  mov   ecx, [eax + 8]    ; ecx == lparam->code
  cmp   ecx, TTN_GETDISPINFO
  jne   Fin
  mov   [eax + 12], offset szRGB
Fin:
  xor   eax, eax
  ret   16
DlgProc ENDP
      
;FONCTION BRUNEWS
ultoa PROC ; IN:EAX=dwNbr, ECX=lpsz
  test     eax, eax
	jnz      short L1
	lea      eax, [ecx+1]
	mov      byte ptr[ecx], 48
	mov      byte ptr[eax], 0
	ret      0
L1:
	mov      [esp-4], edi
	mov      [esp-8], ecx
	mov      edi, ecx
	mov      ecx, eax
L2:
 	mov      eax, -858993459
 	mul      ecx
 	mov      eax, edx
 	shr      eax, 3
 	mov      edx, ecx
 	lea      ecx, [eax+eax*8]
 	add      ecx, eax
 	sub      edx, ecx
 	add      dl, 48
 	mov      [edi], dl
 	mov      ecx, eax
 	inc      edi
  test     eax, eax
  jnz      short L2
	mov      [esp-12], edi
  mov      eax, [esp-8]
  mov      byte ptr[edi], 0
L3:
 	dec      edi
 	mov      dl, [eax]
 	mov      cl, [edi]
 	mov      [edi], dl
 	mov      [eax], cl
 	inc      eax
 	cmp      eax, edi
 	jb       short L3
 	mov      eax, [esp-12]
 	mov      edi, [esp-4]
 	ret      0
ultoa ENDP
  
END start

Conclusion :


Je mettrai a jour le code plus tard pour rectifier certains trucs qui ne doivent pas aller.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.