aswang
1.0
|
00001 /* 00002 Copyright 2003 Joseph Alvis 00003 00004 This file is part of Aswang. 00005 00006 Aswang is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU Lesser General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 Aswang is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public License 00017 along with Aswang. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include "DC.h" 00021 00022 using namespace aswang; 00023 00024 bool DC::Get() { 00025 Free(); 00026 if((dc = GetDC(fromWindow))!=NULL) 00027 return true; 00028 return false; 00029 } 00030 00031 void DC::Free() { 00032 if(dc != NULL) { 00033 UnselectAll(); 00034 if(fromWindow) 00035 _Release(); 00036 else 00037 _Delete(); 00038 dc = NULL; 00039 } 00040 } 00041 00042 void DC::_Release() { 00043 if(dc != NULL) { 00044 ReleaseDC(fromWindow,dc); 00045 } 00046 } 00047 00048 void DC::_Delete() { 00049 if(dc != NULL) { 00050 DeleteDC(dc); 00051 } 00052 } 00053 00054 DC &DC::operator = (HWND fw) { 00055 fromWindow=fw; 00056 Get(); 00057 return *this; 00058 } 00059 00060 DC &DC::operator = (HDC ndc) { 00061 dc = ndc; 00062 return *this; 00063 } 00064 00065 DC &DC::operator = (const DC &ndc) { 00066 dc = ndc.CreateCompatibleHDC(); 00067 return *this; 00068 } 00069 00070 void DC::SetPos(int x,int y) const { 00071 if(dc!=NULL) { 00072 MoveToEx(dc,x,y,NULL); 00073 } 00074 } 00075 00076 POINT DC::GetPos() const { 00077 POINT p; 00078 memset(&p,0,sizeof(p)); 00079 if(dc!=NULL) { 00080 GetCurrentPositionEx(dc,&p); 00081 } 00082 return p; 00083 } 00084 00085 void DC::DrawLine(int width,int height) { 00086 if(dc!=NULL) { 00087 POINT p=GetPos(); 00088 LineTo(dc,p.x+width,p.y+height); 00089 } 00090 } 00091 00092 void DC::DrawRect(int x,int y,int width,int height) { 00093 if(dc!=NULL) { 00094 Rectangle(dc,x,y,x+width,y+height); 00095 } 00096 } 00097 00098 void DC::DrawRect(const RECT *r) { 00099 if(dc!=NULL) { 00100 Rectangle(dc,r->left,r->top,r->right,r->bottom); 00101 } 00102 } 00103 00104 HDC DC::CreateCompatibleHDC() const { 00105 if(dc!=NULL) { 00106 return CreateCompatibleDC(dc); 00107 } 00108 return NULL; 00109 } 00110 00111 HBITMAP DC::CreateCompatibleBitmap() const { 00112 if(dc!=NULL) { 00113 return ::CreateCompatibleBitmap(dc,GetDeviceCaps(dc,HORZRES),GetDeviceCaps(dc,VERTRES)); 00114 } 00115 return NULL; 00116 } 00117 00118 void DC::Copy(const DC &dvc) { 00119 if(dc!=NULL && dvc.dc != NULL) { 00120 BitBlt(dc,0,0,GetDeviceCaps(dc,HORZRES),GetDeviceCaps(dc,VERTRES),dvc.dc,0,0,SRCCOPY); 00121 } 00122 } 00123 00124 void DC::Select(HGDIOBJ obj) { 00125 if(dc!=NULL) { 00126 DWORD type = GetObjectType(obj); 00127 HGDIOBJ oldobj = SelectObject(dc,obj); 00128 object_map[type].push_back(oldobj); 00129 } 00130 } 00131 00132 void DC::Unselect(HGDIOBJ obj) { 00133 if(dc!=NULL) { 00134 DWORD type = GetObjectType(obj); 00135 if(!object_map[type].empty()) { 00136 obj = object_map[type].back(); 00137 SelectObject(dc,obj); 00138 object_map[type].pop_back(); 00139 } 00140 } 00141 } 00142 00143 void DC::UnselectAll() { 00144 if(dc!=NULL) { 00145 object_map_type::iterator it; 00146 for(it=object_map.begin();it!=object_map.end();it++) { 00147 if(!it->second.empty()) { 00148 HGDIOBJ obj = it->second.front(); 00149 SelectObject(dc,obj); 00150 it->second.clear(); 00151 } 00152 } 00153 } 00154 } 00155 00156 void DC::SolidFill(HBRUSH brush) { 00157 if(dc!=NULL) { 00158 RECT r = {0,0,GetDeviceCaps(dc,HORZRES),GetDeviceCaps(dc,VERTRES)}; 00159 ::FillRect(dc,&r,brush); 00160 } 00161 } 00162 00163 void DC::FillRect(int x,int y,int width,int height,HBRUSH hbr) { 00164 if(dc!=NULL) { 00165 RECT r = {x,y,x+width,y+height}; 00166 ::FillRect(dc,&r,hbr); 00167 } 00168 } 00169 00170 void DC::FillRect(const RECT *r,HBRUSH hbr) { 00171 if(dc!=NULL) { 00172 ::FillRect(dc,r,hbr); 00173 } 00174 } 00175 00176 void DC::DrawAbsLine(int x,int y) { 00177 if(dc!=NULL) { 00178 LineTo(dc,x,y); 00179 } 00180 } 00181 00182 void DC::Draw(HICON hIcon,int destX,int destY) { 00183 if(dc!=NULL) { 00184 DrawIconEx(dc,destX,destY,hIcon,0,0,0,0,DI_NORMAL); 00185 } 00186 }