Novel Design of Bitmap Buttons in Visual C ++

Source: Internet
Author: User
Bitmapbutton is indeed a very important role in many Windows software. Compared with a normal button, it replaces text with a graphic, Which is intuitive, makes the screen more lively, and makes the human-machine interface more friendly. I often use Bitmap Buttons when using Visual C ++ to compile software. However, during the compilation process, it is found that there are still some shortcomings in the United States.

 


I. Question proposal

When we are on the Internet, we often use various browsers, such as IE or Netscape. The bitmap button on the toolbar cannot help but impress users. When the mouse is not touched timely, the bitmap button in normal state is just a plane image; once touched, it immediately

"Coming Out", on the one hand, prompting users, on the other hand, their color and three-dimensional contrast also give people a fresh feeling. Currently, many software programs written in Visual C ++ do not change the bitmap button before and after it is touched. Although it has made great progress compared with common buttons, it still seems dull, lack of "dynamic ". This article tries to use the bitmap button's "get input status" (focused) to work with the mouse operation.

 


Ii. Solution and related functions

The Microsoft basic class library (MFC) provides the cbitmapbutton class. Its common member functions include autoload and loadbitmaps. The following is a brief introduction:

1. bool autoload (uint NID, cwnd * pparent );

This function associates a common button with a cbitmapbutton class object.

  1. Bool loadbitmaps (maid = NULL, maid = NULL, maid = NULL );

The bitmap button has four states: normal (u) state, pressed (d) state, get the input (f) state, and disabled (x) state. F status is not commonly used.

This function maps the preceding four states of the cbitmapbutton Class Object to the four bitmap files. The parameters are as follows:

Lpszbitmapresource is the bitmap file name in the normal state (u) of the bitmap button.

Lpszbitmapresourcesel is the bitmap file name in the status (d) pressed by the bitmap button.

Lpszbitmapresourcefocus is the bitmap file name obtained by the bitmap button in the input state (f.

Lpszbitmapresourcedisabled is the bitmap file name in the forbidden state (x) of the bitmap button.

  1. Setcapture () and releasecapture ()

Setcapture ()

"Snap", even if the mouse cursor has been removed from the window, the window can still receive all messages about the mouse. Releasecapture () is used to release the Mouse capture.

 

When the mouse clicks but the mouse is not pressed, it does not correspond to any of the statuses. It can be seen that VC ++ does not provide the corresponding mechanism to solve the problem raised in this article. This program uses the mouse to move the bitmap button within the range, set it to get the input state, transferred to the third resource file "F" bitmap file to implement. When the mouse clicks the bitmap, it "appears", changes the mouse cursor to the shape of a small hand, and then calls the bell to remind you. At the same time, because the setcapture () function is used, various operations on the mouse need to be designed. The main program section is as follows.

 


3. Main program section

I have compiled a small demo program to simulate the electrical switch function. It has two Bitmap Buttons and an editing control box used as the status display. When the mouse is placed above the bitmap button, the Status display displays "the mouse cursor on the bitmap button ". When the power switch is turned on, the speaker bitmap button will be voiced; otherwise, the speaker bitmap button will become grayed out and will not work.

Void cbmpdlg: onmousemove (uint nflags, cpoint point)

{

Cwnd * pwnd;

Hcursor mycursor;

Crgn m_regionpower; // region of power button

Crgn m_regionplay; // region of play button

// Create the rectangle area of the bitmap button

M_regionpower.createellipticrgnindirect (crect (27,56, 72,92 ));

M_regionplay.createellipticrgnindirect (crect (78,56, 120,92 ));

Cstring showstring0 = "place the cursor on the bitmap button !!! ";

...

... ... ...

M_bplay = false;

M_bpower = false;

If (m_regionpower.ptinregion (point) // place the cursor over the bitmap button

{

M_bpower = true;

// Set the bitmap button to get the input state

Pwnd = getdlgitem (idc_button_power );

Pwnd-> setfocus ();

Setcapture ();

Inputedit (). setwindowtext (showstring0 );

Inputedit (). showwindow (true );

// Turn the mouse cursor into a small hand shape

Mycursor = afxgetapp ()-> loadcursor (idc_mycursor );

: Setcursor (mycursor );

Verify (m_play.loadbitmaps ("playu", "playd", "playf", "playx "));

M_bpressedplay = false;

Return;

}

If (m_regionplay.ptinregion (point) // place the cursor over the bitmap button

{

If (m_bpoweron) {// if the power supply is enabled

M_bplay = true;

Pwnd = getdlgitem (idc_button_play );

Pwnd-> setfocus ();

Setcapture ();

Inputedit (). setwindowtext (showstring0 );

Inputedit (). showwindow (true );

Mycursor = afxgetapp ()-> loadcursor (idc_mycursor );

: Setcursor (mycursor );

Verify (m_power.loadbitmaps ("poweronu", "powerond", "poweronf "));

}

Else {// if the power supply is off

Releasecapture ();

Inputedit (). setwindowtext (showstring0 + showstring2 );

Inputedit (). showwindow (true );

Verify (m_power.loadbitmaps ("powerofu", "powerofd", "poweroff "));

}

M_bpressedpower = false;

Return;

}

// Place the cursor outside all Bitmap Buttons

Releasecapture ();

Inputedit (). setwindowtext (showstring1 );

Inputedit (). showwindow (true );

Pwnd = getdlgitem (idok );

Pwnd-> setfocus ();

Verify (m_play.loadbitmaps ("playu", "playd", "playf", "playx "));

If (m_bpoweron)

Verify (m_power.loadbitmaps ("poweronu", "powerond", "poweronf "));

Else

Verify (m_power.loadbitmaps ("powerofu", "powerofd", "poweroff "));

M_bpressedplay = false;

M_bpressedpower = false;

Cdialog: onmousemove (nflags, point );

}

Void cbmpdlg: onlbuttondown (uint nflags, cpoint point)

{

Cwnd * pwnd;

If (m_bplay & m_bpoweron ){

// Change focus so as to change the bitmap of m_play

Pwnd = getdlgitem (idok );

Pwnd-> setfocus ();

Verify (m_play.loadbitmaps ("playd "));

M_play.updatewindow ();

M_play.invalidate (true );

Onbuttonplay ();

M_bpressedplay = true;

}

If (m_bpower = true ){

// Change focus so as to change the bitmap of m_power

Pwnd = getdlgitem (idok );

Pwnd-> setfocus ();

If (m_bpoweron)

Verify (m_power.loadbitmaps ("powerond "));

Else

Verify (m_power.loadbitmaps ("powerofd "));

M_power.updatewindow ();

M_power.invalidate (true );

Onbuttonpower ();

M_bpressedpower = true;

}

Cdialog: onlbuttondown (nflags, point );

}

 

Void cbmpdlg: onlbuttonup (uint nflags, cpoint point)

{

Cwnd * pwnd;

If (m_bpressedplay = true ){

Pwnd = getdlgitem (idok );

Pwnd-> setfocus ();

Verify (m_play.loadbitmaps ("playf "));

M_play.updatewindow ();

M_play.invalidate (true );

M_bpressedplay = false;

}

If (m_bpressedpower = true ){

Pwnd = getdlgitem (idok );

Pwnd-> setfocus ();

If (m_bpoweron)

Verify (m_power.loadbitmaps ("poweronf "));

Else

Verify (m_power.loadbitmaps ("poweroff "));

M_power.updatewindow ();

M_power.invalidate (true );

M_bpressedpower = false;

}

Cdialog: onlbuttonup (nflags, point );

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.