⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paint_bk.c

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -