Encapsulation Win32 SDK with class in DMD2028 problem--Help wanted

Denis Koroskin 2korden at gmail.com
Fri Apr 17 03:10:47 PDT 2009


On Fri, 17 Apr 2009 13:19:16 +0400, Sam Hu <samhudotsamhu at gmail.com> wrote:

> Hello,
>
> After my reading below post:
> http://d.puremagic.com/issues/show_bug.cgi?id=2580
> I can compile the win32 SDK sample in dmd\sample folder.After that I  
> tried to encapsulate all those staff in class.Attached is my  
> chance.Below is a short description about all the 3 classes:
> WinClass:Encapsulation of WNDCLASSEX
> WinFrame:Encapsulation of window frame
> WinApp:Encapsulation of the Gui application
>
> Here I have a couple of question.It would be grateful if anybody here  
> kindly let me know,though I do know it is a bit long and it is  just an  
> excise of a beginner:
> 1.The program compiled but runs with an Access Violation error.I can not  
> found the place the error occurred.I tried to comments out the  
> try...catch block,but the program runs invisible and with 100% CPU  
> usage,I have to aborted from the task manager.

Need to look at source code to determine the reason.

> 2.Is it possible to encapsulate the callback function
> int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
> into any of the three classes?Which one should be the ideal place?

yes. Once you created a window and received a hWnd handle, bind your class and hWnd. One of the two ways:
1) global WinClass[HWND] _hWndToWinClass;
_hWndToWinClass[hWnd] = this;

2) SetWindowLong(hWnd, GWL_USERDATA, cast(int)this);

WinClass getWinClassByHWND(HWND hWnd)
{
1)    if (auto ptr = hWnd in _hWndToWinClass) return *ptr;
      return null;

2)    return cast(WinClass)GetWindowLong(hWnd, GWL_USERDATA);
}


Now when you receive a callback, get a corresponding WinClass reference and redirect to it:
extern(Windows) LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // a lookup from HWND to WinClass here:
    WinClass win = getWinClassByHWND(hWnd);

    if (win is null) {
        return DefWindowProcW(hWnd, message, wParam, lParam);
    }

    return win.wndProc(hWnd, message, wParam, lParam);
}

> 3.Is is possible to encapsulate all the framework into just one  
> class,say WinFrame,and then we can call it by
> auto app=new WinFrame;
> app.show;
> If yes,how?

It's possible, of course, but it's a lot of code to write.

> 4.Is it possible to use main() other than int WinMain(),if yes,how?
> And I do know point 3 and 4 may be a longer story and there are a lot of  
> GUI libararies for reference.But I can not gain a clear idea just by  
> reading the other's large project with out short explanation.So really  
> appreciate,if anybody can teach me.
>

I am successfully using both and have no problems. Just write void main() {} or follow win32 guide on digitalmars.com
I prefer void main() personally as it is shorter to write :)

> Thanks and best regards,
> Sam
>
>
>





More information about the Digitalmars-d-learn mailing list