Thread: random picture

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    random picture

    I have a program in MFC (dialog based) that creates a random number. I have imported 3 pictures (a.bmp, b.bmp, c.bmp). The random number is between 1 and 10. I want to make it so if the random number is 123 or 4 display a, 567 display b, and 8910 display c.

    How can i do this?

    ALSO

    how about a dialog box that can be used as a login that will talk with a mysql server?

    thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    int x = (rand() % 10) + 1;
    Somepicture *pic = NULL;
    
    ....
    
    switch(x)
    {
    	case 1:
    	case 2:
    	case 3:
    	case 4:
    		pic = a;
    	break;
    	case 5:
    	case 6:
    	case 7:
    		pic = b;
    	break;
    	case 8:
    	case 9:
    	case 10:
    		pic = c;
    }
    
    /* Do something with pic if it's not NULL. */
    I don't know how you have the pictures stored, but if you follow this general idea and have a pointer to the data type that represents the picture, and then points to the right picture based upon the random number, you're set. Alternatively, if you have all of the pictures in an array, instead of dealing with a pointer, set an index value, and then access that element of the array, provided you got a valid index.

    As far as talking to a mysql server, what about it? Check the documentation for the mysql SDK.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    When I do stuff like this I tend to use an If statement instead of a switch. It saves time typing, especially if you have a large list. (might not be as readable tho)

    Code:
    if(x<5) pic=a;
    else if(x<8) pic=b;
    else pic=c;

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    wasn't really what i was looking for. I made a dialog app in mfc and i want it to display a picture like in the if statement and the switch. I already had that, but how do i make it so that IDC_BITMAP1 is displayed if a=1234 and IDC_BITMAP2 is displayed if b=567 etc.

    thanks

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Erm. I'm not sure if I get exactly what you are on about. Are you asking how to actually draw the picture by any chance?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    nope. I imported 3 pictures, with resources named IDB_BITMAP1, 2 and 3 respectively. I created a bitmap display section in the dialog box. I want to make an if/switch statement that will let me display IDB_BITMAP1 in the bitmap section if a==123 or 4, IDB_BITMAP2 is a >4.

    Get it?

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Not really.

    Apparently you know how to load and draw an image. You also know how to generate a random number, and select an image based on the result.

    What else is there you need to know?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't do MFC, so I can't help ya with specifics if you're asking an MFC-specific question. I agree with Mike. If you know how to display an image and already have code like what I posted for a random number, then you should be set.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Create a static and give it the SS_BITMAP style (at least).

    OR

    Use the class wiz to link a variable to a static control.

    [Picture controls are a sub-class of static controls see CStatic as the base class]



    Call the SetBitmap() method to change the image displayed.

    ie
    Picture.SetBitmap(::LoadBitmap(NULL,MAKEINTRESOURC E(IDB_MYIMAGE));


    May require a paint msg (to the statics parent) before the changes are visible
    [ InvalidateRect() then UpdateWindow() ]
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    36
    Try this: Bitmap[rand()%3].Draw().

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    The MFC class CBitmap does not have a 'Draw()' method.

    Nor does that link the Bitmap to the Picture control.

    I think you are thinking of managed C++ [.NET] not MFC [non .NET].
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM