📄 gdi.c
字号:
/* Copyright (C) 2004-2005 Li Yudong*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "../include/common.h"#include "../include/hdc.h"#include "../include/rect.h"#include "../include/message.h"#include "../include/blockheap.h"#include "../include/invalidregion.h"#include "../include/clipregion.h"#include "../include/caret.h"#include "../include/winnc.h"#include "../include/winbase.h"#include "../include/window.h"#include "../include/bmp.h"#include "../include/framebuffer.h"#include "../include/gdi.h" COLORREF GUIAPI SetBkColor( HDC hDC, COLORREF crColor){ if(!hDC) return RGB(0,0,0); hDC->crTextBkColor=crColor; hDC->isTextBkTrans = false; return crColor;}COLORREF GUIAPI SetTextColor( HDC hDC, COLORREF crColor){ if(!hDC) return RGB(0,0,0); hDC->crTextColor=crColor; return crColor;}void SetTextBkTrans( HDC hDC){ if(!hDC) return; hDC->isTextBkTrans=true;}COLORREF GUIAPI inlineSetPixel( HDC hDC, int x, int y, COLORREF crColor){ if(hDC->iCoordType == DC_COORDTYPE_CLIENT){ return cliSetPixel(hDC,x,y,crColor); } else{ return winSetPixel(hDC,x,y,crColor); }}//client area coordsysCOLORREF inline cliSetPixel( HDC hDC, int x, int y, COLORREF crColor){ RECT rc; GetClientRect(hDC->hWnd,&rc); SetRect(&rc,0,0,rc.right - rc.left,rc.bottom - rc.top); if(PtInRect(&rc,x,y)){ ClientToWindow(hDC->hWnd,&x,&y); return winSetPixel(hDC,x,y,crColor); } return RGB(0,0,0);}//window area coordsysCOLORREF inline winSetPixel( HDC hDC, int x, int y, COLORREF crColor){ if(hDC->iType==DC_TYPE_WIN) return SetPixel_win(hDC, x, y, crColor); else return SetPixel_mem(hDC, x, y, crColor);}COLORREF inline SetPixel_win( HDC hDC, int x, int y, COLORREF crColor){ PClipRect pClipRect; pClipRect=((PWindowsTree)(hDC->hWnd))->pClipRgn->pHead; WindowToScreen(hDC->hWnd,&x,&y); while(pClipRect){ if(PtInRect(&(pClipRect->rect),x,y)){ lGUI_SetPixel(x,y,crColor); return crColor; } pClipRect=pClipRect->pNext; } return RGB(0,0,0);}COLORREF inline SetPixel_mem( HDC hDC, int x, int y, COLORREF crColor){ int iWinWidth,iWinHeight; int iBorder; COLORREF* pData; if(!hDC->pData) return RGB(0,0,0); iBorder = wndGetBorder(hDC->hWnd); iWinWidth =((BITMAP*)(hDC->hBitmap))->bmWidth; iWinHeight =((BITMAP*)(hDC->hBitmap))->bmHeight; if((x<0) || (x >=iWinWidth) || (y<0) || (y>=iWinHeight)) return RGB(0,0,0); pData=(PCOLORREF)(hDC->pData); *(pData + y*iWinWidth + x)=crColor; return crColor;}COLORREF GUIAPI inline GetPixel( HDC hDC, int x, int y){ if(hDC->iCoordType == DC_COORDTYPE_CLIENT) return cliGetPixel(hDC,x,y); else return winGetPixel(hDC,x,y);}COLORREF inline cliGetPixel( HDC hDC, int x, int y){ ClientToWindow(hDC->hWnd,&x,&y); return winGetPixel(hDC,x,y);}COLORREF inline winGetPixel( HDC hDC, int x, int y){ if(hDC->iType==DC_TYPE_WIN) return GetPixel_win(hDC, x, y); else return GetPixel_mem(hDC, x, y);}COLORREF inline GetPixel_win( HDC hDC, int x, int y){ PClipRect pClipRect; pClipRect=((PWindowsTree)(hDC->hWnd))->pClipRgn->pHead; WindowToScreen(hDC->hWnd,&x,&y); while(pClipRect){ if(PtInRect(&(pClipRect->rect),x,y)) return lGUI_GetPixel(x,y); pClipRect=pClipRect->pNext; } return RGB(0,0,0);}COLORREF inline GetPixel_mem( HDC hDC, int x, int y){ int iWinWidth,iWinHeight; COLORREF* pData; if(!hDC->pData) return RGB(0,0,0); iWinWidth =((BITMAP*)(hDC->hBitmap))->bmWidth; iWinHeight =((BITMAP*)(hDC->hBitmap))->bmHeight; if((x<0) || (x >=iWinWidth) || (y<0) || (y>=iWinHeight)) return RGB(0,0,0); pData=(PCOLORREF)(hDC->pData); return *(pData + y*iWinWidth + x);}//client coordsys BOOL GUIAPI MoveToEx( HDC hDC, int x, int y, LPPOINT lpPoint){ if(hDC->iCoordType = DC_COORDTYPE_CLIENT) return cliMoveToEx(hDC,x,y,lpPoint); else return winMoveToEx(hDC,x,y,lpPoint); }BOOL cliMoveToEx( HDC hDC, int x, int y, LPPOINT lpPoint){ //Convert coordsys to window coordsys ClientToWindow(hDC->hWnd,&x,&y); //Since the coordsys of hDC->point is window coordsys //we must convert it to the client coordsys before return WindowToClient(hDC->hWnd,&(lpPoint->x),&(lpPoint->y)); lpPoint->x = hDC->point.x; lpPoint->y = hDC->point.y; hDC->point.x = x; hDC->point.y = y; return true;}// window coordsysBOOL winMoveToEx( HDC hDC, int x, int y, LPPOINT lpPoint){ lpPoint->x=hDC->point.x; lpPoint->y=hDC->point.y; hDC->point.x=x; hDC->point.y=y; return true;}//client coordsysBOOL GUIAPI LineTo( HDC hDC, int x2, int y2){ if(hDC->iCoordType == DC_COORDTYPE_CLIENT) return cliLineTo(hDC,x2,y2); else return winLineTo(hDC,x2,y2);}//for we have to judeg whether this para point out range of client area//we sould call cliSetPixel function instead winSetPixelBOOL inlinecliLineTo( HDC hDC, int x2, int y2){ int winCoordx2,winCoordy2; float k; float dx,dy; float fx,fy; int x,y; int x1,y1; COLORREF crColor; if(!hDC) return false; x1=hDC->point.x; y1=hDC->point.y; //the point saved as current position in the hDC->point is window coordsys. //we must convert it to client coordsys before use it. WindowToClient(hDC->hWnd,&x1,&y1); //and we should convert the x2, y2 into window coordsys before store it into hDC->point winCoordx2 = x2; winCoordy2 = y2; ClientToWindow(hDC->hWnd,&winCoordx2,&winCoordy2); hDC->point.x = winCoordx2; hDC->point.y = winCoordy2; dx=(float)(x2-x1); dy=(float)(y2-y1); crColor=((PEN*)(hDC->hPen))->crPenColor; //a point if((x1==x2)&&(y1==y2)){ cliSetPixel(hDC,x1,y1,crColor); return true; } if(x1==x2){//vertical line if(y1>y2) swap(y1,y2); for(y=y1;y<=y2;y++) cliSetPixel(hDC,x1,y,crColor); return true; } if(y1==y2){//horizon line if(x1>x2) swap(x1,x2); for(x=x1;x<=x2;x++) cliSetPixel(hDC,x,y1,crColor); return true; } k=dy/dx; if(fabs(k)<=1){// x is loop control variable if(x1>x2){ swap(x1,x2); swap(y1,y2); } fy=(float)y1; for(x=x1;x<=x2;x++){ cliSetPixel(hDC,x,(int)(fy+0.5),crColor); fy=fy+k; } } else{//y is loop control variable if(y1>y2){ swap(x1,x2); swap(y1,y2); } fx=(float)x1; for(y=y1;y<=y2;y++){ cliSetPixel(hDC,(int)(fx+0.5),y,crColor); fx=fx+1/k; } } return true;}//window coordsys//we use this function in the no client area drawing functionBOOL inlinewinLineTo( HDC hDC, int x2, int y2){ float k; float dx,dy; float fx,fy; int x,y; int x1,y1; COLORREF crColor; if(!hDC) return false; x1=hDC->point.x; y1=hDC->point.y; hDC->point.x=x2; hDC->point.y=y2; dx=(float)(x2-x1); dy=(float)(y2-y1); crColor=((PEN*)(hDC->hPen))->crPenColor; //a point if((x1==x2)&&(y1==y2)){ winSetPixel(hDC,x1,y1,crColor); return true; } if(x1==x2){//vertical line if(y1>y2) swap(y1,y2); for(y=y1;y<=y2;y++) winSetPixel(hDC,x1,y,crColor); return true; } if(y1==y2){//horizon line if(x1>x2) swap(x1,x2); for(x=x1;x<=x2;x++) winSetPixel(hDC,x,y1,crColor); return true; } k=dy/dx; if(fabs(k)<=1){// x is loop control variable if(x1>x2){ swap(x1,x2); swap(y1,y2); } fy=(float)y1; for(x=x1;x<=x2;x++){ winSetPixel(hDC,x,(int)(fy+0.5),crColor); fy=fy+k; } } else{//y is loop control variable if(y1>y2){ swap(x1,x2); swap(y1,y2); } fx=(float)x1; for(y=y1;y<=y2;y++){ winSetPixel(hDC,(int)(fx+0.5),y,crColor); fx=fx+1/k; } } return true;}BOOL GUIAPIRectangle( HDC hDC, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect){ if(hDC->iCoordType == DC_COORDTYPE_CLIENT) return cliRectangle(hDC,nLeftRect,nTopRect,nRightRect,nBottomRect); else return winRectangle(hDC,nLeftRect,nTopRect,nRightRect,nBottomRect);}//client coordsysBOOL inline cliRectangle( HDC hDC, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect){ COLORREF crBackColor; COLORREF crColor; POINT Point; int iCount; if(!hDC) return false; //backup current point position Point.x = hDC->point.x; Point.y = hDC->point.y; if(!IsNullBrush(hDC)){ if(IsSolidBrush(hDC)){ crColor = GetSolidBrushColor(hDC); //backup pen color of current DC crBackColor = ((PEN*)(hDC->hPen))->crPenColor; ((PEN*)(hDC->hPen))->crPenColor = crColor; for(iCount = nTopRect+1;iCount<nBottomRect;iCount++){ cliMoveToEx(hDC,nLeftRect+1,iCount,&Point); cliLineTo(hDC,nRightRect-1,iCount); } //restore pen color of current DC ((PPEN)(hDC->hPen))->crPenColor=crBackColor; } } cliMoveToEx(hDC,nLeftRect,nTopRect,&Point); cliLineTo(hDC,nLeftRect,nBottomRect); cliLineTo(hDC,nRightRect,nBottomRect); cliLineTo(hDC,nRightRect,nTopRect); cliLineTo(hDC,nLeftRect,nTopRect); //restore current point position hDC->point.x = Point.x; hDC->point.y = Point.y; return true;}//window coordsys//we use this function in the no client area drawing functionBOOL inlinewinRectangle( HDC hDC, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect){ COLORREF crBackColor; COLORREF crColor; POINT Point; int iCount; if(!hDC) return false; //backup current point position Point.x = hDC->point.x; Point.y = hDC->point.y; if(!IsNullBrush(hDC)){ if(IsSolidBrush(hDC)){ crColor = GetSolidBrushColor(hDC); //backup pen color of current DC crBackColor = ((PEN*)(hDC->hPen))->crPenColor; ((PEN*)(hDC->hPen))->crPenColor = crColor; for(iCount = nTopRect+1;iCount<nBottomRect;iCount++){ winMoveToEx(hDC,nLeftRect+1,iCount,&Point); winLineTo(hDC,nRightRect-1,iCount); } //restore pen color of current DC ((PPEN)(hDC->hPen))->crPenColor=crBackColor; } } winMoveToEx(hDC,nLeftRect,nTopRect,&Point); winLineTo(hDC,nLeftRect,nBottomRect); winLineTo(hDC,nRightRect,nBottomRect); winLineTo(hDC,nRightRect,nTopRect); winLineTo(hDC,nLeftRect,nTopRect); //restore current point position hDC->point.x = Point.x; hDC->point.y = Point.y; return true;}BOOL GUIAPIFillRect( HDC hDC, RECT *lprc, HBRUSH hbr)//exclude border{ if(hDC->iCoordType == DC_COORDTYPE_CLIENT) cliFillRect(hDC,lprc,hbr); else winFillRect(hDC,lprc,hbr);}BOOL inlinecliFillRect( HDC hDC, RECT *lprc, HBRUSH hbr)//exclude border{ POINT ptOriginal,point; BRUSH* pBrush; COLORREF crColor,crBackColor; int i; if(!hDC) return false; pBrush = (BRUSH*)hbr; //backup pen color crBackColor = ((PEN*)(hDC->hPen))->crPenColor; crColor = pBrush->crBrushColor; ((PEN*)(hDC->hPen))->crPenColor = crColor; //back original hdc point position ptOriginal.x = hDC->point.x; ptOriginal.y = hDC->point.y; for(i=lprc->top+1;i<lprc->bottom;i++){ cliMoveToEx(hDC,lprc->left+1,i,&point); cliLineTo(hDC,lprc->right-1,i); } //restore pen color ((PEN*)(hDC->hPen))->crPenColor = crBackColor; //restore original hdc point position hDC->point.x = ptOriginal.x; hDC->point.y = ptOriginal.y; return true; }BOOL winFillRect( HDC hDC, RECT *lprc, HBRUSH hbr)//exclude border{ POINT ptOriginal,point; BRUSH* pBrush; COLORREF crColor,crBackColor; int i; if(!hDC) return false; pBrush = (BRUSH*)hbr; //backup pen color crBackColor = ((PEN*)(hDC->hPen))->crPenColor; crColor = pBrush->crBrushColor; ((PEN*)(hDC->hPen))->crPenColor = crColor; //back original hdc point position ptOriginal.x = hDC->point.x; ptOriginal.y = hDC->point.y; for(i=lprc->top+1;i<lprc->bottom;i++){ winMoveToEx(hDC,lprc->left+1,i,&point); winLineTo(hDC,lprc->right-1,i); } //restore pen color ((PEN*)(hDC->hPen))->crPenColor = crBackColor; //restore original hdc point position hDC->point.x = ptOriginal.x; hDC->point.y = ptOriginal.y; return true; }BOOL GUIAPI FillRectangle( HDC hDC, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, COLORREF crColor, COLORREF crFillColor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -