Click here to Skip to main content
13,860,571 members
Click here to Skip to main content
Add your own
alternative version

Stats

98.8K views
2K downloads
23 bookmarked
Posted 1 Apr 2003
Licenced

RGB Editor

, 17 Apr 2003
Rate this:
Please Sign up or sign in to vote.
RGB Color Coder

Sample Image - RGBEditor.gif

Introduction

This article demonstrates the technique of creating a RGB color editor, useful at web-page and software development time, to know the RGB code for different combinations of Red, Green and Blue colors. The article has two projects both independent, one was developed using the popular MFC and the other in Win32 (a.k.a. SDK). Both applications are identical in functionality and helps developers who are not very familiar with Win32 programming, to compare and see how easy it is to develop applications in Win32.

These applications also have an additional facility of tracking RGB color code of any point on the desktop in Hex and Red, Green and Blue codes, and x and y co-ordinates of the cursor location. You can also copy the codes to the clipboard making it easy to incorporate the color code in your source code.

Background

The idea for writing this application came from a similar utility I was using when developing some accounting applications in Power-Builder; this application is called RGB Calculator in Power-Builder, and another shareware utility for tracking color codes of images on screen. I combined functionality of both these utilities in these applications.

I added the Win32 application to the article since the MFC application was working as expected (refer the "Know problems" section), and not to show my programming capabilities, but I found similar problems with the Win32 application also.

Points of interest

Copying to clipboard

if(::OpenClipboard(NULL))
{
   LPTSTR  lptstrCopy; 
   HGLOBAL hglbCopy; 
   int cch = 30;
   hglbCopy = GlobalAlloc(GMEM_DDESHARE, 
                          (cch + 1) * sizeof(TCHAR)); 
   if (hglbCopy == NULL) 
   { 
       CloseClipboard(); 
       return; 
   } 

   char lpStr[30];
   wsprintf(lpStr, "RGB(%d, %d , %d)" , m_Slider_Red.GetPos() , 
              m_Slider_Green.GetPos(),   m_Slider_Blue.GetPos());
   lptstrCopy = (LPTSTR)GlobalLock(hglbCopy); 
   memcpy(lptstrCopy, lpStr, cch * sizeof(TCHAR)); 
   lptstrCopy[cch] = (TCHAR) 0;    // null character 
   GlobalUnlock(hglbCopy); 

   ::EmptyClipboard();
   ::SetClipboardData(CF_TEXT, hglbCopy);
   ::CloseClipboard();
}

Both these are dialog-based applications, but I have added tool and status bars to them through code like this:

Toolbar

TBBUTTON tb;

m_ToolBar.Create(
   WS_CHILD|WS_VISIBLE|WS_BORDER|TBSTYLE_FLAT,
   CRect(0,0,0,0), this, 0);

m_ToolBar.SetImageList(&m_pImageList);
tb.iBitmap = 0;
tb.iString = NULL;
tb.fsState = TBSTATE_ENABLED;
tb.fsStyle = TBSTYLE_BUTTON;
tb.idCommand = ID_FILE_EXIT;
m_ToolBar.AddButtons(1, &tb);

tb.iBitmap = 1;
tb.idCommand = ID_ABOUTBOX;
m_ToolBar.AddButtons(1, &tb);

Status bar

m_StatusBar.Create(WS_CHILD|WS_VISIBLE|SBT_OWNERDRAW,
                                   CRect(0,0,0,0), this, 0);
int strPartDim[4]= {80, 160, 240, -1};
m_StatusBar.SetParts(4, strPartDim);

A point of irritation in a dialog-based MFC application is that when you press any key (specially Enter), the application closes. I have prevented it by overriding the following function:

BOOL CColorCoderDlg::PreTranslateMessage(MSG* pMsg) 
{
    // This prevents the Main Dialog Window
    // from closing on press of a key
    if (pMsg->message == WM_KEYDOWN)
        return FALSE;
    
    return CDialog::PreTranslateMessage(pMsg);
}

The MFC application sets up a Windows hook whenever it needs to track color code and cursor positions of any location. It first changes the pointer cursor and sets it back to original when you release left-button. I found the hook unavoidable because if you change the mouse cursor without a hook, the changed cursor is only available within the client-area, not outside of it.

Windows hook function was not required in the Win32 application.

Known problems

  1. When tracking color codes and cursor location by pressing and dragging left-mouse button under Windows 98, Windows Me, the mouse cursor flickers to a very large extent (it virtually disappears for a second before reappearing), but under Windows XP (and maybe under Windows 2000) it seems fine, no prizes for guessing.
  2. In the Win32 application if you click on the close button (x) at the top-right of the main window, nothing happens. I have not added code to close the application for this message.
  3. Only RGB code is copied to clipboard, if you want the hex code to be copied to clipboard make changes in the source as shown in the function OnCopytoClipboard().

Win32 applications

Some developers may be wondering, is development under Win32 easier, is it better then MFC. I am not competent enough to answer such questions but if I am asked to develop an application and I know I can easily develop it in either, I would settle for Win32. Win32 applications are robust, less prone to errors at run-time and you do not have to worry about "overridable" functions, as in MFC applications.

You are however required to know about Window messages in a big way since class-wizard used in MFC is not available here. Visual Studio's "Spy++" utility is the most useful tool available for tracking messages.

Note: I had to override 2 functions in the MFC application viz OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) and PreTranslateMessage(MSG* pMsg).

Spy++ utility

Spy++ is a utility which is part of Visual Studio (Professional Edition). I have only used it so far to track Windows messages, maybe it can be used for other purposes, if you want to track messages.

  1. Start you application (preferably not in full-screen).
  2. Start Spy++
  3. From Spy++ menu select Spy->Messages
  4. Press and drag left-button on the "Bullseye" icon (right of 'Finder Tool'), onto you application, select entire window or a control depending on you message requirement
  5. Your mouse cursor will change to "Bullseye" (round)
  6. Release mouse button
  7. Click Ok on Spy++
  8. Do not iconize Spy++

If you have a command button on your application and want to know what message will be sent and where.

Note the last message number in Spy++ (left-most number in list box) and then click on the command button in your application, a line (at times many) will be added to Spy++ list of messages.

Note: Spy++ can be used for all types of applications, be it in MFC, Win32 , ATL etc.

History

  • New article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Barretto VN
India India
Nothing to boast about

You may also be interested in...

Comments and Discussions

 
GeneralOverride CDialog::OnOK method [modified] Pin
td222-Aug-06 3:15
membertd222-Aug-06 3:15 
GeneralRe: subclassing an MFC instance Pin
td222-Aug-06 2:57
membertd222-Aug-06 2:57 
GeneralNot bad, ... not bad at all !! Pin
WREY22-Apr-03 22:51
memberWREY22-Apr-03 22:51 
GeneralGood news Pin
56789012342-Apr-03 23:14
member56789012342-Apr-03 23:14 
GeneralRe: Good news Pin
Andreas S. Franci Gonçalves3-Apr-03 0:48
professionalAndreas S. Franci Gonçalves3-Apr-03 0:48 
GeneralUneducated Reader Contributions Pin
jhwurmbach2-Apr-03 5:40
memberjhwurmbach2-Apr-03 5:40 
GeneralRe: Uneducated Reader Contributions Pin
Andreas S. Franci Gonçalves2-Apr-03 5:56
professionalAndreas S. Franci Gonçalves2-Apr-03 5:56 
GeneralRe: Uneducated Reader Contributions Pin
Davide Calabro2-Apr-03 20:56
memberDavide Calabro2-Apr-03 20:56 
GeneralRe: Uneducated Reader Contributions Pin
David Wulff18-Apr-03 8:39
memberDavid Wulff18-Apr-03 8:39 
GeneralRe: Uneducated Reader Contributions Pin
jhwurmbach2-Apr-03 23:53
memberjhwurmbach2-Apr-03 23:53 
GeneralRe: Uneducated Reader Contributions Pin
Andreas S. Franci Gonçalves3-Apr-03 0:47
professionalAndreas S. Franci Gonçalves3-Apr-03 0:47 
GeneralRe: Uneducated Reader Contributions Pin
Bartosz Bien18-Apr-03 3:45
memberBartosz Bien18-Apr-03 3:45 
GeneralRe: Uneducated Reader Contributions Pin
jhwurmbach21-Apr-03 22:33
memberjhwurmbach21-Apr-03 22:33 
GeneralWill u stop posting this low kind of low-level f***ing garbage here Pin
programmer20032-Apr-03 4:58
sussprogrammer20032-Apr-03 4:58 
GeneralRe: Will u stop posting this low kind of low-level f***ing garbage here Pin
EpicBoy2-Apr-03 7:23
memberEpicBoy2-Apr-03 7:23 
GeneralRe: Will u stop posting this low kind of low-level f***ing garbage here Pin
YoSilver6-May-03 10:51
memberYoSilver6-May-03 10:51 
GeneralAnother example of "Unedited Reader Contributions" article Pin
Davide Calabro2-Apr-03 4:20
memberDavide Calabro2-Apr-03 4:20 
GeneralRe: Another example of "Unedited Reader Contributions" article Pin
EpicBoy2-Apr-03 7:24
memberEpicBoy2-Apr-03 7:24 
Generalbroken links Pin
Marc Clifton2-Apr-03 1:25
protectorMarc Clifton2-Apr-03 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 18 Apr 2003
Article Copyright 2003 by Barretto VN
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid