demoview.cpp
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C++ 代码 · 共 824 行 · 第 1/2 页
CPP
824 行
SetImageRectSelection(pDoc,&(m_tracker.m_rect));
} else {
m_tracker.m_rect = CRect(0,0,0,0);
}
} else {
if (m_tracker.Track(this, point, true)){
m_tracker.m_rect.NormalizeRect();
SetImageRectSelection(pDoc,&(m_tracker.m_rect));
} else {
m_tracker.m_rect = CRect(0,0,0,0);
}
}
OnUpdate(0,0,0);
}
break;
case 3: //text
pDoc->m_tool=-1;
CxImage* ima = pDoc->GetImage();
if (ima){
pDoc->SubmitUndo();
long x = point.x;
long y = point.y;
GetImageXY(pDoc, ima, &x,&y);
#ifndef VATI_EXTENSIONS
RGBQUAD c = ima->RGBtoRGBQUAD(pDoc->m_color);
c.rgbReserved=255;
ima->DrawString(0,x,y,pDoc->m_text,c,
pDoc->m_font.lfFaceName,
pDoc->m_font.lfHeight,
pDoc->m_font.lfWeight,
pDoc->m_font.lfItalic,
pDoc->m_font.lfUnderline,
true);
#else
ima->DrawStringEx(0,x,y,&theApp.m_text );
#endif
}
Invalidate(0);
break;
}
if (pDoc->GetWaitingClick()){
pDoc->SetWaitingClick(0);
CxImage* ima = pDoc->GetImage();
if (ima) {
long x = point.x;
long y = point.y;
GetImageXY(pDoc, ima, &x,&y);
if (ima->IsInside(x,y)) {
pDoc->SubmitUndo();
long yflip = ima->GetHeight() - y - 1;
ima->SetTransIndex(ima->GetPixelIndex(x,yflip));
// <DP> RGB transparency
ima->SetTransColor(ima->GetPixelColor(x,yflip));
pDoc->UpdateAllViews(NULL);
}
}
}
#if CXIMAGE_DEMO_SELECT
else {
KillTimer(1);
pDoc->m_NumSel=0;
m_SelShift=0;
}
#endif
}
CScrollView::OnLButtonDown(nFlags, point);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnLButtonUp(UINT nFlags, CPoint point)
{
CDemoDoc* pDoc = GetDocument();
#if CXIMAGE_DEMO_SELECT
if (pDoc && pDoc->m_tool == 1){
CxImage* ima=pDoc->GetImage();
if (ima){
int i=pDoc->m_NumSel;
if (i>2){
#ifdef VATI_EXTENSIONS
if ( !pDoc->m_isRectSel )
{
pDoc->m_Sel[i].x = pDoc->m_Sel[0].x;
pDoc->m_Sel[i].y = pDoc->m_Sel[0].y;
pDoc->m_NumSel++;
}
#else
pDoc->m_Sel[i].x = pDoc->m_Sel[0].x;
pDoc->m_Sel[i].y = pDoc->m_Sel[0].y;
pDoc->m_NumSel++;
#endif
SetTimer(1,333,NULL);
long i;
long n=pDoc->m_NumSel;
POINT* p = (POINT*)malloc(n*sizeof(POINT));
long h=ima->GetHeight();
for(i=0;i<n;i++){
p[i].x = pDoc->m_Sel[i].x;
p[i].y = h-1-pDoc->m_Sel[i].y;
}
pDoc->Stopwatch(0);
ima->SelectionClear();
ima->SelectionAddPolygon(p,n);
pDoc->Stopwatch(1);
pDoc->UpdateStatusBar();
free(p);
} else {
ima->SelectionDelete();
}
Invalidate(0);
}
}
#endif
CScrollView::OnLButtonUp(nFlags, point);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
switch (lHint)
{
case WM_USER_NEWIMAGE:
{
m_tracker.m_rect = CRect(0,0,0,0);
CDemoDoc* pDoc = GetDocument();
CxImage* ima = pDoc->GetImage();
if (ima) {
int px=GetScrollPos(SB_HORZ);
int py=GetScrollPos(SB_VERT);
CSize sz(GetTotalSize());
int x=(int)(ima->GetWidth()*pDoc->GetZoomFactor());
int y=(int)(ima->GetHeight()*pDoc->GetZoomFactor());
SetScrollSizes(MM_TEXT, CSize(x,y));
CSize sz2(GetTotalSize());
CWnd* pFrame=GetParentFrame();
RECT rClient;
pFrame->GetClientRect(&rClient);
if (sz.cx!=0 && sz.cy!=0){
if (x>rClient.right) SetScrollPos(SB_HORZ,sz2.cx*px/sz.cx); else SetScrollPos(SB_HORZ,0);
if (y>rClient.bottom) SetScrollPos(SB_VERT,sz2.cy*py/sz.cy); else SetScrollPos(SB_VERT,0);
}
if (!(pFrame->IsIconic()||pFrame->IsZoomed())){
RECT rMainCl,rFrame,rMainFr;
((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetClientRect(&rMainCl);
((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetWindowRect(&rMainFr);
pFrame->GetWindowRect(&rFrame);
pFrame->SetWindowPos(0,0,0,
(4+rFrame.right-rFrame.left-rClient.right+rClient.left)+
min(rMainCl.right-(rFrame.left-rMainFr.left+12),x),
(4+rFrame.bottom-rFrame.top-rClient.bottom+rClient.top)+
min(rMainCl.bottom-(rFrame.top-rMainFr.top+12),y),
SWP_NOMOVE|SWP_NOZORDER);
//ResizeParentToFit(1);
}
if (!ima->SelectionIsValid()) KillTimer(1);
#ifdef VATI_EXTENSIONS
ima->SetJpegQuality(theApp.m_optJpegQuality);
#endif
CMainFrame* pMain = (CMainFrame*) AfxGetMainWnd();
if (pMain->m_HistoBar.IsWindowVisible()){
pDoc->m_hmax=ima->Histogram(pDoc->m_hr,pDoc->m_hg,pDoc->m_hb,pDoc->m_hgray);
pMain->m_HistoBar.Invalidate();
} else {
pDoc->m_hmax = 0;
}
}
break;
}
default:
{
CDemoDoc* pDoc = GetDocument();
if (pDoc){
CxImage* ima = pDoc->GetImage();
if (ima){
if (pDoc->GetStretchMode()) SetScrollSizes(MM_TEXT, CSize(0,0));
else SetScrollSizes(MM_TEXT,CSize((int)(ima->GetWidth()*pDoc->GetZoomFactor()),
(int)(ima->GetHeight()*pDoc->GetZoomFactor())));
}
}
}
}
CScrollView::OnUpdate(pSender, lHint, pHint);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnContextMenu(CWnd* pWnd, CPoint point)
{
CDemoDoc* pDoc = GetDocument();
if (pDoc && pDoc->m_tool==2) return;
#ifdef VATI_EXTENSIONS
// suppress context menu if right-click function was called just now
if ( m_suppressContextMenu == true )
return;
#endif
static UINT toolbars[]={
IDR_MAINFRAME,
IDR_TOOLBAR1,
};
BCMenu menu;
menu.LoadMenu(IDM_CXIMAGE1);
menu.LoadToolbars(toolbars,2);
CMenu *pContextMenu=menu.GetSubMenu(0);
pContextMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd());
}
//////////////////////////////////////////////////////////////////////////////
LRESULT CDemoView::OnNewImage(WPARAM wParam, LPARAM lParam)
{
CDemoDoc* pDoc = GetDocument();
pDoc->UpdateAllViews(0,WM_USER_NEWIMAGE);
pDoc->UpdateStatusBar();
return 0;
}
//////////////////////////////////////////////////////////////////////////////
LRESULT CDemoView::OnNewProgress(WPARAM wParam, LPARAM lParam)
{
CStatusBar& statusBar = ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetStatusBar();
CString s;
s.Format("%d %%",(int)wParam);
statusBar.SetPaneText(2, s);
statusBar.Invalidate(1);
/* CProgressCtrl& progressBar = ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetProgressBar();
progressBar.SetPos((int)wParam);
progressBar.Invalidate(1);*/
return 0;
}
//////////////////////////////////////////////////////////////////////////////
#if CXIMAGE_DEMO_SELECT
void CALLBACK LineDDAProc(int x, int y, CDemoView *pview)
{
CDC* pDC = pview->GetDC();
if (pDC) {
pview->m_SelCount++;
if (pview->m_SelCount <= 4) pDC->SetPixelV(x,y,RGB(255,255,255));
else pDC->SetPixelV(x,y,RGB(0,0,0));
if (pview->m_SelCount == 8) pview->m_SelCount=0;
}
pview->ReleaseDC(pDC); //<jh>
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::DrawSelection()
{
CDemoDoc* pDoc = GetDocument();
if (pDoc->m_NumSel>2){
CPoint pos(GetScrollPosition());
float zoom=pDoc->GetZoomFactor();
m_SelCount=m_SelShift;
// pDC->MoveTo(zoom*pDoc->m_Sel[0].x - pos.x,zoom*pDoc->m_Sel[0].y - pos.y);
for(int i=1;i<pDoc->m_NumSel;i++){
// pDC->LineTo(zoom*pDoc->m_Sel[i].x - pos.x,zoom*pDoc->m_Sel[i].y - pos.y);
LineDDA((int)(zoom*pDoc->m_Sel[i-1].x - pos.x),
(int)(zoom*pDoc->m_Sel[i-1].y - pos.y),
(int)(zoom*pDoc->m_Sel[i].x - pos.x),
(int)(zoom*pDoc->m_Sel[i].y - pos.y),
(LINEDDAPROC)LineDDAProc,(LPARAM)this);
}
}
}
#endif
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnDestroy()
{
CScrollView::OnDestroy();
((CMainFrame*)AfxGetMainWnd())->m_HistoBar.Invalidate();
#if CXIMAGE_DEMO_SELECT
KillTimer(1);
#endif
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnTimer(UINT nIDEvent)
{
#if CXIMAGE_DEMO_SELECT
m_SelShift++;
if (m_SelShift==8) m_SelShift=0;
DrawSelection();
#endif
CScrollView::OnTimer(nIDEvent);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_ESCAPE){
CMainFrame* pMain = (CMainFrame*) AfxGetMainWnd();
if (pMain->m_fullscreen){
pMain->PostMessage(WM_COMMAND, ID_VIEW_FULLSCREEN);
}
}
CScrollView::OnChar(nChar, nRepCnt, nFlags);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnRButtonDown(UINT nFlags, CPoint point)
{
CDemoDoc* pDoc = GetDocument();
if (pDoc) {
switch(pDoc->m_tool){
case 1:
{
#ifdef VATI_EXTENSIONS
m_suppressContextMenu = false; //default: Context Menu can be displayed on ButtonUp
CxImage *ima = pDoc->GetImage();
if ( !(nFlags & MK_CONTROL) && pDoc->m_isRectSel && pDoc->m_NumSel == 5 && ima )
{
// calculate origo
m_orgPnt.x = (pDoc->m_Sel[0].x + pDoc->m_Sel[1].x)/2;
m_orgPnt.y = (pDoc->m_Sel[0].y + pDoc->m_Sel[3].y)/2;
for(int i=0;i<5;i++)
{
// copy corner coordinates to double-prec array
m_dpnt[i].x = pDoc->m_Sel[i].x ;
m_dpnt[i].y = pDoc->m_Sel[i].y ;
}
// get real coo from point
long x = point.x;
long y = point.y;
GetImageXY(pDoc, ima, &x,&y);
// initialise old point var.
m_oldPnt.x = x;
m_oldPnt.y = y;
m_suppressContextMenu = true; // ButtonUp should not invoke context menu
}
else if ( nFlags & MK_CONTROL && ima /* && pDoc->m_isRectSel && pDoc->m_NumSel == 5 */)
{
// get real coo from point
long x = point.x;
long y = point.y;
GetImageXY(pDoc, ima, &x,&y);
// initialise old point var.
m_oldPnt.x = x;
m_oldPnt.y = y;
m_suppressContextMenu = true; // ButtonUp should not invoke context menu
}
#endif
}
break;
case 2:
if (!pDoc->GetWaitingClick()) PostMessage(WM_COMMAND,ID_VIEW_ZOOMOUT);
return;
break;
#ifdef VATI_EXTENSIONS
default:
m_suppressContextMenu = false; //default: Context Menu can be displayed on ButtonUp
#endif
} // end switch
} // end if pDoc
CScrollView::OnRButtonDown(nFlags, point);
}
//////////////////////////////////////////////////////////////////////////////
void CDemoView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
if (bActivate){
((CMainFrame*)AfxGetMainWnd())->m_HistoBar.Invalidate();
}
CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
//////////////////////////////////////////////////////////////////////////////
BOOL CDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (m_tracker.SetCursor( pWnd, nHitTest )) return TRUE;
return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}
//////////////////////////////////////////////////////////////////////////////
bool CDemoView::SetImageRectSelection(CDemoDoc *pDoc,CRect *rect)
{
if (pDoc==0)
return false;
CxImage* ima = pDoc->GetImage();
if (ima==0)
return false;
long x,y;
RECT rect_img;
x = rect_img.left = rect->left;
y = rect_img.top = rect->top;
GetImageXY(pDoc, ima, &x,&y);
rect_img.top = ima->GetHeight() - 1 - y;
rect_img.left = x;
x = rect_img.right = rect->right;
y = rect_img.bottom = rect->bottom;
GetImageXY(pDoc, ima, &x,&y);
rect_img.bottom = ima->GetHeight() - 1 - y;
rect_img.right = x;
ima->SelectionClear();
ima->SelectionAddRect(rect_img);
return true;
}
//////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?