📄 mainframe.cpp
字号:
if(ElapsedTime > FLT_EPSILON || ElapsedTime < -FLT_EPSILON)
m_Fps.Add(1.0 / ElapsedTime);
// Show framerate
CString Buffer;
Buffer.Format("%3.1f fps", m_Fps.Avg());
m_StatusBar.SetPaneText(ID_FPS_PANE, Buffer);
}
CATCH_MSGBOX( "CMainFrame::OnGrabFinished" )
return 0;
}
/*!
Called each time all messages in the message queue of the GUI thread are processed and
no more messages are waiting.
- If the #m_BufferQueue contains buffer indices these are removes. For each buffer
a new grab command is enqueued. When the grab commands are finished they turn up in
the #GrabThreadProc. The most recent buffer is put to m_view for display.
- Updates the user interface's enable stati.
*/
BOOL CMainFrame::OnIdle()
{
try
{
// If the grab is to be stopped, don't enqueue buffers any more
if(m_LiveGrabbing)
{
// For all buffer which might have been pushed to the queue by OnGrabFinished
while(0 < m_BufferQueue.size())
{
// Get the buffer's index from the queue
unsigned int BufferIndex = m_BufferQueue.front();
m_BufferQueue.pop();
// If more than one buffer is in the queue display only the most recent
if(0 < m_BufferQueue.size() )
{
// Enqueue a new Grab command
SubmitGrabCommand(BufferIndex);
}
else
{
// Update View
m_view.SetBitmap(m_ptrBitmaps[BufferIndex], m_AoiPositions[BufferIndex]);
// Submit image last displayed
if(m_DisplayedBufferIndex >=0)
{
// Enqueue a new Grab command
SubmitGrabCommand(m_DisplayedBufferIndex);
}
m_DisplayedBufferIndex = BufferIndex;
}
}
}
// Update the user interface
UpdateUI();
}
CATCH_MSGBOX( "CMainFrame::OnIdle" )
return FALSE;
}
/*!
Called if the user wants the live grab to stop. Enqueues a #NotifyQuit message to the grab
queue. Sets the #m_LiveGrabbing flag to false to prevent new grab commands from being enqueued.
When all commands currently being in the queue are finished the NotifyQuit message will turn up
in the #GrabThreadProc.
*/
LRESULT CMainFrame::OnGrabStop(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
try
{
// This will prevent new images from being enqueued
m_LiveGrabbing = false;
// Enqueue a "clean-up" message which will show up after all images being currently
// in the queue have been received
m_Bcam.Notify(NotifyQuit, NULL);
}
CATCH_MSGBOX( "CMainFrame::OnGrabStop" )
return 0;
}
/*!
Called if the #GrabThreadProc receives a #NotifyQuit message. This means that the command queue is
empty now and everything can be cleaned up.
*/
LRESULT CMainFrame::OnGrabStopped(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
try
{
// Clean up queue
while(0 < m_BufferQueue.size())
m_BufferQueue.pop();
// forget about the displayed buffer
m_DisplayedBufferIndex = -1;
// shutdown thread
m_GrabThread.Release(); // this waits for the thread to die
// Disable ISO_ENABLE
m_Bcam.ContinuousShot = false;
// Cancel pending I/O requests
m_Bcam.Cancel();
// Release bandwidth, isoch channels and isoch transfer contexts
// This is optional since closing the driver will trigger this automatically
m_Bcam.FreeResources();
// Close driver
m_Bcam.Close();
// We could take away the Bitmap from the view
//but since it's reference counted we leavi ti there for display
// Delete bitmaps
for(int i=0; i<NUM_BUFFERS; ++i)
m_ptrBitmaps[i].Release();
// Reset frame rate display
m_Fps.Reset();
m_StatusBar.SetPaneText(ID_FPS_PANE, "");
}
CATCH_MSGBOX( "CMainFrame::OnGrabStopped" )
return 0;
}
/*!
Called if an error occurred in the #GrabThreadProc. Shows an error message box.
*/
LRESULT CMainFrame::OnError(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// Show the error message coming from another thread
BcamException* pE = (BcamException*)wParam;
CString Buffer, B;
Buffer += (B.Format("Exception 0x%X occurred\n", pE->Error() ), B);
Buffer += (B.Format("Message = %s\n", pE->Description() ), B);
Buffer += (B.Format("Context = %s\n", pE->Context()), B);
MessageBox(Buffer, _T("CMainFrame::OnError"), MB_OK | MB_ICONEXCLAMATION);
delete pE;
return 0;
}
/*!
Makes sure the application terminates cleanly if closed via the system menu
*/
LRESULT CMainFrame::OnSysCommand(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
{
if(wParam == SC_CLOSE)
{
if(m_LiveGrabbing)
{
// Enqueue a "clean-up" message which will show up after all images being currently
// in the queue have been received
m_Bcam.Notify(NotifyQuit, NULL);
// Wait until the GrabThread died
::WaitForSingleObject((HANDLE)m_GrabThread,INFINITE);
// The rest will clean up itselfe via the destructors
}
}
bHandled = false;
return 0;
}
LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if((bool)m_view.m_ptrBitmap)
m_view.ReleaseBitmap();
return 0;
}
LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CFileDialog dlg(TRUE, _T("bmp"), NULL, 0, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
if(dlg.DoModal() == IDOK)
{
try
{
CDibPtr ptrBitmap;
ptrBitmap.LoadBMP(dlg.m_szFileName);
m_view.SetBitmap(ptrBitmap, CPoint(0,0));
m_view.SetImageSize(ptrBitmap->GetSize());
}
CATCH_MSGBOX( "CMainFrame::OnFileOpen" )
}
return 0;
}
LRESULT CMainFrame::OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
if(!m_view.m_ptrBitmap)
return 0;
CFileDialog dlg(FALSE, _T("bmp"), _T("Temp.bmp"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Bitmap Files (*.bmp)\0*.bmp\0All Files (*.*)\0*.*\0"), m_hWnd);
if(dlg.DoModal() == IDOK)
{
try
{
m_view.m_ptrBitmap->StoreBMP(dlg.m_szFileName);
}
CATCH_MSGBOX( "CMainFrame::OnFileSave" )
}
return 0;
}
LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
try
{
if(m_LiveGrabbing)
{
// Enqueue a "clean-up" message which will show up after all images being currently
// in the queue have been received
m_Bcam.Notify(NotifyQuit, NULL);
// Wait until the GrabThread died
::WaitForSingleObject((HANDLE)m_GrabThread,INFINITE);
// The rest will clean up itselfe via the destructors
}
}
CATCH_MSGBOX( "CMainFrame::OnFileExit" )
PostMessage(WM_CLOSE);
return 0;
}
LRESULT CMainFrame::OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
try
{
if(!m_LiveGrabbing && (bool)m_view.m_ptrBitmap)
{
m_view.m_ptrBitmap->CopyToClipboard();
}
}
CATCH_MSGBOX( "CMainFrame::OnEditCopy" )
return 0;
}
void CMainFrame::UpdateUI()
{
// update UI elements
UIEnable(ID_FILE_NEW, !m_LiveGrabbing);
UIEnable(ID_FILE_OPEN, !m_LiveGrabbing);
UIEnable(ID_FILE_SAVE, !m_LiveGrabbing && (bool)m_view.m_ptrBitmap);
UIEnable(ID_GRAB_LIVE, !m_LiveGrabbing);
UIEnable(ID_GRAB_STOP, m_LiveGrabbing);
UIEnable(ID_EDIT_COPY, !m_LiveGrabbing && (bool)m_view.m_ptrBitmap);
// These functions are not yet implemented so they have to be disabled always
UIEnable(ID_FILE_PRINT, false);
UIEnable(ID_FILE_PRINT_PREVIEW, false);
UIEnable(ID_FILE_PRINT_SETUP, false);
UIUpdateToolBar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -