1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
{$APPTYPE GUI}
{$MODE DELPHI}
uses
Windows, Cairo, CairoWin32;
{$R wincairo.res}
const
CLASSNAME = 'FENETRE_DESSIN';
var
gHandle: THandle;
procedure CairoDraw(aDC: HDC; aClientWidth, aClientHeight: integer);
var
cr: pcairo_t;
surface: pcairo_surface_t;
begin
surface := cairo_win32_surface_create_with_dib(CAIRO_FORMAT_ARGB32, aClientWidth, aClientHeight);
cr := cairo_create(surface);
cairo_scale(cr, aClientWidth, aClientHeight);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_set_line_width(cr, 0.1);
cairo_move_to(cr, 0.2, 0.2);
cairo_line_to(cr, 0.8, 0.8);
cairo_stroke(cr);
cairo_destroy(cr);
BitBlt(
aDC,
0,
0,
aClientWidth,
aClientHeight,
cairo_win32_surface_get_dc(surface),
0,
0,
SRCCOPY
);
cairo_surface_destroy(surface);
end;
function WinProc(aHandle: THandle; aMsg: UINT; aWP: WPARAM; aLP: LPARAM): LRESULT; stdcall;
var
vPS: TPaintStruct;
vDC: HDC;
vRECT: RECT;
begin
case aMsg of
WM_SIZE:
begin
{
InvalidateRect(aHandle, nil, TRUE);
UpdateWindow(aHandle);
}
end;
WM_KEYDOWN:
if aWP = VK_ESCAPE then
begin
PostQuitMessage(0);
result := 0;
exit;
end;
WM_DESTROY:
begin
PostQuitMessage(0);
result := 0;
exit;
end;
WM_PAINT:
begin
vDC := BeginPaint(gHandle, vPS);
GetClientRect(aHandle, @vRECT);
CairoDraw(vDC, vRECT.right - vRECT.left, vRECT.bottom - vRECT.top);
EndPaint(gHandle, vPS);
result := 0;
exit;
end;
end;
result := DefWindowProc(aHandle, aMsg, aWP, aLP);
end;
procedure TextMode;
begin
DestroyWindow(gHandle);
end;
procedure DoEvents;
var
vMsg: TMsg;
begin
while PeekMessage(vMsg, 0, 0, 0, PM_REMOVE) do
begin
TranslateMessage(vMsg);
DispatchMessage(vMsg);
if vMsg.Message = WM_QUIT then
begin
TextMode;
Halt;
end;
end;
end;
procedure GraphMode(aClientWidth, aClientHeight: integer);
var
vClass: TWndClass;
vBorder, vCaption, vWidth, vHeight, vLeft, vTop: integer;
begin
vClass.style := CS_HREDRAW or CS_VREDRAW;;
vClass.lpfnWndProc := @WinProc;
vClass.cbClsExtra := 0;
vClass.cbWndExtra := 0;
vClass.hInstance := system.hInstance;
vClass.hIcon := LoadImage(hInstance, MAKEINTRESOURCE(1), IMAGE_ICON, 0, 0, 0);
vClass.hCursor := LoadCursor(0, IDC_ARROW);
vClass.hbrBackground := GetStockObject(WHITE_BRUSH);;
vClass.lpszMenuName := nil;
vClass.lpszClassName := CLASSNAME;
if RegisterClass(vClass) = 0 then
Halt;
vBorder := 2 * (GetSystemMetrics(SM_CXDLGFRAME) + GetSystemMetrics(SM_CXBORDER));
vCaption := GetSystemMetrics(SM_CYCAPTION);
vWidth := aClientWidth + 2 * vBorder;
vHeight := aClientHeight + 2 * vBorder + vCaption;
vLeft := (GetSystemMetrics(SM_CXFULLSCREEN) - vWidth) div 2;
vTop := (GetSystemMetrics(SM_CYFULLSCREEN) - vHeight) div 2;
gHandle := CreateWindow(
CLASSNAME,
'Dessin avec Cairo',
WS_OVERLAPPEDWINDOW or WS_VISIBLE,
vLeft,
vTop,
vWidth,
vHeight,
0,
0,
hInstance,
nil
);
if gHandle = 0 then
Halt;
end;
begin
GraphMode(400, 400);
while TRUE do
DoEvents;
end. |
Partager