Programming with Microsoft Visual C++.NET 6ed [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Programming with Microsoft Visual C++.NET 6ed [Electronic resources] - نسخه متنی

George Shepherd, David Kruglinski

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید








Putting Bitmaps on Pushbuttons


The MFC library makes it easy to display a bitmap (instead of text) on a pushbutton. If you were to program this from scratch, you would set the Owner Draw property for your button and then write a message handler in your dialog class that would paint a bitmap on the button control's window. If you use the MFC CBitmapButton class instead, you end up doing a lot less work, but you have to follow a kind of "cookbook" procedure. Don't worry too much about how it all works (but be glad that you don't have to write much code!).

To make a long story short, you lay out your dialog resource as usual, with unique text captions for the buttons you designate for bitmaps. Next, you add some bitmap resources to your project, and you identify those resources by name rather than by numeric ID. Finally, you add some CBitmapButton data members to your dialog class, and you call the AutoLoad member function for each one, which matches a bitmap name to a button caption. If the button caption is Copy, you add two bitmaps: COPYU for the up state and COPYD for the down state. By the way, you must still set the button's Owner Draw property. (This will all make more sense when you write a program).





Note

If you look at the MFC source code for the CBitmapButton class, you'll see that the bitmap is an ordinary GDI bitmap painted with a BitBlt call. Thus, you can't expect any palette support. That's rarely a problem because bitmaps for buttons are usually 16-color bitmaps that depend on standard VGA colors.



The Ex06e Example


Ex06e shows how to show different bitmaps on a pushbutton. Here are the steps for building Ex06e:



    Run the MFC Application Wizard to produce the Ex06e project. Accept all the defaults but two: Select Single Document and deselect Printing And Print Preview.



    Modify the project's IDD_ABOUTBOX dialog resource in Resource View. We'll use the About dialog box that the MFC Application Wizard generates for hosting the bitmap button. Add three pushbuttons with captions, as shown below, accepting the default IDs IDC_BUTTON1, IDC_BUTTON2, and IDC_BUTTON3. The size of the buttons isn't important because the framework adjusts the button size at run time to match the bitmap size.


    Set the Owner Draw property to True for all three buttons.



    Import

    EditCopy.bmp ,

    EditPast.bmp , and

    EditCut.bmp from the \vcppnet\Ex06e directory on the companion CD. Choose Add Resource from the Project menu, and then click the Import button to import the bitmaps into the project. Start with

    EditCopy.bmp . Assign the name COPYU to the button.

    Be sure to use quotes around the name in order to identify the resource by name rather than by ID. This is now the bitmap for the button's up state. Close the bitmap window and, from Resource View, use the Clipboard to make a copy of the bitmap. Rename the copy COPYD (down state), and then edit this bitmap. Choose Invert Colors from the Image menu. There are other ways of making a variation of the up image, but inversion is the quickest.

    Repeat the steps listed above for the EditCut and EditPast bitmaps. When you're finished, you should have the following bitmap resources in your project.




























    Resource Name


    Original File


    Invert Colors


    "COPYU"


    EditCopy.bmp


    No


    "COPYD"


    EditCopy.bmp


    Yes


    "CUTU"


    EditCut.bmp


    No


    "CUTD"


    EditCut.bmp


    Yes


    "PASTEU"


    EditPast.bmp


    No


    "PASTED"


    EditPast.bmp


    Yes




    Edit the code for the CAboutDlg class. Both the declaration and the implementation for this class are contained in the

    Ex06e.cpp file. First add the three private data members shown here in the class declaration:


    CBitmapButton m_editCopy;
    CBitmapButton m_editCut;
    CBitmapButton m_editPaste;

    Then use the code wizards available from the Properties window to override the OnInitDialog virtual function. This function is coded as follows:

    BOOLCAboutDlg::OnInitDialog() { 
    CDialog::OnInitDialog();
    VERIFY(m_editCopy.AutoLoad(IDC_BUTTON1, this));
    VERIFY(m_editCut.AutoLoad(IDC_BUTTON2, this));
    VERIFY(m_editPaste.AutoLoad(IDC_BUTTON3, this));
    return TRUE;
    //return TRUE unless you set the focus to a control
    //EXCEPTION: OCX Property Pages should return FALSE
    }

    The AutoLoad function connects each button with the two matching resources. The VERIFY macro is an MFC diagnostic aid that displays a message box if you didn't code the bitmap names correctly.



    Edit the OnDraw function in

    Ex06eView.cpp . Replace the code generated by the MFC Application Wizard with the following line:


    pDC->TextOut(0, 0, "Choose About from the Help menu.");



    Build and test the application. When the program starts, choose About from the Help menu and observe the button behavior. The following image shows the CUT button in the down state.




Note that bitmap buttons send BN_CLICKED notification messages just as ordinary buttons do. The code wizards available from the Properties window can, of course, map those messages in your dialog class.



Going Further with Bitmap Buttons


You've seen bitmaps for the buttons' up and down states. The CBitmapButton class also supports bitmaps for the focused and disabled states. For the Copy button, the focused bitmap name would be COPYF, and the disabled bitmap name would be COPYX. If you want to test the disabled option, make a COPYX bitmap, possibly with a red line through it, and then add the following line to your program:

m_editCopy.EnableWindow(FALSE); 



/ 319