NO

Author Topic: Need Some Tips/Direction/Help on a Compile  (Read 7318 times)

EdPellesC99

  • Guest
Need Some Tips/Direction/Help on a Compile
« on: October 03, 2011, 05:59:58 AM »
Hi,

  I found a nifty little freeware tool, a while back for giving you the RGB colors under the cursor in a tiny window.

I started using it, and I would like to modify it so that the window stays on top.

Trouble is I lost notes on where I got it. The caption for the window is What Color.

I would just use CreatProcess start the .exe, in HIDE mode, and use FindWindow.

But although I can use the same code on notepad, for some reason FindWindow fails on this window, and I don't know why...

So I could show my code and talk about that, but maybe instead I could compile myself.

At this site:
http://www.go4expert.com/forums/showthread.php?t=14587

I found what I believe is the source code for my little .exe.

I attached the .zip here.

The exe I have been using is called "Color Under Cursor.exe" but when you run it and use a window Id tool the caption is What Color.

Probably ....someone got this source and compiled it as "Color Under Cursor.exe" and posted it in some forum ( I remember that), and now I cannot find the forum (which might have had his modified code.

Anyway *This*..... is c code, and I cannot get it to compile.

I do not need it to be setting a taskbar icon, but I have been unsuccessful at ripping out this part of the code.

If I can get it to compile, I can verify it is the same as the .exe I had, and I can then manipulate the window as I want.

Tx for any ideas.

Ed

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Need Some Tips/Direction/Help on a Compile
« Reply #1 on: October 03, 2011, 08:29:39 AM »
Insert into WhatColor.c these lines after #include <windows.h>
Code: [Select]
#include <shellapi.h>

#pragma comment(lib, "shell32.lib")
Modified example:
Code: [Select]
// WhatColor2.c
// Shows the position and color of the point under the cursor.
// To use, click on the window and drag the cursor around the screen.
// Dbl-click to close (or single-click on notification-area icon).
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define SIZEX 92
#define SIZEY 42
#define POSX   0
#define POSY   0
#define COLOR_BACK RGB(0,0,0)
#define COLOR_FORE RGB(0,200,200)
#define ID_TASKBAR_ICON 1
#define MYWM_NOTIFYICON WM_USER

LRESULT CALLBACK windowProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    static HDC hdcDisp;
    static int x, y;
    static COLORREF color;

    switch (msg) {

    case WM_CREATE:
        hdcDisp = CreateDC ("DISPLAY", NULL, NULL, NULL);
        break;

    case WM_PAINT:
        {
        HDC hdc;
        PAINTSTRUCT ps;
        char txt[32];

        hdc = BeginPaint (hwnd, &ps);
        SetBkColor (hdc, COLOR_BACK);
        SetTextColor (hdc, COLOR_FORE);
        TextOut (hdc, 5, 5, txt, wsprintf (txt, "(%d, %d)", x, y));
        TextOut (hdc, 5, 23, txt, wsprintf (txt, "%02x %02x %02x",
            GetRValue (color), GetGValue (color), GetBValue (color)));
        EndPaint (hwnd, &ps);
        }
        break;

    case WM_LBUTTONDOWN:
        SetCapture (hwnd);
        SendMessage (hwnd, WM_MOUSEMOVE, wp, lp);
        break;

    case WM_MOUSEMOVE:
        if (GetCapture() != hwnd) break;
        // The extra stuff here is to make it work properly
        // when the window is not positioned at 0,0.
        x = POSX + (SHORT) LOWORD (lp);
        y = POSY + (SHORT) HIWORD (lp);
        color = GetPixel (hdcDisp, x, y);
        InvalidateRect (hwnd, NULL, TRUE);
        break;

    case WM_LBUTTONUP:
        ReleaseCapture();
        break;

    case WM_LBUTTONDBLCLK:
    case WM_DESTROY:
        DeleteDC (hdcDisp);
        PostQuitMessage (0);
        break;

    default:
        return DefWindowProc (hwnd, msg, wp, lp);
    }
    return 0;
}

void registerWindow (HINSTANCE hinst, char *appName,
                     HBRUSH hbrBackground, WNDPROC wndproc)
{
    WNDCLASSEX wc;
    wc.cbSize        = sizeof (wc);
    wc.style         = CS_DBLCLKS;
    wc.lpfnWndProc   = wndproc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hinst;
    wc.hIcon         = LoadIcon (hinst, appName);
    wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = hbrBackground;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = appName;
    wc.hIconSm       = wc.hIcon;
    RegisterClassEx (&wc);
}

int __cdecl WinMainCRTStartup(void)
{
    char *appName = "WhatColor2";
    MSG msg;
    HWND hwnd;
    HBRUSH hbrBackground;
    HINSTANCE hinst;

    hinst = GetModuleHandle(0);
    hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
    registerWindow (hinst, appName, hbrBackground, windowProc);

    hwnd = CreateWindow (appName, appName, WS_POPUP|WS_VISIBLE,
                POSX, POSY, SIZEX, SIZEY, 0, 0, hinst, 0);
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);
    while (GetMessage (&msg, 0, 0, 0))
        DispatchMessage (&msg);

        ExitProcess(msg.wParam);
}
« Last Edit: October 04, 2011, 10:55:35 AM by timovjl »
May the source be with you

EdPellesC99

  • Guest
Re: Need Some Tips/Direction/Help on a Compile
« Reply #2 on: October 03, 2011, 05:15:05 PM »

Great,

  Thanks Timo !

If I had more energy to think, I would have searched the header files for "NOTIFYICONDATA".

I had exhausted my brain trying to deal with the "Color Under Cursor.exe" file I had: (& trying to get FindWindow to work).

I can see the orig .exe at 38 kb, has a bit more going on than the new "WhatColor.exe" at 17 kb.

I will try to modify this code to be more similar (more than ever - I think someone modified this .c file to produce this Color Under Cursor.exe.)

In a couple days, I will post my other effort.... where I started Color Under Cursor.exe in Hide Mode, and was unable to find it using FindWindow (knowing the class, and window title as determined by SpyXX).

I am curious as to why I could not Find the window. I will give more details in the next thread. I am looking for someone to verify that manipulating the window of an .exe, using another .exe (with the api) is only possible if the window was of a certain class or style (as I suspect).

Thanks much, Timo ! ..... Ed



EdPellesC99

  • Guest
Re: Need Some Tips/Direction/Help on a Compile
« Reply #3 on: October 03, 2011, 11:25:34 PM »
Well,

  Though I was able to compile the program. I had some effort to put into "WhatColor.exe" if I wanted it to work as well as this bit larger file "Color Under Cursor.exe".

  So in trying to simplify my code to post along with the program "Color Under Cursor.exe", and ask *WHY* I could not use  FindWindow and actually *find* the window ..... my problem disappeared !!!!


  Anyway I attached the program I like, if anyone is interested. I feel stupid because I have no idea who it was that made it !

  In my defense, one day I was trying to solve another problem, and read about this guy talking about his c program that gave you the RGB value under the cursor. I had NO need for it..... but like the pack-rat I am I grabbed it and stuck it in my folder drawer. I don't even remember if the source was there.

  Lately I wanted to do some html work from scratch using the .chm file...... w/ the Html WorkShop freebee from Microsoft.

So I rummaged around in my drawers, and found that little program,  and really liked it !

Except I could not control the window by using FindWindow: because I couldn't find the window !!!

   I know *tmi* !   ;D

  The moral to this story is if you can't see your way ... to finding the window, it is like trying to see through a brick wall... not much fun. 

  Tx again Timo, Ed

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Need Some Tips/Direction/Help on a Compile
« Reply #4 on: October 04, 2011, 12:21:56 AM »
How about creating your own color spy?

You just need four API functions:
  • GetDC
  • GetCursorPos
  • GetPixel
  • ReleaseDC
UNIX systems ship with a RGB.txt file, if you need the name of the color assigned to the values.
Attached is an old one from HP-UX 10 and X11.
« Last Edit: October 04, 2011, 07:55:34 AM by Stefan Pendl »
---
Stefan

Proud member of the UltraDefrag Development Team

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Need Some Tips/Direction/Help on a Compile
« Reply #5 on: October 04, 2011, 08:57:28 AM »
Maybe this helps: ftp://ftp.charlespetzold.com/ProgWin5/Chap08/WhatClr/WhatClr.c
Code: [Select]
...
     HBRUSH          hbbg, hbold ;
...
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rc) ;
          hbbg = CreateSolidBrush(cr);
          hbold = SelectObject(hdc, hbbg);
          PatBlt(hdc, 0, 0, rc.right, rc.bottom, PATCOPY);
          wsprintf (szBuffer, TEXT ("  %02X %02X %02X  "),
                    GetRValue (cr), GetGValue (cr), GetBValue (cr)) ;
          DrawText (hdc, szBuffer, -1, &rc,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          SelectObject(hdc, hbold);
          DeleteObject(hbbg);
          EndPaint (hwnd, &ps) ;
          return 0 ;
or
Code: [Select]
...
     HBRUSH          hbbg ;
...
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rc) ;
          hbbg = CreateSolidBrush(cr);
          FillRect(hdc, &rc, hbbg);
          DeleteObject(hbbg);
          wsprintf (szBuffer, TEXT ("  %02X %02X %02X  "),
                    GetRValue (cr), GetGValue (cr), GetBValue (cr)) ;
          DrawText (hdc, szBuffer, -1, &rc,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          EndPaint (hwnd, &ps) ;
          return 0 ;
« Last Edit: October 04, 2011, 10:56:31 AM by timovjl »
May the source be with you

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Need Some Tips/Direction/Help on a Compile
« Reply #6 on: October 04, 2011, 09:38:43 AM »
It is not a good idea to paste the link directly into the message, since it gets corrupted by the parser and HTTP:// gets added to the front.
One needs to use the FTP tags to correct this.

ftp://ftp.charlespetzold.com/ProgWin5/Chap08/WhatClr/WhatClr.c
---
Stefan

Proud member of the UltraDefrag Development Team

EdPellesC99

  • Guest
Re: Need Some Tips/Direction/Help on a Compile
« Reply #7 on: October 04, 2011, 06:57:07 PM »
Timo !!

   Wow are you on the ball !
   You know I saw that Petzold program 3 yrs ago when I knew even less about the win32 api than I do today ( :) ).
I of course forgot it existed.

How nice.

Yes you have recreated the "Color Under Cursor.exe"... perfectly !
Fantastic !
   It has that great plus of painting the window constantly over the original Petzold file !
The exe is only 11 kb as opposed to the 38 kb file size of "Color Under Cursor.exe"
Absolutely terrific.
  You have found the ORIGINAL source of all this work with Petzold !
Quote
Funny that people continued to use the window title What Color, and the class "WhatClr" and of course this proves everyone was working with Petzold 's work !
  Great Detective work !

  What a nice, clever program.... so useful and now the code is there in all it's glory !

  Although Stefan is right it would be a nice project to develop this from the start, I was reluctant to go off on a tangent right now to build this tool from scratch just to help me on this .chm work ! (I go off on tangents ....and forget where I am !)
 
Thanks Stefan for looking up that RGB file. It is nice to associate a color name with an RGB, I wonder if this is an "official" list for .....HTML 4.
 
     Yesterday, I was able to position "Color Under Cursor.exe", and make it an "OnTop" all TOPMOST windows.
But when I used CreatProcess to call the .exe, I could not send a message to the window to bring "Color Under Cursor.exe" into Focus.
  (This would have been my next post !)
 
  SendMessage(hWnd, 0x0007, 0, 0 ); /*Though this would work on "NotePad.exe" it would not work on "Color Under Cursor.exe"*/

  I assume because Message Handling was goofed up inside the black box "Color Under Cursor.exe".

  Now that I have the better and less bloated version, I will re-look and suspect there will be no problem on Timo's ver of Petzold's file.

   I will get back later today.

  Thanks much ...... so much !

  ........ Ed        ;D

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Need Some Tips/Direction/Help on a Compile
« Reply #8 on: October 04, 2011, 07:32:59 PM »
Thanks Stefan for looking up that RGB file. It is nice to associate a color name with an RGB, I wonder if this is an "official" list for .....HTML 4.

It is an official list for X11, but Wikipedia has tons of color lists available.
---
Stefan

Proud member of the UltraDefrag Development Team

EdPellesC99

  • Guest
Re: Need Some Tips/Direction/Help on a Compile
« Reply #9 on: October 04, 2011, 07:46:31 PM »
Tx Stefan, I'll look there soon too.

Timo,

  Getting back, .......at the end of my last reply: I was thinking about positioning etc this new .exe with my windowless win32.exe but of course now that I am ...."inside the black box", I can do all that there. 
So I wasn't thinking (again !).

  Thanks again, It will be a useful tool I will enjoy using.

  Happy on this subject for sure  :)


Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Need Some Tips/Direction/Help on a Compile
« Reply #10 on: October 04, 2011, 08:42:26 PM »
This version is 3 kb when compiled with PellesC 6.50.
Code: [Select]
/*------------------------------------------
   WHATCLR.C -- Displays Color Under Cursor
                (c) Charles Petzold, 1998
  ------------------------------------------*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define ID_TIMER    1

void FindWindowSize (int *, int *) ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
#define SMALL
#ifdef SMALL
int __cdecl WinMainCRTStartup(void)
{
     STARTUPINFO si;
     GetStartupInfo(&si);
     HINSTANCE hInstance = GetModuleHandle(0);
     int iCmdShow = (si.dwFlags & STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOWDEFAULT;
#else
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
#endif
     static TCHAR szAppName[] = TEXT ("WhatClr2") ;
     HWND         hwnd ;
     int          cxWindow, cyWindow ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = 0; //(HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }

     FindWindowSize (&cxWindow, &cyWindow) ;

     hwnd = CreateWindow (szAppName, TEXT ("What Color"),
                          WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_BORDER,
                          0, 0, cxWindow, cyWindow,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSIZE);
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
#ifdef SMALL
     ExitProcess(msg.wParam);
#else
     return msg.wParam ;
#endif
}

void FindWindowSize (int * pcxWindow, int * pcyWindow)
{
     HDC        hdcScreen ;
     TEXTMETRIC tm ;

     hdcScreen = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
     GetTextMetrics (hdcScreen, &tm) ;
     DeleteDC (hdcScreen) ;
     
     * pcxWindow = 2 * GetSystemMetrics (SM_CXBORDER)  +
                        12 * tm.tmAveCharWidth ;

     * pcyWindow = 2 * GetSystemMetrics (SM_CYBORDER)  +
                       GetSystemMetrics (SM_CYCAPTION) +
                         2 * tm.tmHeight ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static COLORREF cr, crLast ;
     static HDC      hdcScreen ;
     HDC             hdc ;
     PAINTSTRUCT     ps ;
     POINT           pt ;
     RECT            rc ;
     HBRUSH          hbbg, hbold ;
     TCHAR           szBuffer [16] ;

     switch (message)
     {
     case WM_CREATE:
          hdcScreen = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
          SetTimer (hwnd, ID_TIMER, 100, NULL) ;
          return 0 ;

     case WM_DISPLAYCHANGE:
          DeleteDC (hdcScreen) ;
          hdcScreen = CreateDC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
          return 0 ;

     case WM_TIMER:
          GetCursorPos (&pt) ;
          cr = GetPixel (hdcScreen, pt.x, pt.y) ;
         
          if (cr != crLast)
          {
               crLast = cr ;
               InvalidateRect (hwnd, NULL, FALSE) ;
          }
          return 0 ;

     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          GetClientRect (hwnd, &rc) ;
          hbbg = CreateSolidBrush(cr);
          FillRect(hdc, &rc, hbbg);
          DeleteObject(hbbg);
          wsprintf (szBuffer, TEXT ("  %02X %02X %02X  "),
                    GetRValue (cr), GetGValue (cr), GetBValue (cr)) ;
          DrawText (hdc, szBuffer, -1, &rc,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          EndPaint (hwnd, &ps) ;
          return 0 ;

     case WM_ERASEBKGND:
          return 0;

     case WM_DESTROY:
          DeleteDC (hdcScreen) ;
          KillTimer (hwnd, ID_TIMER) ;
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
May the source be with you

EdPellesC99

  • Guest
Re: Need Some Tips/Direction/Help on a Compile
« Reply #11 on: October 04, 2011, 09:42:26 PM »
Wow !

Yes it is 3kb ! An icon will double it's weight !

You have out Petzolded .......Petzold !!!!

Your work is smokin' today ! Congrats !

I will have to look this over, maybe wearing asbestos gloves !

Tx, Ed