colours!!!!

This is a discussion on colours!!!! within the Windows Programming forums, part of the Platform Specific Boards category; How can my program automatically colour a word???? For example, when I write the word "help", that word should be ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Question colours!!!!

    How can my program automatically colour a word???? For example, when I write the word "help", that word should be coloured with red! How can I do that?????

    thanx and sorry, my english is bad!!!!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: colours!!!!

    Originally posted by eAgle
    How can my program automatically colour a word???? For example, when I write the word "help", that word should be coloured with red! How can I do that?????

    thanx and sorry, my english is bad!!!!
    It depends.....

    Is your application a fullblown GUI windows program or is it console based?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Cool

    it's GUI

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    From MSDN

    Code:
    SetTextColor
    The SetTextColor function sets the text color for the 
    specified device context to the specified color. 
    
    COLORREF SetTextColor(
      HDC hdc,           // handle to device context
      COLORREF crColor   // text color
    );
     
    Parameters
    hdc 
    Handle to the device context. 
    crColor 
    Specifies the color of the text. 
    Return Values
    If the function succeeds, the return value is a 
    color reference for the previous text color.
    
    If the function fails, the return value is CLR_INVALID. 
    
    Windows NT: To get extended error information, 
    callGetLastError. 
    
    Remarks
    The text color is used to draw the face of each character 
    written by the TextOut and ExtTextOut functions. The text 
    color is also used in converting bitmaps from color to monochrome and vice versa.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    can you give me an simple example, please???? I'm inexperienced in windows programming

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    As I am bored.......

    Here's a brief example;

    Code:
    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[] = "WindowsApp";
    HINSTANCE g_hInst;
    
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
    
    {
        HWND hwnd;               
    	MSG messages;            
    	WNDCLASSEX wincl; 
    	
    	g_hInst = hThisInstance;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProc;      
        wincl.style = CS_DBLCLKS;               
        wincl.cbSize = sizeof(WNDCLASSEX);
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
        wincl.cbClsExtra = 0; 
        wincl.cbWndExtra = 0; 
    	wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    
        
        if(!RegisterClassEx(&wincl)) return 0;
    
       
        hwnd = CreateWindowEx(0,szClassName, "Windows App",
    		WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
    		CW_USEDEFAULT,CW_USEDEFAULT, HWND_DESKTOP,NULL,            
               hThisInstance,NULL );    
    
       
       ShowWindow(hwnd, nFunsterStil);
    	UpdateWindow(hwnd);
       
        while(GetMessage(&messages, NULL, 0, 0))
        {
              TranslateMessage(&messages);
              DispatchMessage(&messages);
        }
    
        return messages.wParam;
    }
    
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, 
    							WPARAM wParam, LPARAM lParam)
    {
    
    	HDC hdc; //handle to device context
    	static enum {RED = 0,GREEN,BLUE};//enumerate possible colours
    	static int nCol = 0;//Active colour
    	COLORREF col;//RGB colour to use
    	PAINTSTRUCT ps;//Paint structure
    	
    
        switch (message)                
        {
               case WM_COMMAND:
    			   switch(LOWORD(wParam)){//What col was selected in menu
    			   case ID_COLOUR_GREEN:
    				   nCol = GREEN;
    				   break;
    			   case ID_COLOUR_BLUE:
    				   nCol = BLUE;
    				   break;
    			   case ID_COLOUR_RED:
    				   nCol = RED;
    				   break;
    			   }  
    		   InvalidateRect(hwnd,NULL,TRUE);//Make window repaint itself
               break;
               
               case WM_PAINT:
    			   switch(nCol){//What is the active colour
    			   case BLUE:
    				   col = RGB(0,0,255);
    				   break;
    			   case GREEN:
    				   col = RGB(0,255,0);
    				   break;
    			   case RED:
    				   col = RGB(255,0,0);
    				   break;
    			   }
    
    			   hdc = BeginPaint(hwnd, &ps);//Get DC and remove paint message
    			   SetTextColor(hdc,col);//Set the colour of test
    			   
    			   TextOut(hdc,100,100,"Hello World - in colour",23);//Some text to write
    			   EndPaint(hwnd,&ps);//Clear DC
    			             
               break;
               
               case WM_DESTROY:
               PostQuitMessage(0);        
               break;
               default:                   
               return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    Here's an app that paints some txt on its window.....you click down the menu and you can change the colour

    I have enclosed the project files in case you want to run the program - they work on VC++

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Doh...here are the files
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RGB Colours
    By rogster001 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 04-22-2008, 01:56 PM
  2. Colours in DOS box
    By denizengt in forum Tech Board
    Replies: 1
    Last Post: 09-10-2003, 07:53 AM
  3. it's not colours
    By Skarr in forum Game Programming
    Replies: 0
    Last Post: 05-13-2003, 01:21 PM
  4. Changing Text/Background colours for window controls ...
    By Shag in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:57 AM
  5. Colours?
    By Fountain in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2002, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21