kcvinu

How can i do some stuff in $WM_CREATE and $WM_PAINT

6 posts in this topic

Hi all, 

i am playing with some gui code. I am trying to do some stuff in WM_CREATE. This is my code. But this is not working.  Somebody please guide me.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
Local $hForm1 = GUICreate("Hello Program", 550, 306, 277, 153)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

Local $hLabel1 = GUICtrlCreateLabel("Hello from AutoIt", 200, 120, 136, 17)

Local $hBtn = GUICtrlCreateButton("Click", 100, 100, 86, 57)
GUICtrlSetOnEvent(-1, "BtClick")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;~ GUIRegisterMsg($WM_CREATE, "WM_Create")
Local $idMsg = 0
$whHook = DllCallbackRegister('_WinProc', 'ptr', 'hwnd;uint;wparam;lparam')
$wpHook = DllCallbackGetPtr($whHook)
While 1
    Sleep(50)
;~  $idMsg = GUIGetMsg()
;~  Select
;~      Case $idMsg = $WM_CREATE
;~          SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav",1)
;~          ;ConsoleWrite("Created" & @CRLF)

;~      Case $WM_PAINT
;~          GUICtrlSetData($hLabel1,"Label After Painted")
;~          ;ConsoleWrite("Painted" & @CRLF)
;~      Case $WM_DESTROY
;~          Exit
;~  EndSelect
WEnd

Func Form1Close()
    Exit
EndFunc

Func BtClick()
    ;$imsg = GUIGetMsg(1)
    ;_ArrayDisplay($imsg)
    SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav",1)
EndFunc

Func WM_Create($hWnd, $iMsg, $wParam, $lParam)
    #forceref $Hwnd, $iMsg
    SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav",1)
    Return 0
EndFunc

Func _WinProc($hWnd, $iMsg, $wParam, $lParam)
    Local $hDC, $hSv, $oldMsg
    ConsoleWrite('_winProc $lParam: ' & $lParam / 255 & @CRLF)
    Switch $iMsg
        Case $WM_CREATE
            SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav",1)
            ConsoleWrite('WM_CREATE:' & @CRLF)
            Return 0
    EndSwitch
    Return _WinAPI_DefWindowProc($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>_WinProc

 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

First i tested with GUIRegisterMsg. But didn't worked. Then i tested with _WinProc function. (I got that function from this forum after a short search) 

Actually i got this idea from this C code. This is from the great Charles Petzold's book "Windows Programming 5th Edition". This is the code

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("HelloWin") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName,                  // window class name
                          TEXT ("The Hello Program"), // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
     
     switch (message)
     {
     case WM_CREATE:
          PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
          return 0 ;
          
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          
          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          
          EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;

 

Edited by kcvinu

My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

a simple example...

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=

Local $hForm1 = GUICreate("Hello Program", 550, 306, 277, 153)

GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

Local $hLabel1 = GUICtrlCreateLabel("Hello from AutoIt", 200, 120 + 50, 136, 17)

Local $hBtn = GUICtrlCreateButton("fire Create Event creating a new GUI", 100, 100, 200, 57)
GUICtrlSetOnEvent(-1, "BtClick")

GUIRegisterMsg($WM_CREATE, "WM_Create")
GUIRegisterMsg($WM_PAINT, "WM_Paint")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


Local $idMsg = 0

While 1
    Sleep(50)
;~  $idMsg = GUIGetMsg()
;~  Select
;~      Case $idMsg = $WM_CREATE
;~          SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav",1)
;~          ;ConsoleWrite("Created" & @CRLF)

;~      Case $WM_PAINT
;~          GUICtrlSetData($hLabel1,"Label After Painted")
;~          ;ConsoleWrite("Painted" & @CRLF)
;~      Case $WM_DESTROY
;~          Exit
;~  EndSelect
WEnd

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

Func BtClick()
    ;$imsg = GUIGetMsg(1)
    ;_ArrayDisplay($imsg)
;~  SoundPlay("C:\Users\Vinod\Desktop\ProgWin5\Chap03\HelloWin\HelloWin.wav", 1)
    GUICreate("New GUI")
    GUISetState(@SW_SHOW)
EndFunc   ;==>BtClick

Func WM_Create($hWnd, $iMsg, $wParam, $lParam)


    ConsoleWrite("Create" & @CRLF)
    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_Create

Func WM_Paint($hWnd, $iMsg, $wParam, $lParam)


    ConsoleWrite("Paint" & @CRLF)
    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_Paint

Saludos

Share this post


Link to post
Share on other sites

Example with callback...

#include <WinAPIRes.au3>
#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Local Const $sClass = 'MyWindowClass'
Local Const $sName = 'Test ' & StringReplace(@ScriptName, '.au3', '()')

Global $g_bExit = False

; Get module handle for the current process
Local $hInstance = _WinAPI_GetModuleHandle(0)

; Create a class cursor
Local $hCursor = _WinAPI_LoadCursor(0, 32512) ; IDC_ARROW

; Create a class icons (large and small)
Local $tIcons = DllStructCreate('ptr;ptr')
_WinAPI_ExtractIconEx(@SystemDir & '\shell32.dll', 130, DllStructGetPtr($tIcons, 1), DllStructGetPtr($tIcons, 2), 1)
Local $hIcon = DllStructGetData($tIcons, 1)
Local $hIconSm = DllStructGetData($tIcons, 2)

; Create DLL callback function (window procedure)
Local $hProc = DllCallbackRegister('_WndProc', 'lresult', 'hwnd;uint;wparam;lparam')

; Create and fill $tagWNDCLASSEX structure
Local $tWCEX = DllStructCreate($tagWNDCLASSEX & ';wchar szClassName[' & (StringLen($sClass) + 1) & ']')
DllStructSetData($tWCEX, 'Size', DllStructGetPtr($tWCEX, 'szClassName') - DllStructGetPtr($tWCEX))
DllStructSetData($tWCEX, 'Style', 0)
DllStructSetData($tWCEX, 'hWndProc', DllCallbackGetPtr($hProc))
DllStructSetData($tWCEX, 'ClsExtra', 0)
DllStructSetData($tWCEX, 'WndExtra', 0)
DllStructSetData($tWCEX, 'hInstance', $hInstance)
DllStructSetData($tWCEX, 'hIcon', $hIcon)
DllStructSetData($tWCEX, 'hCursor', $hCursor)
DllStructSetData($tWCEX, 'hBackground', _WinAPI_CreateSolidBrush(_WinAPI_GetSysColor($COLOR_3DFACE)))
DllStructSetData($tWCEX, 'MenuName', 0)
DllStructSetData($tWCEX, 'ClassName', DllStructGetPtr($tWCEX, 'szClassName'))
DllStructSetData($tWCEX, 'hIconSm', $hIconSm)
DllStructSetData($tWCEX, 'szClassName', $sClass)

; Register a window class
_WinAPI_RegisterClassEx($tWCEX)

; Create a window
_WinAPI_CreateWindowEx(0, $sClass, $sName, BitOR($WS_CAPTION, $WS_POPUPWINDOW, $WS_VISIBLE), (@DesktopWidth - 400) / 2, (@DesktopHeight - 400) / 2, 400, 400, 0)

While 1
    Sleep(100)
    If $g_bExit Then
        ExitLoop
    EndIf
WEnd

; Unregister window class and release unnecessary resources
_WinAPI_UnregisterClass($sClass, $hInstance)
_WinAPI_DestroyCursor($hCursor)
_WinAPI_DestroyIcon($hIcon)
_WinAPI_DestroyIcon($hIconSm)

DllCallbackFree($hProc)

; Func _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam)
; Local $aRet = DllCall('user32.dll', 'lresult', 'DefWindowProcW', 'hwnd', $hWnd, 'uint', $iMsg, 'wparam', $wParam, 'lparam', $lParam)

; If @error Then
; Return SetError(1, 0, 0)
; EndIf
; Return $aRet[0]
; EndFunc   ;==>_WinAPI_DefWindowProcW

Func _WndProc($hWnd, $iMsg, $wParam, $lParam)
    Switch $iMsg
        Case $WM_CLOSE
            $g_bExit = True
        Case $WM_CREATE
            ConsoleWrite("Create" & @CRLF)

        Case $WM_PAINT
            ConsoleWrite("Paint" & @CRLF)

    EndSwitch
    Return _WinAPI_DefWindowProcW($hWnd, $iMsg, $wParam, $lParam)
EndFunc   ;==>_WndProc

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

@Danyfirex Thanks for your reply. First simple exapmple didn't worked. But the second example with callback is superb and worked as expected.  After all, The second example is the ditto translation of the C code. I liked the way you did it. So this is my question- Is it possible to use the $WM_CREATE like messages in normal window creation function ?


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

why first example does not work?

 

what do you mean with  use the "$WM_CREATE like messages in normal window creation"?

 

If you mean to get to handle first GUICreate creation you must register your message $WM_CREATE before call GUIcreate.

Saludos

Share this post


Link to post
Share on other sites

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