Thread: colorref and setpixel

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    67

    colorref and setpixel

    why can't i do this?

    Code:
    COLORREF pixColor2 = (10,50,100);
    cout <<pixColor2 << endl;
    clr=SetPixel(hdcScrn,col,row,pixColor2);
    (this is in 2 for loops and it makes about 100 pixels that color in the shape of a box)
    when i run it it sets the pixel at pos (col,row) to the color-value of 100. it is always the 3rd value in the (R,G,B) above. i know that because pixColor2 is printed out.

    also is there a way i can assign the value of pixColor2 to a value in an array? how would i declare that array?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> why can't i do this?

    COLORREF is a typedef for an unsigned int. as such you have to set the values using bitshifts. fortunately, MS defines a macro RGB() that does the bitshifting for you, as well as macros for extracting the values, ie:

    Code:
    COLORREF pixColor2 = RGB(10,50,100);
    cout << "Red = " << GetRValue(pixColor2) << endl
            << "Green = " << GetGValue(pixColor2) << endl
            << "Blue = " << GetBValue(pixColor2) << endl;
    clr=SetPixel(hdcScrn,col,row,pixColor2);
    this belongs in the Windows forum, btw...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    thanks a lot. but what about assigning the value of pixcolor2 to an array.

    Code:
    for (int x = 0; x < 5; x++)
    {
       COLORREF pixColor2 = RGB(10,50,100);
       clr=SetPixel(hdcScrn,col,row,pixColor2);
       array1[x] = pixColor2;
    }
    what nees to be changed there?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> what nees to be changed there?

    well, assuming array1 is declared as a 5-element (or more) array of COLORREF's - no changes are necessary.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    hmm well i can get it to compile but when i run it it immediately exits the program.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    We'd need to see more of your code to be sure, but are you putting something at the end of your program that waits for some form of user input before returning 0?

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    in my code i put the line in

    array1[x] = pixColor2;

    it compiles but does not run. i comment that line out it compiles and runs like normal. whatever is wrong with the program is in that line.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >>when i run it it immediately exits the program.
    ...
    >>but does not run...runs like normal

    How To Ask Questions The Smart Way

    gg

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    what's so confusing about what those few sentences said?

    "it compiles but does not run. i comment that line out it compiles and runs like normal. whatever is wrong with the program is in that line."

    i will put it in simpler english for you

    Code:
    code
    array1[x] = pixColor2;
    code
    outcome = program compiles, program opens and closes in a split second

    Code:
    code
    //array1[x] = pixColor2;
    code
    outcome = program compiles, program opens, program works like it should, by process of elimination (...or just plain common sense) the one line of code that i commented out should be the source of my problem above.

    hope you understand it now

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> what's so confusing about what those few sentences said?
    Once you read "How To Ask Questions The Smart Way", you'll know the awnser.

    gg

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    i'm not reading your nonsense noob's guide to asking a question. what i said was not even a question it was a statement of the problem. i have explained the problem enough that any person with any experience in anything in life would be able to understand my reasoning. i put one line of code into the program it doesn't work. i take that line out it works. it's not that hard to understand where the problem is. it is just difficult to understand why that is.

    if anyone can help with any other way besides posting links to stupid proper question asking techniques it would be much appreciated.
    Last edited by bballzone; 09-11-2004 at 05:00 PM.

  12. #12
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    >>i'm not reading your nonsense noob's guide to asking a question.
    LOL

    >>what i said was not even a question it was a statement of the problem.
    I think what he was trying to get at was we're not really sure exactly what the problem is, because programs can stop running for a lot of reasons. Have you tried debugging the program? Possibly x is extending too far in to the array and crashing your program? We don't know, because you only gave us a few lines of code and expect us to know why your program's not working.


    >>if anyone can help with any other way besides posting links to stupid proper question asking techniques it would be much appreciated.
    The guide's really not that stupid, as it explains a lot of the things that get a lot of the older members here ticked off. You don't have to follow that guide as a lifelong rule, just take it in to account next time you post and need help and I can gaurantee you'll get a lot more (and better) help.

    Probably the best advice from that guide is to not act like you're acting You're in cboard territory now. Not kiddy-playland. We're here to help you, but we aren't a substitute for your own brain.

    -edit-
    At codeplug:
    I've always wondered what the gg means at the end of all your posts?
    Last edited by jverkoey; 09-11-2004 at 05:08 PM.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    in my code i put the line in

    array1[x] = pixColor2;

    it compiles but does not run.
    how is array1[] defined?

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    67
    Code:
    	INT array1[1000][1000];
    ....
    	for (INT col = 400; col < 500; col += 10)
    	{
    		for (INT row = 400; row < 500; row += 10)
    		{
    			GetCursorPos(&pt);
    			clr=GetPixel(hdcScrn,pt.x,pt.y);
    			COLORREF pixColor = GetPixel(hdcScrn,col,row);
    			char chPixColor[1024];
    
    			//array1[row][col] = pixColor;
    			Sleep(10);
    		}
    	}
    ...
    	system("pause");
    the system pause has always been in there yet it does not display it and ask for any key when that problem line of code is uncommented.

    come to think about it...the array is probably not declared correctly...to google i go

Popular pages Recent additions subscribe to a feed