www.pudn.com > GDIyuvplayer.rar > DisplayWin.cpp


// DisplayWin.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "YUVPlayer.h" 
#include "DisplayWin.h" 
#include "YUVPlayerDlg.h" 
 
#include "vfw.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
HDRAWDIB m_hdd; 
 
 
extern CEvent eventStart; 
extern CEvent ReadKill,ConvertKill; 
extern char pathName[256]; 
CYUVPlayerDlg *pParent; 
///////////////////////////////////////////////////////////////////////////// 
// DisplayWin 
 
IMPLEMENT_DYNCREATE(DisplayWin, CFrameWnd) 
 
DisplayWin::DisplayWin() 
{ 
	//	m_hdd = DrawDibOpen();//ljz 
} 
 
DisplayWin::DisplayWin(CFrameWnd* pParentWnd,int Width,int Height) 
{ 
	DWORD AttrStyle; 
	m_FullScreen = FALSE; 
	dWidth       = Width;  
	dHeight      = Height; 
	pParent      = (CYUVPlayerDlg*) pParentWnd; 
    getSeqName(pathName, seqName); 
	if (NULL == (RGBbuf = (unsigned char *)malloc(dWidth * dHeight * 3) ) ) 
	{ 
		AfxMessageBox("Couldn't allocate memory for RGBbuf\n"); 
		exit(0); 
	}	 
    AttrStyle = WS_OVERLAPPEDWINDOW; 
    bmpHeap   = HeapCreate(NULL,sizeof(BITMAPINFOHEADER),NULL); 
	bmpInfo   = (LPBITMAPINFO)HeapAlloc(bmpHeap,HEAP_ZERO_MEMORY,sizeof(BITMAPINFOHEADER)); 
	if (bmpInfo == NULL)  
	{ 
		AfxMessageBox("heap allocate is failed "); 
		exit(0);  
	}     
	Create(NULL, "YUVPlayer", AttrStyle, rectDefault, NULL); 
	//pParentWnd  Specifies the parent window of this frame window. This parameter 
	//should be NULL for top-level frame windows. 
	pParentWnd->GetIcon(FALSE); 
	SetIcon(pParentWnd->GetIcon(FALSE),FALSE);///设定播放窗口图标 
} 
 
void  DisplayWin::Endfullscreen() 
{    
	m_FullScreen = FALSE; 
	ShowWindow(SW_HIDE); 
	ModifyStyle(NULL, WS_OVERLAPPEDWINDOW); 
	SetWindowPlacement(&m_OldWndPlacement);	 
} 
 
DisplayWin::~DisplayWin() 
{ 
	HeapFree(bmpHeap,NULL,bmpInfo);	 
	HeapDestroy(bmpHeap); 
	free(RGBbuf); 
} 
 
BEGIN_MESSAGE_MAP(DisplayWin, CFrameWnd) 
//{{AFX_MSG_MAP(DisplayWin) 
ON_WM_CREATE() 
ON_WM_PAINT() 
	ON_WM_CLOSE() 
	ON_WM_SIZE() 
	ON_WM_KEYDOWN() 
ON_WM_TIMER() 
	ON_WM_LBUTTONDBLCLK() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
void DisplayWin::FullScreen() 
{ 
    GetWindowPlacement( & m_OldWndPlacement);	 
	ModifyStyle(WS_THICKFRAME|WS_CAPTION,NULL);//修改窗体属性  
	rcFlScreen.left   = 0; 
	rcFlScreen.top    = 0; 
	rcFlScreen.right  = GetSystemMetrics(SM_CXSCREEN); 
	rcFlScreen.bottom = GetSystemMetrics(SM_CYSCREEN); 
	m_FullScreen      = TRUE; 
    MoveWindow(&rcFlScreen);  
} 
 
void DisplayWin::CenterWindow(int width, int height) 
{ 
	RECT rc; 
    RECT rw;    
    int cyBorder, cxBorder; 
    int cyTotal,cxTotal; 
    int cyMenuAndCaption;	 
	RECT r; 
	int nCx  = GetSystemMetrics(SM_CXSCREEN); 
	int	nCy  = GetSystemMetrics(SM_CYSCREEN);   
    cxBorder = GetSystemMetrics(SM_CXBORDER); 
	cyBorder = GetSystemMetrics(SM_CYBORDER); 
    // Figure out the height of the menu, Toolbar, and caption 
    GetWindowRect(&rw); 
    GetClientRect(&rc);	 
    ClientToScreen ((LPPOINT) &rc); 
    cyMenuAndCaption = (rc.top -  rw.top) ;	 
    cyTotal          = height  +  cyMenuAndCaption + cyBorder * 2 ; 
    cxTotal          = width   +  cxBorder * 2;            	 
	r.left           = (nCx    -  cxTotal) / 2; 
	r.top            = (nCy    -  cyTotal) / 4; 
	r.right          = (nCx    +  cxTotal) / 2; 
	r.bottom         = nCy / 4 +  cyTotal  * 0.75; 
 //  SetWindowPos(&wndTop ,r.left,r.top,cxTotal,cyTotal,SWP_NOACTIVATE    ); 
	MoveWindow(&r,FALSE);//选择FALSE,防止再次调用OnPaint()函数;	 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// DisplayWin message handlers 
int DisplayWin::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
		return -1;		 
    bmpInfo -> bmiHeader.biSize        = sizeof(BITMAPINFOHEADER); 
	bmpInfo -> bmiHeader.biWidth       = dWidth; 
	bmpInfo -> bmiHeader.biHeight      = -dHeight;// see "BITMAPINFOHEADER" in MSDN library  
	bmpInfo -> bmiHeader.biPlanes      = 1; 
	bmpInfo -> bmiHeader.biBitCount    = 24; 
    bmpInfo -> bmiHeader.biCompression = BI_RGB; 
    bmpInfo -> bmiHeader.biSizeImage   = 0;	 
	return 0; 
} 
 
void DisplayWin::ShowImage(CDC *pdc, unsigned char *pImage) 
{ 
	int iRet; 
	pdc -> SetStretchBltMode(STRETCH_DELETESCANS); 
	GetClientRect(&rcClient); 
	if(m_FullScreen == FALSE) 
	{ 
		iRet = StretchDIBits(pdc -> m_hDC, 
			// destination rectangle 
			0, 0, rcClient.right, rcClient.bottom, //在这里进行改动,使全屏图片适应窗口大小 
			// source rectangle 
			0, 0, dWidth, dHeight, 
			pImage,  
			bmpInfo, 
			DIB_RGB_COLORS, 
			SRCCOPY); 
	} 
	else 
	{ 
		iRet = StretchDIBits(pdc -> m_hDC, 
			// destination rectangle 
			0, 0, rcFlScreen.right, rcFlScreen.bottom, //在这里进行改动,使全屏图片适应窗口大小 
			// source rectangle 
			0, 0, dWidth, dHeight, 
			pImage,  
			bmpInfo, 
			DIB_RGB_COLORS, 
			SRCCOPY); 
	} 
	if (iRet == GDI_ERROR) 
        return ; 
} 
 
void DisplayWin::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	if (!RGBbuf)  return; 
	SetWindowText(seqName); 
	ShowImage(&dc,RGBbuf); 
} 
 
void DisplayWin::PostNcDestroy()  
{ 
	delete this;	 
//	CFrameWnd::PostNcDestroy(); 
} 
 
 
void DisplayWin::OnClose()  
{    
	if(*m_pPointToMe != NULL) 
	{	 
	  ReadKill.SetEvent(); 
	  ConvertKill.SetEvent(); 
	  *m_pPointToMe = NULL; 
      pParent -> OnCloseAll(); 
	} 
	CFrameWnd::OnClose(); 
} 
 
void DisplayWin::OnSize(UINT nType, int cx, int cy)  
{ 
	CFrameWnd::OnSize(nType, cx, cy); 
	GetClientRect(&rcClient); 
	InvalidateRect (NULL,FALSE); 
	UpdateWindow (); 
} 
 
 
void DisplayWin::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	if(nChar == VK_ESCAPE) // 如果按的键为Esc键  
	{  
	  Endfullscreen(); 
	} 
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
void DisplayWin::OnLButtonDblClk(UINT nFlags, CPoint point)  
{ 
   if (m_FullScreen == TRUE)  
	   Endfullscreen(); 
   else 
	   FullScreen(); 
	CFrameWnd::OnLButtonDblClk(nFlags, point); 
}