Author Topic: Wrap window procedure  (Read 6458 times)

0 Members and 2 Guests are viewing this topic.

Aurel

  • Guest
Wrap window procedure
« on: July 21, 2012, 10:41:35 PM »
Hi Charles...

Do you have idea how avoid to much typing like:
Code: [Select]
Function WndProc(byval hWnd as long,byval wMsg as long, byval wParam as long,byval lparam as long) as long callbackand
Code: [Select]
FUNCTION = DefWindowProc hwnd,wMsg,wParam,lParam
I ask this because many other compilers/interpreters can wrap this code into something
simplier?
I have search trough source codes but i don't find something concrete to do this...
Your opinion?

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #1 on: July 21, 2012, 11:08:16 PM »
Hi Aurel,

The most compact form for WndProc is:

function WndProc ( sys hWnd, wMsg, wParam, lparam ) as sys callback

or even:

function WndProc ( hWnd, wMsg, wParam, lparam ) as sys callback

When no type and no ByVal/ByRef is specified then the variable are assumed to be of Sys type.

Charles

Aurel

  • Guest
Re: Wrap window procedure
« Reply #2 on: July 22, 2012, 08:19:36 AM »
Hi Charles..
Ok.
Maybe i'm not clear enough what i want.
How do do this on minimalistic way?
Or in another words how bypass winProc or put whole function in hendler
or class maybe?
Any idea about that?

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #3 on: July 22, 2012, 08:49:13 AM »
It can be done by using a macro. but it would not be a step in the right direction, it you intend the user to have full access to Wndproc and its parameters.

Can you give an example of an implementation you like, Aurel?

Charles

Aurel

  • Guest
Re: Wrap window procedure
« Reply #4 on: July 22, 2012, 11:30:06 AM »
Hi Charles...
Maybe is this way not best aproach for end user but simplify things a lot.
For example like we all know implementation from thinBasic,EBasic,PureBasic and similar
interpreters/compilers
 for example thinB:
Code: [Select]
function TBMain()
DIM hDlg    AS LONG  
'---Create a new dialog
DIALOG NEW 0, "ListBox example", -1, -1, 275, 120, _ %WS_DLGFRAME OR %DS_CENTER OR %WS_CAPTION OR %WS_SYSMENU, _ 0 TO hDlg

'---Show dialog in MODAL state
    DIALOG SHOW MODAL hDlg, call dlgCallback
  end function
  
  callback function dlgCallback() as long

    '---Now test the message
    SELECT CASE cbMsg

Or EBasic more simplifyed:
Code: [Select]
WINDOW w1
int up
REM open the main window
OPENWINDOW w1,0,0,350,350,@MINBOX|@MAXBOX|@SIZE,0,"Test for",&main

WAITUNTIL w1 = 0
END
'---

SUB main
IF @message = @IDCLOSEWINDOW
   CLOSEWINDOW w1
ENDIF
RETURN
ENDSUB

And in both i think(thinBasic to) can use direct way trough acces to
window procedure.
And many others basic-like compilers have.
I think that you have now picture about what i am talking.
Any suggestion will be great :)

PS.I found one interesting article here:
http://www.gamedev.net/page/resources/_/technical/general-programming/creating-a-win32-window-wrapper-class-r1810

By the way on rosetta code is many GUI examples in
many languages who are very simple..
http://rosettacode.org/wiki/Window_creation
« Last Edit: July 22, 2012, 04:18:17 PM by Aurel »

Aurel

  • Guest
Re: Wrap window procedure
« Reply #5 on: July 23, 2012, 01:08:48 AM »
Charles...you say with macros ,do you can add any example about that?

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #6 on: July 23, 2012, 05:19:07 AM »
Yes, thanks for those examples, Aurel.

I am refactoring the demo Oxide: This macro is deployed within the WM_CREATE case.

Oxygen macros can be placed anywhere and have local scope. I think this is an acceptable use of macros because it does not hide the code.

Code: [Select]
   macro EditW(i,style)
    hchw(i)=CreateWindowEx(0,"edit",null,style,0,0,0,0,hwnd,id+i,inst,null)
    end macro
    '
    EditW 0,MainEditStyle
    EditW 1,style
    EditW 2,style
    EditW 3,style

Charles

Aurel

  • Guest
Re: Wrap window procedure
« Reply #7 on: July 23, 2012, 06:44:38 AM »
Hi Charles...
This is very cool way to create windows controls with ID and style params only
But how on similar way create window ?
In those examples are used OOP way of creation - is there a way to do this in traditional way?
Another point is in those examples is used last parameter (lpVoid) as pointer to window
procedure ,if i understand this correctly ...right?

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #8 on: July 23, 2012, 08:44:50 AM »

My best generic solution for creating the main window. It also intercepts a number of messages and relays them to the parent window via WM_COMMAND messages

MainWindow 480,480,WS_OVERLAPPEDWINDOW

Code: OxygenBasic
  1.   Function MainWindow(width,height,style)
  2.   =======================================
  3.   '
  4.  sys      a,b,c,hWnd
  5.   WNDCLASS wc
  6.   MSG      wm
  7.   '
  8.  inst=GetModuleHandle 0
  9.   '
  10.  with wc.                 '
  11.    style=CS_HREDRAW or CS_VREDRAW
  12.     lpfnWndProc   = @wndproc
  13.     cbClsExtra    = 0
  14.     cbWndExtra    = 0    
  15.     hInstance     = inst
  16.     hIcon         = LoadIcon 0, IDI_APPLICATION
  17.     hCursor       = LoadCursor 0,IDC_ARROW
  18.     hbrBackground = GetStockObject WHITE_BRUSH '
  19.    lpszMenuName  = 0
  20.     lpszClassName = strptr "wins"
  21.   end with
  22.  
  23.  
  24.   if not RegisterClass @wc
  25.     MessageBox 0,`Registration failed`,`Problem`,MB_ICONERROR
  26.     exit function
  27.   end if
  28.  
  29.  
  30.  
  31.   'Create the main window.
  32.  
  33.    hwnd = CreateWindowEx(
  34.     0,                      'no extended styles          
  35.    "wins",                 'class name                  
  36.    "Main Window",          'window name  
  37.    style,                  '                
  38.    CW_USEDEFAULT,          'default horizontal position  
  39.    CW_USEDEFAULT,          'default vertical position    
  40.    width,                  'default width                
  41.    height,                 'default height              
  42.    null,                   'no parent or owner window    
  43.    null,                   'class menu used              
  44.    inst,                   'instance handle              
  45.    null);                  'no window creation data      
  46.  
  47.      
  48.   if not hWnd then
  49.     MessageBox 0,`Unable to create window`,`problem`,MB_ICONERROR
  50.     exit function
  51.   end if
  52.                          '
  53.  ShowWindow hWnd,SW_NORMAL
  54.   UpdateWindow hWnd
  55.   '
  56.  'MESSAGE LOOP
  57.  '============
  58.  '
  59.  sys bRet
  60.   '
  61.  while bRet := GetMessage @wm, 0, 0, 0
  62.     if bRet == -1 then
  63.       'show an error message?
  64.    else
  65.       m=wm.message
  66.       select m
  67.       '
  68.      case WM_LBUTTONDBLCLK
  69.       =====================
  70.         '
  71.        SendMessage hwnd,WM_COMMAND,1100-0x200+m, 0
  72.         '
  73.      case WM_KEYDOWN
  74.       ===============
  75.         '
  76.        m=wm.wparam
  77.         select m
  78.         case VK_RETURN
  79.           for i=1 to nchw
  80.             if wm.hwnd=hchw(i)
  81.               SendMessage hwnd,WM_COMMAND,2000+VK_RETURN,0
  82.               if i>1 then continue while 'first window is editor
  83.            end if
  84.           next
  85.         case VK_F1 to VK_F10
  86.           SendMessage hwnd,WM_COMMAND,1001+m-VK_F1, 0
  87.         case "A" to "Z"
  88.           if CtrlKey
  89.             SendMessage hwnd,WM_COMMAND,2000+m, 0
  90.             if m=65 or m=70 or m=71 then continue while
  91.           end if
  92.         case VK_ESCAPE
  93.           SendMessage hwnd,WM_COMMAND,2200+VK_ESCAPE, 0
  94.           Continue do
  95.         end select
  96.         '
  97.      end select
  98.       '
  99.      TranslateMessage @wm
  100.       DispatchMessage @wm
  101.     end if
  102.   wend
  103.   '
  104.  end function ; end of WinMain
  105.  

Charles

Aurel

  • Guest
Re: Wrap window procedure
« Reply #9 on: July 23, 2012, 09:51:27 AM »
Charles..
I recive whole set of errors when i try compile this example  ???
Where is WndProc function stored because i don't see any include command?

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #10 on: July 23, 2012, 10:10:07 AM »
Intended for study only :)

It is part of OxideUtil.inc  at present but I may deploy it in a Winutil, to replace WinMain in most GUI demos.

Then for simple applications, only WndProc would need to be coded.

Aurel

  • Guest
Re: Wrap window procedure
« Reply #11 on: July 23, 2012, 12:30:22 PM »
Aha i see... ;D
I make some search trough FB forum and found Vanya window9.bi which is
complete GUI wraper but is written like syntax-clone of PureBasic  ::)
I don't say that is not good but this simply is not style of Oxygen if you agree.
Most problematic part in GUI wrapper is message router which most articles define
as static member,what a heck is a static member ???

Your way is clean if you ask me,i will do more research....

Charles Pegge

  • Guest
Re: Wrap window procedure
« Reply #12 on: July 23, 2012, 02:18:57 PM »

In OOP, a static member is a variable/function shared by all the objects of a class. If one object changes the value of the variable, then all the other objects defined with this class will see it.

Implementing WndProc inside a class is a little complicated, because there is only one WndProc callback servicing the main window and all its child windows. You get added complexity without much advantage because wndproc does not fit directly into the object model.

So I think the practical solution is to wrap WinMain, then  use wndproc directly, or with callbacks, or to set variables as Peter does.

Aurel

  • Guest
Re: Wrap window procedure
« Reply #13 on: July 23, 2012, 10:26:53 PM »
Quote
Implementing WndProc inside a class is a little complicated, because there is only one WndProc callback servicing the main window and all its child windows. You get added complexity without much advantage because wndproc does not fit directly into the object model.

I agree with you...not very much advantage because u must again fill msgRoutine with full
code...
We will see...

Aurel

  • Guest
Re: Wrap window procedure
« Reply #14 on: July 24, 2012, 08:27:20 AM »
Charles..
How is possible that Oxygen accept SUB and FUNCTION as same  ???
Code: [Select]
sub WinMain(hw,msg,wp,lp) as sys callback

SELECT msg

CASE WM_DESTROY
PostQuitMessage 0

END SELECT

Return DefWindowProc(hw,msg,wp,lp)

END sub

FUNCTION WinMain(hw,msg,wp,lp) as sys callback

SELECT msg

CASE WM_DESTROY
PostQuitMessage 0

END SELECT

Return DefWindowProc(hw,msg,wp,lp)

END FUNCTION