📄 demo4_5.c
字号:
260 }
261 }
262
263
264
265 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
266 {
267 HBRUSH hBrush;
268
269 if (nHatchStyle == -1)
270 hBrush = CreateSolidBrush(crColor);
271 else
272 hBrush = CreateHatchBrush(nHatchStyle, crColor);
273
274 return (hBrush);
275 }
276
277
278
279 void DrawGraph(HDC hDC, BOOL bSure)
280 {
281 HPEN hPen, hPrePen;
282 HBRUSH hBrush, hPreBrush;
283
284 if (ToolID==IDM_PENCIL || bSure)
285 {
286 hPen = CreatePen(nPenStyle, nPenWidth,
287 MKCOLOR(crPCurColor[nPenColor]));
288 hPrePen = SelectObject(hDC, hPen);
289
290 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
291 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
292 {
293 hBrush = MyCreateBrush(nHatch,
294 MKCOLOR(crBCurColor[nBrushColor]));
295 hPreBrush = SelectObject(hDC, hBrush);
296 }
297 else
298 {
299 hBrush = GetStockObject(NULL_BRUSH);
300 hPreBrush = SelectObject(hDC, hBrush);
301 }
302 }
303 else
304 SelectObject(hDC, GetStockObject(NULL_BRUSH));
305
306 switch (ToolID)
307 {
308 case IDM_PENCIL :
309 DrawPencil(hDC);
310 break;
311
312 case IDM_LINE :
313 DrawLine(hDC, bSure);
314 break;
315
316 case IDM_RECT_F :
317 case IDM_RECT :
318 DrawRect(hDC, bSure);
319 break;
320
321 case IDM_ELLIP_F :
322 case IDM_ELLIP :
323 DrawEllip(hDC, bSure);
324 break;
325
326 case IDM_CIRCLE_F :
327 case IDM_CIRCLE :
328 DrawCircle(hDC, bSure);
329 break;
330
331 case IDM_ROUNDRECT_F :
332 case IDM_ROUNDRECT :
333 DrawRoundRect(hDC, bSure);
334 break;
335 }
336
337 if (ToolID==IDM_PENCIL || bSure)
338 {
339 SelectObject(hDC, hPrePen);
340 DeleteObject(hPen);
341
342 if (ToolID==IDM_RECT_F || ToolID==IDM_ELLIP_F ||
343 ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
344 {
345 SelectObject(hDC, hPreBrush);
346 DeleteObject(hBrush);
347 }
348 else
349 {
350 SelectObject(hDC, hPreBrush);
351 }
352 }
353 }
354
355
356
357 void DrawPencil(HDC hDC)
358 {
359 MoveTo(hDC, PrePoint.x, PrePoint.y);
360 LineTo(hDC, CurPoint.x, CurPoint.y);
361 }
362
363
364
365 void DrawLine(HDC hDC, BOOL bSure)
366 {
367 int nDrawMode;
368
369 if (! bSure)
370 {
371 nDrawMode = SetROP2(hDC, R2_NOT);
372
373 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
374 LineTo(hDC, PrePoint.x, PrePoint.y);
375
376 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
377 LineTo(hDC, CurPoint.x, CurPoint.y);
378
379 SetROP2(hDC, nDrawMode);
380 }
381 else
382 {
383 MoveTo(hDC, OrgPoint.x, OrgPoint.y);
384 LineTo(hDC, CurPoint.x, CurPoint.y);
385 }
386 }
387
388
389
390 void DrawRect(HDC hDC, BOOL bSure)
391 {
392 int nDrawMode;
393
394 if (! bSure)
395 {
396 nDrawMode = SetROP2(hDC, R2_NOT);
397
398 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
399 PrePoint.x, PrePoint.y);
400
401 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
402 CurPoint.x, CurPoint.y);
403
404 SetROP2(hDC, nDrawMode);
405 }
406 else
407 {
408 Rectangle(hDC, OrgPoint.x, OrgPoint.y,
409 CurPoint.x, CurPoint.y);
410 }
411 }
412
413
414
415 void DrawEllip(HDC hDC, BOOL bSure)
416 {
417 int nDrawMode;
418
419 if (! bSure)
420 {
421 nDrawMode = SetROP2(hDC, R2_NOT);
422
423 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
424 PrePoint.x, PrePoint.y);
425
426 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
427 CurPoint.x, CurPoint.y);
428
429 SetROP2(hDC, nDrawMode);
430 }
431 else
432 {
433 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
434 CurPoint.x, CurPoint.y);
435 }
436 }
437
438
439
440 void DrawRoundRect(HDC hDC, BOOL bSure)
441 {
442 int nDrawMode;
443
444 if (! bSure)
445 {
446 nDrawMode = SetROP2(hDC, R2_NOT);
447
448 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
449 PrePoint.x, PrePoint.y,
450 (PrePoint.x-OrgPoint.x)/4,
451 (PrePoint.y-OrgPoint.y)/4);
452
453 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
454 CurPoint.x, CurPoint.y,
455 (CurPoint.x-OrgPoint.x)/4,
456 (CurPoint.y-OrgPoint.y)/4);
457
458 SetROP2(hDC, nDrawMode);
459 }
460 else
461 {
462 RoundRect(hDC, OrgPoint.x, OrgPoint.y,
463 CurPoint.x, CurPoint.y,
464 (CurPoint.x-OrgPoint.x)/4,
465 (CurPoint.y-OrgPoint.y)/4);
466 }
467 }
468
469
470
471 void DrawCircle(HDC hDC, BOOL bSure)
472 {
473 int nDrawMode;
474 int nLogPixSx, nLogPixSy;
475 int Width, Height;
476 int SignX, SignY;
477
478 nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
479 nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
480
481 Width = CurPoint.x - OrgPoint.x;
482 Height = CurPoint.y - OrgPoint.y;
483 SignX = (Width >= 0 ? 1 : -1);
484 SignY = (Height >= 0 ? 1 : -1);
485
486 if (fabs((float) Width/nLogPixSx) >
487 fabs((float) Height/nLogPixSy) )
488 {
489 CurPoint.x = OrgPoint.x + (float)
490 fabs(Height) * nLogPixSx / nLogPixSy * SignX;
491 }
492 else
493 {
494 CurPoint.y = OrgPoint.y + (float)
495 fabs(Width) * nLogPixSy / nLogPixSx * SignY;
496 }
497
498
499 if (! bSure)
500 {
501 nDrawMode = SetROP2(hDC, R2_NOT);
502
503 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
504 PrePoint.x, PrePoint.y);
505
506 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
507 CurPoint.x, CurPoint.y);
508
509 SetROP2(hDC, nDrawMode);
510 }
511 else
512 {
513 Ellipse(hDC, OrgPoint.x, OrgPoint.y,
514 CurPoint.x, CurPoint.y);
515 }
516 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -