VC++ to achieve output GIF form and display GIF animation method
- 2020-04-02 02:27:30
- OfStack
This article tells about VC++ in the form of the display of GIF animation method, this is the main part of the code, in this code, VC++ will use GDI technology to load GIF images, define GUID array, define Pointers, initialize the member variables, release property objects, release GUID and other resources.
The code is as follows:
//Load GIF file
void COutputGIFDlg::OnBrowse()
{
CFileDialog flDlg(TRUE, "", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"GIF|*.gif;||");//The definition file opens dialog box
if (flDlg.DoModal() == IDOK)
{
CString szText;
szText = flDlg.GetPathName();
m_FileName.SetWindowText(szText);
m_pBmp = Bitmap::FromFile(szText.AllocSysString()); //Load GIF image
szText.ReleaseBuffer();
m_nCount = m_pBmp->GetFrameDimensionsCount(); //Gets the frame dimension
GUID *pGuids = new GUID[m_nCount]; //Define an array of guids
m_pBmp->GetFrameDimensionsList(pGuids,m_nCount); //Gets the GUID of the image frame
m_FrameCount = m_pBmp->GetFrameCount(pGuids); //Gets the number of GIF frames
UINT nSize; //Define an integer variable
m_nCount = 0; //Initializes a member variable
m_pBmp->GetPropertySize(&nSize, &m_nDelay); //Get property size
PropertyItem *pItem = NULL; //Define property pointer
pItem = (PropertyItem*)malloc(nSize); //Allocate the appropriate space for the property pointer
m_pBmp->GetAllPropertyItems(nSize, m_nDelay, pItem);//Get attribute information
m_nDelay = ((long*)pItem->value)[0]; //Gets the delay of the first frame
free(pItem); //Release property object
delete [] pGuids; //Release the GUID
m_nIndex = 0; //Initializes a member variable
KillTimer(1);
SetTimer(1, 300, NULL);
}
}
void COutputGIFDlg::OnTimer(UINT nIDEvent)
{
GUID Guid = FrameDimensionTime; //Define a GUID
CDC* pDC = GetDC(); //Gets the device context of the window
//Define a memory canvas
CMemDC dc(pDC, CRect(20, 50, m_pBmp->GetWidth()+20, m_pBmp->GetHeight()+50));
Graphics gh(dc.m_hDC); //Define an image object
//Draws an image to the device context
gh.DrawImage(m_pBmp, 0, 0, m_pBmp->GetWidth(), m_pBmp->GetHeight());
m_pBmp->SelectActiveFrame(&Guid, m_nIndex++);//Set the next frame
if(m_nIndex == m_FrameCount) //Determines whether the current frame is a tail frame
m_nIndex = 0; //Sets the current frame to frame 1
CDialog::OnTimer(nIDEvent);
}
void COutputGIFDlg::OnCancel()
{
if (m_pBmp != NULL)
{
delete m_pBmp;
m_pBmp = NULL;
}
CDialog::OnCancel();
}