1. drawLine( ) fails?
- Posted by lpuster Aug 26, 2010
- 1173 views
I can not get drawLine() or drawLines() to put anything into the window. I am using the latest version of win32Lib. What am I doing wrong?
Louis.
include win32lib.ew
constant Main = create(Window, "No line?", 0, 0, 0, 200, 200, 0)
setPenColor(Main, Black)
drawLine(Main, 10, 10, 190, 190)
WinMain(Main, Normal)
2. Re: drawLine( ) fails?
- Posted by euphoric (admin) Aug 26, 2010
- 1234 views
I can not get drawLine() or drawLines() to put anything into the window. I am using the latest version of win32Lib. What am I doing wrong?
Here's the source of win32lib/demo/drawRecTest.exw. It will give you an idea of what you need to be doing.
include win32lib.ew without warning createForm("Test drawRectangle, size=(150,150)") global procedure Paint_TestdrawRectangle(integer self, integer event, sequence params) -- Draw the background setPenColor(self, Yellow) drawRectangle(self, w32True, 10, 10, 100, 100) -- Draw the outside edge setPenColor(self, BrightRed) setPenWidth(self,2) drawRectangle(self, w32False, 10, 10, 100, 100) -- Draw the inner square setPenColor(self, BrightBlue) setPenWidth(self,3) drawRectangle(self, w32False, 24, 24, 86, 86) end procedure registerRoutine("Paint_TestdrawRectangle", routine_id("Paint_TestdrawRectangle")) include w32Start.ew
3. Re: drawLine( ) fails?
- Posted by DerekParnell (admin) Aug 26, 2010
- 1137 views
I can not get drawLine() or drawLines() to put anything into the window. I am using the latest version of win32Lib. What am I doing wrong?
Louis.
include win32lib.ew constant Main = create(Window, "No line?", 0, 0, 0, 200, 200, 0) setPenColor(Main, Black) drawLine(Main, 10, 10, 190, 190) WinMain(Main, Normal)
The problem is that Main window doesn't kinda exist until the WinMain() is called. Is does exist but it isn't usable until WinMain() starts. Here is some alternate code ...
include win32lib.ew constant Main = create(Window, "No line?", 0, 0, 0, 200, 200, 0) procedure onActivate_Main(integer self, integer event, sequence parms) -- This gets run whenever the Main window is activated. setPenColor(self, Black) drawLine(self, 10, 10, 190, 190) end procedure setHandler(Main, w32HActivate, routine_id("onActivate_Main")) WinMain(Main, Normal)
Programming for a GUI like Windowstm is a lot different from writing a console application. For GUI programs, you need to write a lot of small routines that handle specific events that might happen. The order that the events happen in is determined by what the user does.
In the code above, there is a small routine that is run everytime that the Main window s activated. The w32HActivate is one of the many possible types of events that can occur in a GUI system. The call to setHandler() sets up a connection between a GUI control, an event, and a routine to process (handle) the event for that control.
In the sample code above, the procedure onActivate_Main() is linked to the w32HActivate event for the Main control. One of the first things that WinMain() does, after making the main window visible, is to trigger an activate event for the main window and each of its child controls. Thus the line gets drawn.
Note however, this might not be what you actually want as the line will get wiped out again whenever that portion of the main window is covered by anything else. You might need to draw the line during the w32HPaint event, as that event happens everytime any portion of the window get uncovered (say during resizing or moving a foreground window off the main window).
4. Re: drawLine( ) fails?
- Posted by CoJaBo Aug 27, 2010
- 1118 views
The example code in the last post is incorrect- you shouldn't be doing any screen drawing operations outside of the OnPaint event handler.
A significant number of Win32Lib applications get this wrong, leading to often subtle screen corruption and portability issues.
The corrected coed is as follows:
include win32lib.ew constant Main = create(Window, "Line", 0, 0, 0, 200, 200, 0) procedure onPaint_Main(integer self, integer event, sequence parms) -- This gets run whenever the Main window needs redrawn. setPenColor(self, Black) drawLine(self, 10, 10, 190, 190) end procedure setHandler(Main, w32HPaint, routine_id("onPaint_Main")) WinMain(Main, Normal)
5. Re: drawLine( ) fails?
- Posted by DerekParnell (admin) Aug 27, 2010
- 1121 views
The example code in the last post is incorrect ...
Well ... "incorrect" might be a bit harsh. It is certainly not the normal way to do things but there is nothing inherently wrong with drawing on the screen during the Activate event - if that's what the application designer really wants to do. However 'activate' only happens once (usually) per control and any drawing done then may get wiped out whenever the window gets covered by another window or is made smaller by the user. But hey, if the designer knows about that and it doesn't matter to them, then sure ... use the activate event to draw the stuff.
But if you need the drawing to always be visible, no matter if the window changed size or gets covered by another, then the Paint event is a better one for you to use. In fact, with the paint event you get the exact coordinates of the area that needs to be redrawn, so one can determine the minimum amount of work to be done.
If the drawing on the window is a sort-of static background, it would actually be better to draw it on a Pixmap once and then during the paint event, just copy the corresponding subsection from the Pixmap to the window - a very fast operation.
6. Re: drawLine( ) fails?
- Posted by CoJaBo Aug 27, 2010
- 1147 views
Well ... "incorrect" might be a bit harsh.
Sorry if it came off ass offensive, but it is wrong and should not be recommended under any circumstances. Theres enough faulty Windows applications as it is, we don't any need more. :P
The Activate event occurs after a Window is opened. The window may not be visible at this point. Additionally, drawing in OnActivate implies duplicating code, since it must be done in OnRepaint as well. This also means that a window would be drawn twice when first opened. So yes, it is incorrect to draw during the OnActivate event, as it makes no sense to draw a control that is not visible, duplicate code, or do the same drawing operation twice in a row.
As said, Pixmaps, of course, give more flexibility, as the only operation needed during repaint is to blit the pixmap to the window. Drawing to the pixmap could be done anywhere, tho things are usually cleaner if all graphics code is kept together. Especially if the drawing is complex (taking many API calls to produce), this is indeed a better way as it will eliminate flicker (it comes at the cost of a slight increase in memory usage, but this has long since been made irrelevant).
7. Re: drawLine( ) fails?
- Posted by jaygade Aug 27, 2010
- 1211 views
And this is why I don't write GUI programs. There's just so much "tribal knowledge" etc. It sucks and there's nothing at all logical to it.
8. Re: drawLine( ) fails?
- Posted by petelomax Aug 27, 2010
- 1063 views
And this is why I don't write GUI programs. There's just so much "tribal knowledge" etc. It sucks and there's nothing at all logical to it.
Try it for a week in C, which makes it "easy". I guarantee you'll know the true meaning of easy when you get back.