Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi [nonVCL] Farbe von STATIC bei Mausbewegung ändern (https://www.delphipraxis.net/86237-%5Bnonvcl%5D-farbe-von-static-bei-mausbewegung-aendern.html)

Chris P 11. Feb 2007 14:01


[nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Hi Leute,

mein nonVCL-Programm soll folgendes machen:

Bei einer Mausbewegung soll die Farbe des darunterliegenden Pixels ermittlelt werden.
Soweit kein Problem. Jetzt soll aber die Farbe in einem STATIC-Feld angezeigt werden.
Also so ähnlich wie bei Luckies "WhatColor".

Die Farbe bekomme ich mit
Delphi-Quellcode:
clr := GetPixel(AppDC, pt.x, pt.y);
Und den Pinsel mit
Delphi-Quellcode:
brush := CreateSolidBrush(clr);
Nun habe ich es schon mit SetBKColor versucht aber leider ohne Erfolg.

Muss ich evtl. eine WM_CTLCOLORSTATIC-Nachricht an das Parentfenster schicken?

stefan2005 11. Feb 2007 17:07

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
hi,
ich bin mir jetzt nicht sicher ob du das gemacht hast oder ob das überhaupt stimmt, aber man muss einen erstellten Brush oder Pen doch erst mittels SelectObject aktivieren ?
Vielleicht hast du das vergessen.

Grüsse,
Stefan

Chris P 11. Feb 2007 17:18

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Auch mit SelectObject habe ich es versucht.

Geht leider auch nicht.

Chris P 11. Feb 2007 17:24

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Zur Hilfe nochmal mein Quelltext:

Delphi-Quellcode:
function DlgFunc(hWnd: HWND; uMsg: Cardinal; wParam: WPARAM; lParam: LPARAM): BOOL; stdcall;
var
   pt: TPoint;
   brush: HBRUSH;
begin
   Result := TRUE;
   case uMsg of
   
   ...
   
   WM_MOUSEMOVE:
      begin
        if ((GetCapture = hWnd) and GetCursorPos(pt)) then
        begin

         
           // pixelcolor
          clr := GetPixel(AppDC, pt.x, pt.y);

          dcRect := getdc(GetDlgItem(hwnd, 106));
          brush := CreateSolidBrush(clr);
          SelectObject(dcRect, brush);
         
          SetBkColor(dcRect , clr);
        end;
      end;
    ...
end;

Sunshine 11. Feb 2007 18:44

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Hi,

du must WM_CTLCOLORSTATIC abfangen. SetBkColor funktioniert irgendwie nicht bei Static Controls.

Also ungefähr so:

Delphi-Quellcode:
begin
   Result := TRUE;
   case uMsg of
   
   ...
   
   WM_MOUSEMOVE:
      begin
        if ((GetCapture = hWnd) and GetCursorPos(pt)) then
        begin    
           // pixelcolor
          clr := GetPixel(AppDC, pt.x, pt.y);
      end;
   end;

   WM_CTLCOLORSTATIC:
     begin
       brush := CreateSolidBrush(clr);
       result := BOOL(brush);
     end;

Chris P 11. Feb 2007 19:14

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Erstmal vielen Dank für die Antworten.

WM_CTLCOLORSTATIC wird geschickt wenn das Control neu gezeichnet wird.

Also müsste ich ja nach
Delphi-Quellcode:
clr := GetPixel(AppDC, pt.x, pt.y);
das Control neuzeichnen?

Oder wie löse ich das WM_CTLCOLORSTATIC-Ereignis aus?

stefan2005 11. Feb 2007 19:22

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
hi,
vom Gefühl her würde ich sagen, dass GetPixel nichts neuzeichnet (da es nur einen pixel ausließt).
probier es doch mal mit InvalidRect, das sollte das auf alle Fälle neuzeichnen.

Grüsse,
Stefan

Chris P 11. Feb 2007 19:37

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Das hier führt leider immer noch zu gewünschten Erfolg:
Delphi-Quellcode:
WM_INITDIALOG:
     begin
        GetWindowRect(GetDlgItem(hwnd, 106), rc);
        AppDC := CreateDC('DISPLAY', nil, nil, nil);
     end;
   WM_CTLCOLORSTATIC:
      begin
        case GetDlgCtrlId(lParam) of
          IDC_COLOR_RECT:
            begin
              brush := CreateSolidBrush(clr);
              Result := BOOl(brush);
            end;
        end;
      end;
   WM_MOUSEMOVE:
      begin
        if ((GetCapture = hWnd) and GetCursorPos(pt)) then
        begin
       
          // pixelcolor
          clr := GetPixel(AppDC, pt.x, pt.y);

          invalidateRect(GetDlgItem(hwnd, 106), @rc, true);
         
        end;
      end;
 ....

bitsetter 11. Feb 2007 21:38

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Also ich habe deinen Code mal geändert, bei mir läuft das jetzt. Natürlich nur innerhalb des Fensters, denn außerhalb funktioniert WM_MOUSEMOVE nicht. Kannst ja mal testen, must sicherlich noch anpassen, da du nur ein Teil des Codes gezeigt hast. Ist sicher noch nicht optimal.
Delphi-Quellcode:
var
 clr: integer;//globale variable

//...
var
  WinInfo: TWindowInfo;
  brush: HBrush;
  pt: TPoint;
  hDC: cardinal;
begin
  //Result := 0;
  case uMsg of
    WM_CTLCOLORSTATIC:
      begin
        case GetDlgCtrlId(lParam) of
          IDC_COLOR_RECT:
            begin
              brush := CreateSolidBrush(clr);
              Result := BOOl(brush);
            end;
        end;
      end;
   WM_MOUSEMOVE:
      begin
        if (*(GetCapture = hWnd) and*) GetCursorPos(pt) then
        begin
          FillChar(WinInfo,Sizeof(WinInfo),0);
          WinInfo.cbSize := Sizeof(WinInfo);
          GetWindowInfo(hWnd,WinInfo);
         
          dec(pt.X, WinInfo.rcClient.Left);
          dec(pt.Y, WinInfo.rcClient.Top);
          hDC:= GetDC(hWnd);
          clr:= GetPixel(hDC, pt.x, pt.y);
          windows.ReleaseDC(hWnd, hDC);
          RedrawWindow(hwndLabel1, nil, 0, RDW_INVALIDATE or RDW_ERASE);//neu Zeichnen
       end;
      end;
//...

Chris P 12. Feb 2007 10:28

Re: [nonVCL] Farbe von STATIC bei Mausbewegung ändern
 
Danke bitsetter!!!

RedrawWindow(...) ist genau das was ich gebraucht habe!

Ich kann WM_MOUSEMOVE auch außerhalb des Fensters abfangen, da ich mit SetCapture arbeite.


Mein Problem ist gelöst :thumb:


Mit freundlichen Grüßen

Chris P


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:22 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz