Jump to content

Recommended Posts

Posted

I'm trying to draw some text on my window via double buffer and I just can't figure out how tog sizing right.  I want to draw on the lower 1/3 of the window, so would think I need to create a bitmap that is 1/3 the size of the window ( 600x200 in this case ), and draw the text 2/3rds the way down and then DrawImageRect 2/3rds as well.   It does not work as I expected and I can not seem to figure out what exactly to do.  If someone could provide a little insight I'd be most grateful.

I think I see what's happening, the bitmap is the full size of the window, and so it's squished off the visible screen when I make it 600x200.  If I make my bitmap 600x600, it shows but still looks 'squished' ( maybe it's just me)  Can someone give some advice on how I can drawstringEX with doublebuffer to draw on the lower 2/3'rds of the window?  Here's a repro sample, with the 600x600 size bitmap commented out from my testing.

#include <GDIPlus.au3>
$win = GUICreate("Form",600,600)
GUISetState(@SW_SHOW)
_GDIPlus_Startup()

Local $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $aInfo, $StringToDraw = "This is a test"
Local $bgBrush = _GDIPlus_BrushCreateSolid("0xFF000000")
$hBrush = _GDIPlus_BrushCreateSolid("0xFFFFFFFF")
$tLayout = _GDIPlus_RectFCreate(0, 400, 600, 200)
$hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_StringFormatSetAlign($hFormat, 1)
$hFamily = _GDIPlus_FontFamilyCreate("Arial")
$hFont = _GDIPlus_FontCreate($hFamily, 50, 0)
$tLayout = _GDIPlus_RectFCreate(0,400,600,200)
 
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($win)
$bitmap = _GDIPlus_BitmapCreateFromGraphics(600, 200, $hGraphic)
;$bitmap = _GDIPlus_BitmapCreateFromGraphics(600, 600, $hGraphic)
$backbuffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
 
_GDIPlus_GraphicsClear($backbuffer)
_GDIPlus_GraphicsDrawStringEx($backbuffer, $StringToDraw, $hFont, $tLayout, $hFormat, $hBrush)
_GDIPlus_GraphicsDrawImageRect($hGraphic, $bitmap, 0,400,600,200)
 
MsgBox(0,"","Pause")
_GDIPlus_Shutdown()
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_GraphicsDispose($hGraphic)

 

Thanks for any help!!

Kendama

Posted

Something like this ?

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $hGUI, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
    Local $sString = "This is a Test", $aInfo

    ; Create GUI
    $hGUI = GUICreate("GDI+", 600, 600)
    GUISetState(@SW_SHOW)

    ; Draw a string
    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $hFormat = _GDIPlus_StringFormatCreate()
    $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    $hFont = _GDIPlus_FontCreate($hFamily, 65, 2)
   _GDIPlus_GraphicsFillRect($hGraphic, 0, 400, 600, 200)

    $tLayout = _GDIPlus_RectFCreate(20, 450, 0, 0)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

    ; Clean up resources
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
EndFunc   ;==>Example

 

Posted

Thanks so much for the response.  I'm really trying to use buffering to draw because when I use just DrawStringEX ( which does work as I would expect ) I get flicker when I update the text.  I know my example does not show me updating any text because I tried to simplify my code example to just the simplest set to reproduce my inability to use buffering to get the output in the right part of the window.  

In the above example, would you update to new text I the same place by writing the background (with say FillRect ) color over the text and then put the new text?  Maybe there is a better way to change the text once you've written it with DrawStringEX than what I've tried with blanking the area and writing new text?

 

Thanks again for any help.

kendama

Posted
1 hour ago, kendama said:

In the above example, would you update to new text I the same place by writing the background (with say FillRect ) color over the text and then put the new text? 

Yes, I tested it with a few strings, and no flcker, but I also put a small sleep (500) in between.

Posted

Thanks Nine I really appreciate your help. I'm using 

_GDIPlus_GraphicsFillRect($hGraphic, 0, 400, 600, 200, $BGBrush)

_GDIPlus_GraphicsDrawStringEx($hGraphic, $StringToDraw, $hFont, $aInfo[0], $hFormat, $hBrush)

on a one second loop and I do get flicker when drawing at full screen.  It's a timer application, that shows a second by second countdown. When I add any sleep delay it gets more visible or shows a noticeable blanking depending on where I insert the sleep.  It does not happen every second, say one in 3 on average, but does flicker the text usually at the bottom portion.  As this is intended to be a visual display I'm really hoping to remove the flicker as not to distract people. 

I've tested with the double buffer example above and that does not flicker but I cannot get the font the size I need or the text in the best place.  I've also tested this on about 9 different computers / laptops so do not think it's a graphics driver problem, especially because the double buffer solution although not right location, does not manifest the flicker.

Thanks,

kendama, 

Posted

Could you try this one then...

#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
  Local $hGUI, $hGFX, $hBitmap, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout
  Local $sString[5] = ["This is a Test", "A New String", "Hello Hi", "Watch Out", "Over Now"]
  Local $aInfo

  ; Create GUI
  $hGUI = GUICreate("GDI+", 600, 600)
  GUISetState(@SW_SHOW)

  ; Draw a string
  _GDIPlus_Startup()
  $hGFX = _GDIPlus_GraphicsCreateFromHWND($hGUI)
  $hBitmap = _GDIPlus_BitmapCreateFromGraphics(600, 600, $hGFX)
  $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBitmap)

  $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
  $hFormat = _GDIPlus_StringFormatCreate()
  $hFamily = _GDIPlus_FontFamilyCreate("Arial")
  $hFont = _GDIPlus_FontCreate($hFamily, 65, 2)
  _GDIPlus_GraphicsFillRect($hGraphic, 0, 0, 600, 400, $hBrush)

  For $i = 0 to Ubound($sString)-1
    _GDIPlus_GraphicsFillRect($hGraphic, 0, 400, 600, 200)
    $tLayout = _GDIPlus_RectFCreate(0,450)
    $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString[$i], $hFont, $tLayout, $hFormat)
    $aInfo[0].X = Round ((600-$aInfo[0].Width)/2)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $sString[$i], $hFont, $aInfo[0], $hFormat, $hBrush)
    _GDIPlus_GraphicsDrawImageRect($hGFX, $hBitmap, 0, 0, 600, 600)
    Sleep (500)
  Next

  ; Loop until the user exits.
  Do
  Until GUIGetMsg() = $GUI_EVENT_CLOSE

  ; Clean up resources
  _GDIPlus_FontDispose($hFont)
  _GDIPlus_FontFamilyDispose($hFamily)
  _GDIPlus_StringFormatDispose($hFormat)
  _GDIPlus_BrushDispose($hBrush)
  _GDIPlus_GraphicsDispose($hGraphic)
  _GDIPlus_GraphicsDispose($hGFX)
  _GDIPlus_BitmapDispose($hBitmap)
  _GDIPlus_Shutdown()

EndFunc   ;==>Example

 

Posted

That did the trick, thanks you so very much for spending your time to help.  I can't tell you how much I appreciate it as I was hitting my head against the wall for a while.   I'll definitely need to play with it a bit to make sure I understand what I was doing wrong and how the pieces work, but you have definitely given me enough to help me along the learning journey.

with gratitude, appreciation and awe.

Thanks,

 kendama

Posted
7 minutes ago, kendama said:

That did the trick, thanks you so very much for spending your time to help.  I can't tell you how much I appreciate it as I was hitting my head against the wall for a while.   I'll definitely need to play with it a bit to make sure I understand what I was doing wrong and how the pieces work, but you have definitely given me enough to help me along the learning journey.

with gratitude, appreciation and awe.

Thanks,

 kendama

Thank you for your kind word, glad I could help.

Happy scripting.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...