Forum > GTK
[SOLVED] How to Draw a TCanvas on a GTK2 Window
(1/1)
aydın:
Hi everyone!
For one of my projects, I'm not using the TForm in Lazarus LCL. Instead, I'm writing my own widgetset using pure API.
On Windows, I capture and draw the painting (WM_PAINT) event as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Procedure Form_Paint(hwnd: THandle);Var Context: HDC; BufCanvas: TCanvas;Begin Context := GetDC(hwnd); BufCanvas := TCanvas.Create; BufCanvas.Handle := Context; {I make my drawings} //FIA(BufCanvas); BufCanvas.Free; ReleaseDC(hwnd, Context);End;
To achieve the same in GTK2, you need to set up a drawing trigger as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---gtk_signal_connect(GTK_OBJECT(window), 'expose-event', G_CALLBACK(@Form_Paint), nil);
My question is, how can I draw the TCanvas I created on the GTK2 window?
Thank you in advance.
aydın:
Hello again.
I've examined Lcl a bit, and although I don't fully understand its logic, I managed to create a working system.
After creating my window, I set up the trigger as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---gtk_signal_connect(GTK_OBJECT(PGtkWidget(Result)), 'expose-event', G_CALLBACK(@onDrawEvent),
And here is my drawing code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function onDrawEvent(widget: PGtkWidget; event: PGdkEvent; user_data: gpointer): gboolean; cdecl;var Bmp: TCanvas; Context: HDC;begin Context:= InterfaceBase.WidgetSet.GetDC(THandle(widget)); Bmp:= TCanvas.Create; Bmp.Handle:= Context; // Sample drawings Bmp.Pen.Color:= clBlue; Bmp.Pen.Width:= 2; Bmp.Line(10, 10, 100, 100); // FIA(Bmp); Bmp.Free; InterfaceBase.WidgetSet.ReleaseDC(THandle(widget), Context); Result:= True;end;
In fact, it's almost identical; I hadn't thought of converting PGtkWidget to THandle.
I hope it's useful to someone.
Navigation
[0] Message Index