PAINT_BK.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:9k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1.   1 #include <windows.h>
  2.   2 #include "paint.h"
  3.   3 
  4.   4 extern int OX, OY;
  5.   5 extern int CX, CY;
  6.   6 
  7.   7 extern HDC hMemDC;
  8.   8 
  9.   9 extern POINT OrgPoint;
  10.  10 extern POINT PrePoint;
  11.  11 extern POINT CurPoint;
  12.  12 
  13.  13 extern BOOL  CanUndo;
  14.  14 
  15.  15 BOOL    bBounded  = FALSE;
  16.  16 BOOL    bSelected = FALSE;
  17.  17 
  18.  18 HDC     hBkDC;
  19.  19 HBITMAP hBkBitmap;
  20.  20 RECT    BoundRect;
  21.  21 RECT    OrgRect;
  22.  22 
  23.  23 int     Width, Height;
  24.  24 
  25.  25 void CancelBound(HDC hDC, HMENU hMenu)
  26.  26 {
  27.  27    int  nDrawMode;
  28.  28    HPEN hPen;
  29.  29 
  30.  30    if (! bBounded) return;
  31.  31 
  32.  32    hPen = CreatePen(PS_DOT, 1, 0);
  33.  33    hPen = SelectObject(hDC, hPen);
  34.  34    nDrawMode = SetROP2(hDC, R2_NOTXORPEN);
  35.  35 
  36.  36    Rectangle(hDC, BoundRect.left, BoundRect.top,
  37.  37                   BoundRect.right, BoundRect.bottom);
  38.  38 
  39.  39    SetROP2(hDC, nDrawMode);
  40.  40    DeleteObject(SelectObject(hDC, hPen));
  41.  41 
  42.  42    DeleteDC(hBkDC);
  43.  43    DeleteObject(hBkBitmap);
  44.  44 
  45.  45    EnableMenuItem(hMenu, IDM_COPY, MF_GRAYED);
  46.  46    EnableMenuItem(hMenu, IDM_CUT, MF_GRAYED);
  47.  47    bBounded = FALSE;
  48.  48    bSelected = FALSE;
  49.  49 }
  50.  50 
  51.  51 
  52.  52 void RecoverBlock()
  53.  53 {
  54.  54    if (bSelected)
  55.  55      {
  56.  56        BitBlt(hMemDC,
  57.  57               OX+OrgRect.left, OY+OrgRect.top,
  58.  58               Width, Height,
  59.  59               hBkDC, 0, 0,
  60.  60               SRCCOPY);
  61.  61        bSelected = FALSE;
  62.  62      }
  63.  63 }
  64.  64 
  65.  65 void BoundBlock(HDC hDC, HMENU hMenu, BOOL bSure)
  66.  66 {
  67.  67    int         nDrawMode;
  68.  68    int         OffX, OffY;
  69.  69    BOOL        bInside;
  70.  70    HPEN        hPen;
  71.  71    static BOOL bMoving = FALSE;
  72.  72 
  73.  73    hPen = CreatePen(PS_DOT, 1, 0);
  74.  74    hPen = SelectObject(hDC, hPen);
  75.  75    nDrawMode = SetROP2(hDC, R2_NOTXORPEN);
  76.  76 
  77.  77    bInside = TRUE;
  78.  78 
  79.  79    if (!bBounded ||
  80.  80        (!bMoving &&
  81.  81         (bInside=PtInRect(&BoundRect, CurPoint))==FALSE))
  82.  82 
  83.  83      {
  84.  84        if (! bInside)
  85.  85          BackUpGraph(hDC, hMenu, OX, OY, CX, CY, 0, 0);
  86.  86             /* Also call CancelBound() */
  87.  87             /*  to set bBounded FALSE  */
  88.  88        else
  89.  89        if (! bSure)
  90.  90          {
  91.  91            Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  92.  92                           PrePoint.x, PrePoint.y);
  93.  93 
  94.  94            Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  95.  95                           CurPoint.x, CurPoint.y);
  96.  96          }
  97.  97        else
  98.  98        if (OrgPoint.x!=CurPoint.x ||
  99.  99            OrgPoint.y!=CurPoint.y)
  100. 100          {
  101. 101            Rectangle(hDC, OrgPoint.x, OrgPoint.y,
  102. 102                      CurPoint.x, CurPoint.y);
  103. 103 
  104. 104            BackUpGraph(hDC, hMenu, OX, OY, CX, CY, 0, 0);
  105. 105 
  106. 106            BoundRect.left   =
  107. 107                      max(0, min(OrgPoint.x, CurPoint.x));
  108. 108            BoundRect.top    =
  109. 109                      max(0, min(OrgPoint.y, CurPoint.y));
  110. 110            BoundRect.right  =
  111. 111                      min(CX, max(OrgPoint.x, CurPoint.x));
  112. 112            BoundRect.bottom =
  113. 113                      min(CY, max(OrgPoint.y, CurPoint.y));
  114. 114            CopyRect(&OrgRect, &BoundRect);
  115. 115 
  116. 116            Width  = BoundRect.right - BoundRect.left;
  117. 117            Height = BoundRect.bottom - BoundRect.top;
  118. 118 
  119. 119            hBkDC     = CreateCompatibleDC(hDC);
  120. 120            hBkBitmap = CreateCompatibleBitmap(hDC,
  121. 121                                          Width, Height);
  122. 122 
  123. 123 
  124. 124            SelectObject(hBkDC, hBkBitmap);
  125. 125            BitBlt(hBkDC, 0, 0, Width, Height,
  126. 126                   hDC, BoundRect.left, BoundRect.top,
  127. 127                   SRCCOPY);
  128. 128            PatBlt(hMemDC,
  129. 129                   OX+BoundRect.left, OY+BoundRect.top,
  130. 130                   Width, Height, WHITENESS);
  131. 131 
  132. 132            Rectangle(hDC, BoundRect.left, BoundRect.top,
  133. 133                      BoundRect.right, BoundRect.bottom);
  134. 134            bBounded = TRUE;
  135. 135            bSelected = TRUE;
  136. 136            EnableMenuItem(hMenu, IDM_COPY, MF_ENABLED);
  137. 137            EnableMenuItem(hMenu, IDM_CUT, MF_ENABLED);
  138. 138            if (! CanUndo)
  139. 139              {
  140. 140                CanUndo = TRUE;
  141. 141                EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
  142. 142              }
  143. 143          }
  144. 144      }
  145. 145    else
  146. 146      {
  147. 147        if (! bSure)
  148. 148          {
  149. 149            BitBlt(hDC, BoundRect.left, BoundRect.top,
  150. 150                   Width, Height,
  151. 151                   hMemDC, OX+BoundRect.left, OY+BoundRect.top,
  152. 152                   SRCCOPY);
  153. 153 
  154. 154            OffX = CurPoint.x - PrePoint.x;
  155. 155            OffY = CurPoint.y - PrePoint.y;
  156. 156            OffsetRect(&BoundRect, OffX, OffY);
  157. 157 
  158. 158            BitBlt(hDC, BoundRect.left, BoundRect.top,
  159. 159                   Width, Height,
  160. 160                   hBkDC, 0, 0, SRCCOPY);
  161. 161            Rectangle(hDC, BoundRect.left, BoundRect.top,
  162. 162                      BoundRect.right, BoundRect.bottom);
  163. 163 
  164. 164            bMoving = TRUE;
  165. 165          }
  166. 166        else
  167. 167          bMoving = FALSE;
  168. 168      }
  169. 169 
  170. 170    SetROP2(hDC, nDrawMode);
  171. 171    DeleteObject(SelectObject(hDC, hPen));
  172. 172 }
  173. 173 
  174. 174 
  175. 175 void Copy(HWND hWnd)
  176. 176 {
  177. 177    HDC     hDC;
  178. 178    HDC     hClipDC;
  179. 179    HBITMAP hClipBitmap;
  180. 180 
  181. 181    if (! bBounded)
  182. 182      return ;
  183. 183 
  184. 184    hDC = GetDC(hWnd);
  185. 185    hClipDC = CreateCompatibleDC(hDC);
  186. 186    hClipBitmap = CreateCompatibleBitmap(hDC, Width, Height);
  187. 187    ReleaseDC(hWnd, hDC);
  188. 188 
  189. 189    SelectObject(hClipDC, hClipBitmap);
  190. 190    BitBlt(hClipDC, 0, 0, Width, Height,
  191. 191           hBkDC, 0, 0, SRCCOPY);
  192. 192 
  193. 193    OpenClipboard(hWnd);
  194. 194    EmptyClipboard();
  195. 195    SetClipboardData(CF_BITMAP, hClipBitmap);
  196. 196    CloseClipboard();
  197. 197 
  198. 198    DeleteDC(hClipDC);
  199. 199 }
  200. 200 
  201. 201 
  202. 202 
  203. 203 void Cut(HWND hWnd)
  204. 204 {
  205. 205    HDC   hDC;
  206. 206    HMENU hMenu;
  207. 207 
  208. 208    if (! bBounded)
  209. 209      return ;
  210. 210 
  211. 211    RecoverBlock();
  212. 212 
  213. 213    OpenClipboard(hWnd);
  214. 214    EmptyClipboard();
  215. 215    SetClipboardData(CF_BITMAP, hBkBitmap);
  216. 216    CloseClipboard();
  217. 217 
  218. 218    DeleteDC(hBkDC);
  219. 219 
  220. 220    hDC = GetDC(hWnd);
  221. 221    PatBlt(hDC, BoundRect.left, BoundRect.top,
  222. 222           Width, Height, WHITENESS);
  223. 223    ReleaseDC(hWnd, hDC);
  224. 224 
  225. 225    hMenu = GetMenu(hWnd);
  226. 226    EnableMenuItem(hMenu, IDM_COPY, MF_GRAYED);
  227. 227    EnableMenuItem(hMenu, IDM_CUT, MF_GRAYED);
  228. 228    bBounded = FALSE;
  229. 229 
  230. 230    if (! CanUndo)
  231. 231      {
  232. 232         CanUndo = TRUE;
  233. 233         EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
  234. 234      }
  235. 235 }
  236. 236 
  237. 237 
  238. 238 void Paste(HWND hWnd)
  239. 239 {
  240. 240    HDC     hDC, hClipDC;
  241. 241    HBITMAP hClipBitmap;
  242. 242    HMENU   hMenu;
  243. 243    BITMAP  bm;
  244. 244    HPEN    hPen;
  245. 245    int     nDrawMode;
  246. 246 
  247. 247    OpenClipboard(hWnd);
  248. 248 
  249. 249    hDC   = GetDC(hWnd);
  250. 250    hMenu = GetMenu(hWnd);
  251. 251    SendMessage(hWnd, WM_COMMAND, IDM_BLOCK, (LONG) 0);
  252. 252 
  253. 253    if (hClipBitmap=GetClipboardData(CF_BITMAP))
  254. 254      {
  255. 255        GetObject(hClipBitmap, sizeof(BITMAP), (LPSTR) &bm);
  256. 256 
  257. 257        BackUpGraph(hDC, hMenu, OX, OY, CX, CY, 0, 0);
  258. 258 
  259. 259        BoundRect.left   = 0;
  260. 260        BoundRect.top    = 0;
  261. 261        BoundRect.right  = bm.bmWidth;
  262. 262        BoundRect.bottom = bm.bmHeight;
  263. 263        CopyRect(&OrgRect, &BoundRect);
  264. 264 
  265. 265        Width  = BoundRect.right;
  266. 266        Height = BoundRect.bottom;
  267. 267 
  268. 268        hClipDC   = CreateCompatibleDC(hDC);
  269. 269        SelectObject(hClipDC, hClipBitmap);
  270. 270 
  271. 271        hBkDC     = CreateCompatibleDC(hDC);
  272. 272        hBkBitmap = CreateCompatibleBitmap(hDC,
  273. 273                                          Width, Height);
  274. 274        SelectObject(hBkDC, hBkBitmap);
  275. 275 
  276. 276        BitBlt(hBkDC, 0, 0, Width, Height,
  277. 277               hClipDC, 0, 0, SRCCOPY);
  278. 278        DeleteDC(hClipDC);
  279. 279 
  280. 280        BitBlt(hDC, 0, 0, Width, Height,
  281. 281               hBkDC, 0, 0, SRCCOPY);
  282. 282 
  283. 283        hPen = CreatePen(PS_DOT, 1, 0);
  284. 284        hPen = SelectObject(hDC, hPen);
  285. 285        nDrawMode = SetROP2(hDC, R2_NOTXORPEN);
  286. 286 
  287. 287        Rectangle(hDC, 0, 0,
  288. 288                       Width, Height);
  289. 289 
  290. 290        SetROP2(hDC, nDrawMode);
  291. 291        DeleteObject(SelectObject(hDC, hPen));
  292. 292 
  293. 293        bBounded = TRUE;
  294. 294        EnableMenuItem(hMenu, IDM_COPY, MF_ENABLED);
  295. 295        EnableMenuItem(hMenu, IDM_CUT, MF_ENABLED);
  296. 296        if (! CanUndo)
  297. 297          {
  298. 298            CanUndo = TRUE;
  299. 299            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
  300. 300          }
  301. 301      }
  302. 302 
  303. 303    ReleaseDC(hWnd, hDC);
  304. 304    CloseClipboard();
  305. 305 }