User talk:BrianO: Difference between revisions
No edit summary |
No edit summary |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<lang purebasic>;There are no hard feelings. |
|||
Hi, it's good to see another solver for the PureBasic task list. I've noticed you've posted solutions to some of the tasks recently for PureBasic and wanted to bring something to your attention. In at least some of your code samples you have accessed Windows API. Since PureBasic is cross-platform these samples won't run on the non-Windows OS's that PureBasic was written for. You may want to limit the use of API so that someone testing the code won't be left wondering why it won't compile or alternatively include an explanation that the code will only run on Windows. --[[User:Demivec|Demivec]] 00:46, 14 July 2012 (UTC) |
|||
;Thank you for your code sample, it seems like it will be useful |
|||
Thanks for the advice. The fact that the procedure uses the Windows API should be obvious. This forum is about solving tasks using your language of choice, not about promoting a particular proprietary programming languages features. |
|||
;I modified your sample and provide you with the updated code if it might benefit you. |
|||
--[[User:brianO|brianO] 11:24 PM 7/13/2012 Milwaukee WI time |
|||
;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> |
|||
--[[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)