Use VC ++ to create an animation button to play an AVI Video Stream

Source: Internet
Author: User

The Visual C ++ development environment provides the self-drawing function for the control.ProgramMembers can make full use of their creativity to design more beautiful program interfaces. The so-called Avi button refers to the avi that plays a button prompt every time the mouse passes through the button, in many game programs and 3D animation software (such as, Cool 3D, etc) these Avi buttons are widely used. It makes the user interface of the program very dynamic, and makes our program at least look more professional, this example uses Visual C ++'s powerful control self-drawing function to implement the principle and implementation of this avi button.
I. Implementation Method

To enable playback of an animation button for an AVI video stream, you still need to use the overload function of the control. This part of the content has been introduced in "Example: buttons for implementing the XP style, I will not go into details here. You can refer to this instance for more information. This example describes how to use the canimatectrl animation class of MFC to play an AVI video stream. The video stream can come from an AVI file or a resource. The rational use of animation controls in the program can make the program interface more vivid.

The canimatectrl class of MFC encapsulates animation controls. The create () member function of this class is responsible for creating animation controls. Its declaration is:

Bool create (DWORD dwstyle, const rect & rect, cwnd * pparentwnd, uint NID );

The dwstyle parameter in the function is a combination of control styles shown in table 1. The rect parameter specifies the size of the Control. pparentwnd points to the parent window, and NID is the Control ID. If it is successfully created, the function returns true.

Style Description
Acs_center Place the animation in the center of the control, and keep the size and position of the control window unchanged after the animation is opened. If this style is not specified, the widget size is automatically adjusted to adapt to the animation size.
Acs_transparent Make the background of the cartoon transparent (do not output the background color of the cartoon ).
Acs_autoplay Once the cartoon is turned on, it will be played repeatedly.

Table 1 animation control style

In addition to the style in the table, you must specify ws_child, ws_visible, and ws_border window styles for the animation control. For example, to create an animation control that can be automatically played, set its style to ws_child | ws_visible | ws_border | acs_autoplay.

Main canimatectrl member functions include: bool open (lpctstr lpszfilename), bool open (uint NID), bool play (uint nfrom, uint NTO, uint nrep), bool seek (uint NTO), bool stop (), bool close (), etc.

The preceding open () function enables video streams from an AVI file or resource. If the parameter lpszfilename or NID is null, the system disables the previously opened video streams. The play () function is used to play the selected video stream. The nfrom parameter specifies the index of the Start frame for playing. The index value must be less than 65536. If it is 0, the video is played from the beginning. NTO specifies the index of the end frame. Its value must be less than 65536. If it is-1, it indicates playing to the end of the video stream. Nrep is the number of replay times. If it is-1, it will be played infinitely. The seek () function is used to statically display a frame of a video stream. The NTO parameter is the frame index. The value must be less than 65536. If it is 0, the first frame is displayed. If it is-1, the last frame is displayed. If it is successful, the function returns true. The stop () function is used to stop playing cartoons. The close () function is used to close and clear video streams from memory. True is returned if all the above functions are called successfully. Otherwise, false is returned.

Readers should note that the animation control cannot play all AVI files. Only AVI Files meeting the following conditions can be played: 1) AVI files must be silent, 2) The AVI file must be uncompressed or RLEAlgorithmCompressed; 3) the AVI color palette must remain unchanged. In addition, the biggest limitation of an animation control is that it can only display the default color in the system palette. Therefore, if an animation control is used to play an AVI file of 256 colors, the playback effect looks like a 16-color animation, which is not ideal. In short, the animation control can only play some simple Avi animations with a few colors. If you want to play 256-colored AVI files with high satisfaction, you need to use the MCI interface. For this part, please refer to the relevant materials.

Ii. programming steps

1. Start visual c ++ 6.0, generate a dialog box-based application, name the project testavibutton, and click Finish to create the project;

2. Use the Class Wizard menu command to create the cavibutton Class Based on cbutton and generate the Class header file avibutton. h and the implementation file avibutton. CPP, and reload the message ing between the CREATE (), drawitem () Functions and wm_mousemove functions in the class;

3. Open the "idd_testavibutton_dialog" dialog box in the project to edit it. The prompt text in this dialog box is "move the mouse over the button:" delete "cancel, change the "OK" button attribute styles to ownerdraw, and remove the prompt text "OK". In addition, execute insert | resource... Command. In the pop-up dialog box, press custom... Then, enter "Avi" to add resources in the AVI format to the program, and then select an AVI file to add the resource idr_avi In the AVI format to the project;

4. AddCodeCompile and run the program. At this time, every time we move the mouse over the button, a beautiful Avi button is generated.

Iii. Implementation Code

//// // Avibutton. h: header file
# If! Defined (afx_avibutton_h00005e20d4ef_864e_11d7_886e_f16c81cd642b00000000ded _)
# Define afx_avibutton_h1_5e20d4ef_864e_11d7_886e_f16c81cd642b1_encoded _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
Class cavibutton: Public cbutton
{
// Construction
Public:
Cavibutton ();
Public:
Uint m_naviid;
Canimatectrl animatectrl;
Bool bplaying;
Void loadavi (uint naviid );
Void drawbutton (CDC * PDC, uint nstate, crect rect );
// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cavibutton)
Public:
Virtual void drawitem (lpdrawitemstruct );
Virtual bool create (maid, maid, DWORD
Dwstyle, const rect & rect, cwnd * pparentwnd, uint NID,
Ccreatecontext * pcontext = NULL );
//} Afx_virtual
// Implementation
Public:
Virtual ~ Cavibutton ();
// Generated message map Functions
Protected:
// {Afx_msg (cavibutton)
Afx_msg void onmousemove (uint nflags, cpoint point );
//} Afx_msg
Declare_message_map ()
};
# Endif

//// // Cavibutton. cpp file;
# Include "stdafx. H"
# Include "testavibutton. H"
# Include "avibutton. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

Cavibutton: cavibutton ()
{
M_naviid = 0;
Bplaying = false;
}

Cavibutton ::~ Cavibutton ()
{}
Void cavibutton: loadavi (uint naviid)
{
M_naviid = naviid;
}

Begin_message_map (cavibutton, cbutton)
// {Afx_msg_map (cavibutton)
On_wm_mousemove ()
//} Afx_msg_map
End_message_map ()

//// // Cavibutton message handlers
Void cavibutton: drawbutton (CDC * PDC, uint nstate, crect rect)
{
Colorref upcol, downcol, edgecol;
Edgecol = RGB (, 0); // you can specify the initialization color of the button edge;
If (nstate & ods_selected) = ods_selected)
{
// Set the color of the button when it is pressed.
Upcol = RGB (0, 0 );
Edgecol = RGB (1, 128,128,128 );
Downcol = RGB (0, 0 );
}
Else
{
// Set the button color when the button is normal
Upcol = RGB (255,255,255 );
Downcol = RGB (1, 128,128,128 );
}
Cpen * poldpen = NULL;
Bool pen1created;
Cpen pen1;
Bool pen2created;
Cpen pen2;
If (pen1created = pen1.createpen (ps_solid, 1, upcol ))
Poldpen = PDC-> SelectObject (& pen1 );
// Draw the upper left edge
PDC-> moveTo (1, rect. Height ()-1 );
PDC-> lineto (1, 1 );
PDC-> lineto (rect. Width ()-1, 1 );
PDC-> moveTo (0, rect. Height ()-1 );
PDC-> lineto (0, 0 );
PDC-> lineto (rect. Width ()-1, 0 );
If (pen2created = pen2.createpen (ps_solid, 1, downcol ))
PDC-> SelectObject (& pen2 );
If (pen1created)
{
Pen1.deleteobject ();
Pen1created = false;
}
// Draw the bottom right edge
PDC-> moveTo (rect. Width ()-1, 0 );
PDC-> lineto (rect. Width ()-1, rect. Height ()-1 );
PDC-> lineto (0, rect. Height ()-1 );
PDC-> moveTo (rect. Width ()-2, 1 );
PDC-> lineto (rect. Width ()-2, rect. Height ()-2 );
PDC-> lineto (0, rect. Height ()-2 );
If (pen2created) // Delete the "pen2" paint brush object
{
Pen2.deleteobject ();
Pen2created = false;
}
If (pen1created = pen1.createpen (ps_solid, 1, edgecol ))
Poldpen = PDC-> SelectObject (& pen1 );
If (pen1created)
{
Pen1.deleteobject ();
Pen1created = false;
}
If (poldpen! = NULL)
PDC-> SelectObject (poldpen );
}

Void cavibutton: drawitem (lpdrawitemstruct)
{
// Todo: add your code to draw the specified item
Crect rect;
Getclientrect (rect );
If (! : Iswindow (animatectrl ))
{
// Create an animation control on the button
Animatectrl. Create (ws_child | ws_visible, rect, this, 0 );
// Open the AVI file and display the first frame
Animatectrl. Open (m_naviid );
Animatectrl. getclientrect (rect );
}
CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
Uint nstate = lpdrawitemstruct-> itemstate;
Crect buttonrect;
Getclientrect (buttonrect );
// Draw button
Drawbutton (PDC, nstate, buttonrect );
}

bool cavibutton: Create (maid, maid, DWORD
dwstyle, const rect & rect, cwnd * pparentwnd, uint NID, ccreatecontext * pcontext)
{< br> // todo: add your specialized code here and/or call the base class
return cwnd: Create (lpszclassname, lpszwindowname, dwstyle,
rect, pparentwnd, NID, pcontext);
}

Void cavibutton: onmousemove (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
Clienttoscreen (& Point );
Crect rcwindow;
Getwindowrect (rcwindow );
// Determine whether the mouse goes through the button
Bool bnewmouseoverbutton = rcwindow. ptinrect (point );
If (bnewmouseoverbutton & isw.wenabled ())
{
If (: iswindow (animatectrl )&&! Bplaying)
{
Animatectrl. Play (0,-1, 1 );
Bplaying = true;
Setcapture ();
}
}
Else
{
Bplaying = false;
Releasecapture ();
}
Cbutton: onmousemove (nflags, point );
}
/////////////////////////////////
Bool ctestavibuttondlg: oninitdialog ()
{
Cdialog: oninitdialog ();
..................... // The code is omitted here;
M_avibutton.loadavi (idr_avi );
Return true; // return true unless you set the focus to a control
}

Iv. Summary

with the combination of the canimatectrl class and the Self-painting function of the button control, this example implements the Animation button. If this class is combined with controls such as toolbar and status bar, you can also play an animated video stream on the above controls.

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.