Author Topic: GUI- HDC Child Window  (Read 957 times)

0 Members and 1 Guest are viewing this topic.

Aurel

  • Guest
GUI- HDC Child Window
« on: March 07, 2020, 07:39:03 AM »
In this example i will try build permanent hdc child window
as a simple canvas for DC drawing

Code: [Select]
$ Filename "GUIstaticTest.exe" ' o2
include "RTL32.inc"
include "awinh037.inc"
#lookahead
'init windows/controls.................................................
INT win,wx=100,wy=200,ww=800,wh=608,wstyle = WS_MINMAXSIZE
INT win2,wx2=140,wy2=54,ww2=640,wh2=512,wstyle2 = WS_CHILD | WS_VISIBLE | WS_DLGFRAME
INT button0,b0ID=100, lvControl,lvID=1000
'##### GLOBALS HDC ###############################################
INT hdc, hdcMem, hbmMem,  oldBmp, oldBrush, oldPen, oldFont
INT textX,textY,hBrush
RECT rc
SYS frontColor,backColor
'open main window -----------------------------------------------------
win = SetWindow("GUI- HDC Child Window",wx, wy, ww, wh, 0, wstyle)
'open child window on main window(win)---------------------------------
win2 = SetWindow("", wx2, wy2, ww2, wh2, win, wstyle2)

'init HDC drawings
InitDrawing()
SetWindowColor(win2,100,100,200)
TextColor(win2,RGB(220,200,0),RGB(0, 0, 0)) 'fronColor,backColor
TextOn(win2,280,36,"<--- HDC Window --->")


'----------------------------------------------------------------------
 'message loop
WHILE GetMessage (wm,0,0,0)
  TranslateMessage wm
  DispatchMessage wm
 WEND

'----------------------------------------------------------------------
'window procedure
Function WndProc (sys hwnd,wmsg,wparam,lparam) as int callback
SELECT hwnd
CASE win
Select wmsg

CASE WM_CLOSE
CloseWindow(win)
              CloseWindow(win2)
              CleanUp()
EndProgram()
End Select

    CASE win2
      Select wmsg
CASE WM_SIZE
'get current size of window
GetSize(win2,0,0,ww2,wh2)

CASE WM_PAINT
' blit the memory DC buffer back to the window DC
BitBlt(hDC, 0, 0, ww2, wh2, hdcMem, 0, 0, SRCCOPY)

     
      End Select

END SELECT
Return DefWindowProc(hwnd,wMsg,wParam,lParam)
END FUNCTION

'////////////////////////////////////////////////

'##########################################################################
'##                   SET OF GDI DRAWING FUNCTIONS                     ####
'##########################################################################
SUB TextColor (wnd as INT,byval fColor as sys,byval  bColor as sys )
hdc = GetDC(wnd)
SetTextColor( hDC, fColor)
SetBkColor( hDC, bColor)
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
ReleaseDC( wnd, hdc)
END SUB

'#########################################################################
SUB FrontPen (wID as INT, penR as INT, penG as INT, penB as INT)
    'set global frontpen color
   frontColor = RGB (penR,penG,penB)
End SUB
 
'########################################################
SUB TextOn(wnd as INT,tx as INT,ty as INT,txt as string)
hdc = GetDC(wnd)
'draw text to screen DC
TextOut hdc,tx,ty,txt,Len(txt)
'blit screen DC to memDC
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
ReleaseDC( wnd, hdc)
END SUB
 
'#######################################################
Sub Pset(int wnd, sys x, y)
    hdc = GetDC(wnd)
    'pcolor = frontColor
    SetPixel(hdc, x, y, frontColor)
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
    ReleaseDC(wnd,Hdc)
End Sub

'######################################################
Sub ELIPSE(wnd as INT,x as int,y as int, r1 as int,r2 as int, fcolor as int)
hdc=GetDc(wnd)
SelectObject(hdc, CreateSolidBrush( fcolor ))
Ellipse Hdc,x,y,r1+x,r2+y
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
 ReleaseDC( wnd, hdc)
End Sub

'#########################################################################################
' set window color
Sub FillSolidRect(wnd as INT, x As Long, Y As Long, cx As Long, cy As Long, bbColor as sys)
    INT hBr,oBr ' rc As RECT
    hDC=GetDC(wnd)
    rc.Left = x
    rc.Top = Y
    rc.right = x + cx
    rc.bottom = Y + cy
    hBr = CreateSolidBrush(bbColor)
    'oBr = SelectObject hdc,hBr
    FillRect hDC, rc, hBr
    DeleteObject hBr
    BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
    'DeleteObject(SelectObject(hdc, oBr))
    ReleaseDC( wnd, hdc)
End Sub
'-------------------------------------------------------------------------------------
SUB SetWindowColor(wnd as INT,wr as INT,wg as INT,wb as INT)
    GetSize(wnd,0,0,ww2,wh2)
    backColor = RGB (wr,wg,wb)
    FillSolidRect(wnd,0,0,ww2,wh2,backColor)
End SUB

'########################################################################################
SUB Circle (wnd as INT,cix as INT,ciy as INT,cra as INT)
    hdc = GetDC(wnd)
    'backColor=rgb(200,200,0) ' for test predefined color / yellow
    SetBkMode( hDC, 1) 'transparent
   SetBkColor(hDC, backColor)
    INT np = CreatePen(PS_SOLID,1,frontColor)  'new pen with predefined color / red
    INT op = SelectObject(hdc, np)
    INT nB = CreateSolidBrush(backColor)   'new Brush
    INT oB = SelectObject(hdc, nB)

    Ellipse hdc,cix-cra,ciy-cra,cra+cix,cra+ciy

    BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY) '- not used yet
    DeleteObject(SelectObject(hdc, op))
    DeleteObject(SelectObject(hdc, oB))
    ReleaseDC( wnd, hdc)
End SUB

'#######################################################################################

SUB LineXY (wnd as INT,Lx as INT,Ly as INT,Lx1 as INT,Ly1 as INT)
hdc = GetDC(wnd)
GetSize(wnd,0,0,ww2,wh2)
INT op = SelectObject(hdc, CreatePen(PS_SOLID,1,frontColor))
MoveToEx hdc,Lx,Ly,Byval 0
LineTo hdc,Lx1,Ly1
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)
DeleteObject(SelectObject(hdc, op))
ReleaseDC( wnd, hdc)
End SUB

'#####################################################################################
SUB DrawRect(wnd as INT,rx1 as INT, ry1 as INT, rx2 as INT, ry2 as INT)
hdc = GetDC(wnd)
'GetSize(wID,0,0,ww,hh)

'SetBkMode( hDC, 1) 'transparent
SetBkColor(hDC, backColor)
int np = CreatePen(PS_SOLID,1,frontColor)  'new pen
int op = SelectObject(hdc, np)
int nB = CreateSolidBrush( backColor)   'new Brush
int oB = SelectObject(hdc, nB)
'Rectangle bHdc,x,y,w+x,h+y ...hmmm
Rectangle (hdc,rx1,ry1,rx2+rx1,ry2+ry1)

BitBlt(hDCmem, 0, 0, ww2, wh2 , hdc, 0, 0, SRCCOPY)
DeleteObject(SelectObject(hdc, op))
DeleteObject(SelectObject(hdc, oB))
ReleaseDC( wnd, hdc)
End SUB

'###################################################################################
SUB InitDrawing
''get current size of window2
GetSize(win2,0,0,ww2,wh2)
'get window DC
hdc=GetDC(win2)
hdcMem = CreateCompatibleDC(0)
hbmMem = CreateCompatibleBitmap(hdc, ww2, wh2)
oldBmp = SelectObject( hdcMem, hbmMem )
oldBrush = SelectObject(hdcMem, CreateSolidBrush( RGB(231,223,231)) )
oldPen = SelectObject(hdcMem, CreatePen(PS_SOLID,1,RGB(231,223,231)))
'fill rectangle memDC with brush color
FillRect ( hdcMem,rc, oldBrush)
SetTextColor( hDC,RGB(0,0,0))
SetBkColor( hDC, RGB(231,223,231))
'blit to memDC
BitBlt(hDCmem, 0, 0, ww2, wh2, hdc, 0, 0, SRCCOPY)

ReleaseDC( win2, hdc)
End SUB

'###################################################################################
SUB CleanUp

DeleteDC(hdcMem)
DeleteObject(SelectObject(hdcMem, oldBrush))
DeleteObject(SelectObject(hdcMem, oldPen))
DeleteObject(SelectObject(hdcMem, oldBmp))

END SUB
« Last Edit: March 07, 2020, 08:21:33 AM by Aurel »