User talk:BrianO: Difference between revisions
(Introduction and suggestions) |
No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<lang purebasic>;There are no hard feelings. |
|||
I've noticed that you have been submitting some PureBasic solutions to tasks. That's awesome! |
|||
;Thank you for your code sample, it seems like it will be useful |
|||
I have some suggestions with regard to coding that may be useful, based upon the code you've already posted for some of the tasks. The first is that if the tasks requires text output that a console window (or even window + gadgets) be used in preference to using the debugger output (via the Debug command). This is fairly straight forward and keeps the code size small. It also means that the program will produce the same output whether or not it is being debugged or simply 'ran'. |
|||
;I modified your sample and provide you with the updated code if it might benefit you. |
|||
;The changes address a few simple things that I found helpful: |
|||
The other suggestion is to help the reader of your code whenever possible by doing some simple things such as indenting your code. Rosetta Code has provided a great way to introduce many people to PureBasic. Each of the solutions gives them a glimpse at how PureBasic solves some common problems. It's great to have another person (or two) making contributions here. ;) --[[User:Demivec|Demivec]] 16:17, 18 August 2012 (UTC) |
|||
; It allows negative selections, i.e. making a click and then moving left or up from the click to outline the selection. |
|||
; It removes the toggling of buttons when they served no apparent purpose (namely the 'new' button but also resetting the 'snapit' button). |
|||
; Uses the CanvasGadget for it's more robust event handling (instead of an image gadget). |
|||
; Addresses one small issue when the 'snapit' button is displayed. If a click was made on the image a new selection was started. |
|||
; That is now prevented by requiring the 'snapit' button to be toggled off so that the desktop image is displayed to allow captures to take place. |
|||
; |
|||
;All other changes are simply formatting (no offense intended) and many occur simply by virtue of the IDE (jaPBe) that I use. |
|||
Enumeration |
|||
#ButtonMakeScreenShot |
|||
#ButtonMakeNewScreenShot |
|||
#CanvasScreenShot |
|||
#ImageFullScreenShot = 0 |
|||
#ImageHalfScreenShot |
|||
#ImageTempScreenShot |
|||
#ImageSelectedArea |
|||
EndEnumeration |
|||
Procedure.i MakeDesktopScreenshot(ImageNr, x, y, width, height) |
|||
hImage = CreateImage(ImageNr, width, height) |
|||
hDC = StartDrawing(ImageOutput(ImageNr)) |
|||
DeskDC = GetDC_(GetDesktopWindow_()) |
|||
BitBlt_(hDC, 0, 0, width, height, DeskDC, x, y, #SRCCOPY) |
|||
StopDrawing() |
|||
ReleaseDC_(GetDesktopWindow_(), DeskDC) |
|||
ProcedureReturn hImage |
|||
EndProcedure |
|||
Procedure.s formatTitle(x, y, zoom) |
|||
ProcedureReturn Str(x * zoom) + "|" + Str(y * 2) + "|" |
|||
EndProcedure |
|||
Procedure filterCallback(x, y, sourceColor, targetColor) |
|||
If (x % 16 < 8 And y % 16 < 8) Or (x % 16 > 7 And y % 16 > 7) |
|||
ProcedureReturn RGBA(192, 192, 192, 255) |
|||
Else |
|||
ProcedureReturn RGBA(255, 255, 255, 255) |
|||
EndIf |
|||
EndProcedure |
|||
Style = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered |
|||
LoadFont(0, "tahoma", 9, #PB_Font_HighQuality | #PB_Font_Bold) |
|||
SetGadgetFont(#PB_Default, FontID(0)) |
|||
ExamineDesktops(): dtw = DesktopWidth(0): dth = DesktopHeight(0) |
|||
w = dtw / 2: h = dth / 2 |
|||
MakeDesktopScreenshot(#ImageFullScreenShot, 0, 0, dtw, dth) |
|||
CopyImage(#ImageFullScreenShot, #ImageHalfScreenShot) |
|||
ResizeImage(#ImageHalfScreenShot, w, h) |
|||
OpenWindow(0, 0, 0, w, h + 20, "", Style): StickyWindow(0, 1) |
|||
ButtonGadget(#ButtonMakeNewScreenShot, w - 200, h, 100, 20, "New") |
|||
ButtonGadget(#ButtonMakeScreenShot, w - 100, h, 100, 20,"SnipIt", #PB_Button_Toggle) |
|||
CanvasGadget(#CanvasScreenShot, 0, 0, w, h) |
|||
SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot)) |
|||
StopSelecting=#True |
|||
Repeat |
|||
msg = WaitWindowEvent() |
|||
gid = EventGadget() |
|||
etp = EventType() |
|||
Select msg |
|||
Case #PB_Event_CloseWindow |
|||
End |
|||
Case #PB_Event_Gadget |
|||
Select gid |
|||
Case #ButtonMakeNewScreenShot |
|||
HideWindow(0, 1) |
|||
sleep_(200) |
|||
MakeDesktopScreenshot(#ImageFullScreenShot, 0, 0, dtw, dth) |
|||
CopyImage(#ImageFullScreenShot, #ImageHalfScreenShot) |
|||
ResizeImage(#ImageHalfScreenShot, w, h) |
|||
SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot)) |
|||
HideWindow(0, 0) |
|||
SetGadgetState(#ButtonMakeScreenShot, 0) |
|||
Case #ButtonMakeScreenShot |
|||
Select GetGadgetState(#ButtonMakeScreenShot) |
|||
Case 0 |
|||
SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot)) |
|||
Case 1 |
|||
SetWindowTitle(0, formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom) + " - " + formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom)) |
|||
GrabImage(#ImageFullScreenShot, #ImageSelectedArea, x1 * 2 - 2, y1 * 2 - 2, sw * 2, sh * 2) |
|||
SetClipboardImage(#ImageSelectedArea) |
|||
CreateImage(#ImageTempScreenShot, w, h) |
|||
StartDrawing(ImageOutput(#ImageTempScreenShot)) |
|||
DrawingMode(#PB_2DDrawing_CustomFilter) |
|||
CustomFilterCallback(@filterCallback()) |
|||
Box(0, 0, w, h, RGB(212, 208, 200)) |
|||
DrawingMode(#PB_2DDrawing_Default) |
|||
DrawImage(ImageID(#ImageSelectedArea), 0, 0) |
|||
StopDrawing() |
|||
SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageTempScreenShot)) |
|||
EndSelect |
|||
Case #CanvasScreenShot |
|||
Select etp |
|||
Case #PB_EventType_LeftButtonUp |
|||
StopSelecting = #True |
|||
Case #PB_EventType_MouseMove |
|||
If Not StopSelecting |
|||
xi = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseX) |
|||
yi = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseY) |
|||
If xi < 0: xi = 0: EndIf |
|||
If xi > w: xi = w: EndIf |
|||
If yi < 0: yi = 0: EndIf |
|||
If yi > h: yi = h: EndIf |
|||
x1 = xi: x2 = x0 |
|||
If x2 < x1 |
|||
Swap x2, x1 |
|||
EndIf |
|||
y1 = yi: y2 = y0 |
|||
If y2 < y1 |
|||
Swap y2, y1 |
|||
EndIf |
|||
sw = x2 - x1 |
|||
sh = y2 - y1 |
|||
SetWindowTitle(0, formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom)) |
|||
SetGadgetState(#CanvasScreenShot, ImageID(#ImageHalfScreenShot)) |
|||
CopyImage(#ImageHalfScreenShot, #ImageTempScreenShot) |
|||
StartDrawing(ImageOutput(#ImageTempScreenShot)) |
|||
DrawingMode(#PB_2DDrawing_XOr | #PB_2DDrawing_Outlined) |
|||
Box(x1, y1, sw, sh, $FFFFFF) |
|||
StopDrawing() |
|||
SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageTempScreenShot)) |
|||
EndIf |
|||
Case #PB_EventType_LeftButtonDown |
|||
If StopSelecting = #True And GetGadgetState(#ButtonMakeScreenShot) = 0 |
|||
x0 = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseX) |
|||
y0 = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseY) |
|||
SetWindowTitle(0, formatTitle(x0, y0, 2)) |
|||
StopSelecting = #False |
|||
EndIf |
|||
EndSelect |
|||
EndSelect |
|||
EndSelect |
|||
ForEver</lang> |
|||
--[[User:Demivec|Demivec]] 21:44, 27 August 2012 (UTC) |
Latest revision as of 21:44, 27 August 2012
<lang purebasic>;There are no hard feelings.
- Thank you for your code sample, it seems like it will be useful
- I modified your sample and provide you with the updated code if it might benefit you.
- The changes address a few simple things that I found helpful
- It allows negative selections, i.e. making a click and then moving left or up from the click to outline the selection.
- It removes the toggling of buttons when they served no apparent purpose (namely the 'new' button but also resetting the 'snapit' button).
- Uses the CanvasGadget for it's more robust event handling (instead of an image gadget).
- Addresses one small issue when the 'snapit' button is displayed. If a click was made on the image a new selection was started.
- That is now prevented by requiring the 'snapit' button to be toggled off so that the desktop image is displayed to allow captures to take place.
- All other changes are simply formatting (no offense intended) and many occur simply by virtue of the IDE (jaPBe) that I use.
Enumeration
#ButtonMakeScreenShot #ButtonMakeNewScreenShot #CanvasScreenShot #ImageFullScreenShot = 0 #ImageHalfScreenShot #ImageTempScreenShot #ImageSelectedArea
EndEnumeration
Procedure.i MakeDesktopScreenshot(ImageNr, x, y, width, height)
hImage = CreateImage(ImageNr, width, height) hDC = StartDrawing(ImageOutput(ImageNr)) DeskDC = GetDC_(GetDesktopWindow_()) BitBlt_(hDC, 0, 0, width, height, DeskDC, x, y, #SRCCOPY) StopDrawing() ReleaseDC_(GetDesktopWindow_(), DeskDC) ProcedureReturn hImage
EndProcedure
Procedure.s formatTitle(x, y, zoom)
ProcedureReturn Str(x * zoom) + "|" + Str(y * 2) + "|"
EndProcedure
Procedure filterCallback(x, y, sourceColor, targetColor)
If (x % 16 < 8 And y % 16 < 8) Or (x % 16 > 7 And y % 16 > 7) ProcedureReturn RGBA(192, 192, 192, 255) Else ProcedureReturn RGBA(255, 255, 255, 255) EndIf
EndProcedure
Style = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
LoadFont(0, "tahoma", 9, #PB_Font_HighQuality | #PB_Font_Bold) SetGadgetFont(#PB_Default, FontID(0))
ExamineDesktops(): dtw = DesktopWidth(0): dth = DesktopHeight(0) w = dtw / 2: h = dth / 2
MakeDesktopScreenshot(#ImageFullScreenShot, 0, 0, dtw, dth) CopyImage(#ImageFullScreenShot, #ImageHalfScreenShot) ResizeImage(#ImageHalfScreenShot, w, h)
OpenWindow(0, 0, 0, w, h + 20, "", Style): StickyWindow(0, 1) ButtonGadget(#ButtonMakeNewScreenShot, w - 200, h, 100, 20, "New") ButtonGadget(#ButtonMakeScreenShot, w - 100, h, 100, 20,"SnipIt", #PB_Button_Toggle) CanvasGadget(#CanvasScreenShot, 0, 0, w, h) SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot))
StopSelecting=#True Repeat
msg = WaitWindowEvent() gid = EventGadget() etp = EventType() Select msg Case #PB_Event_CloseWindow End Case #PB_Event_Gadget Select gid Case #ButtonMakeNewScreenShot HideWindow(0, 1) sleep_(200) MakeDesktopScreenshot(#ImageFullScreenShot, 0, 0, dtw, dth) CopyImage(#ImageFullScreenShot, #ImageHalfScreenShot) ResizeImage(#ImageHalfScreenShot, w, h) SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot)) HideWindow(0, 0) SetGadgetState(#ButtonMakeScreenShot, 0) Case #ButtonMakeScreenShot Select GetGadgetState(#ButtonMakeScreenShot) Case 0 SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageHalfScreenShot)) Case 1 SetWindowTitle(0, formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom) + " - " + formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom)) GrabImage(#ImageFullScreenShot, #ImageSelectedArea, x1 * 2 - 2, y1 * 2 - 2, sw * 2, sh * 2) SetClipboardImage(#ImageSelectedArea) CreateImage(#ImageTempScreenShot, w, h) StartDrawing(ImageOutput(#ImageTempScreenShot)) DrawingMode(#PB_2DDrawing_CustomFilter) CustomFilterCallback(@filterCallback()) Box(0, 0, w, h, RGB(212, 208, 200)) DrawingMode(#PB_2DDrawing_Default) DrawImage(ImageID(#ImageSelectedArea), 0, 0) StopDrawing() SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageTempScreenShot)) EndSelect Case #CanvasScreenShot Select etp Case #PB_EventType_LeftButtonUp StopSelecting = #True Case #PB_EventType_MouseMove If Not StopSelecting xi = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseX) yi = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseY) If xi < 0: xi = 0: EndIf If xi > w: xi = w: EndIf If yi < 0: yi = 0: EndIf If yi > h: yi = h: EndIf x1 = xi: x2 = x0 If x2 < x1 Swap x2, x1 EndIf y1 = yi: y2 = y0 If y2 < y1 Swap y2, y1 EndIf sw = x2 - x1 sh = y2 - y1 SetWindowTitle(0, formatTitle(x1, y1, 2) + formatTitle(sw, sh, zoom)) SetGadgetState(#CanvasScreenShot, ImageID(#ImageHalfScreenShot)) CopyImage(#ImageHalfScreenShot, #ImageTempScreenShot) StartDrawing(ImageOutput(#ImageTempScreenShot)) DrawingMode(#PB_2DDrawing_XOr | #PB_2DDrawing_Outlined) Box(x1, y1, sw, sh, $FFFFFF) StopDrawing() SetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_Image, ImageID(#ImageTempScreenShot)) EndIf Case #PB_EventType_LeftButtonDown If StopSelecting = #True And GetGadgetState(#ButtonMakeScreenShot) = 0 x0 = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseX) y0 = GetGadgetAttribute(#CanvasScreenShot, #PB_Canvas_MouseY) SetWindowTitle(0, formatTitle(x0, y0, 2)) StopSelecting = #False EndIf EndSelect EndSelect EndSelect
ForEver</lang> --Demivec 21:44, 27 August 2012 (UTC)