Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module winglib.buttons;
- //---------------------------------------------------
- private import core.runtime;
- private import core.sys.windows.windows ;
- private import core.sys.windows.commctrl ;
- private import std.stdio ;
- private import std.string ;
- private import std.conv;
- //----------------------------------------------------
- private import winglib.commons ;
- private import winglib.controls : Control;
- private import winglib.events;
- //------------------------------------------------------
- private DWORD btnStyle = WS_CHILD | BS_NOTIFY | WS_TABSTOP | WS_VISIBLE;
- private DWORD btnExStyle = 0;
- private Button[] buttonList;
- private static int btnNumber = 1;
- private static int mIdNum = 101;
- private static int subClsID = 1001;
- //---------------------------------------------------------
- class Button : Control {
- import winglib.window : Window;
- import winglib.application : hInst = gModuleInstance;
- import winglib.enums : ControlType ;
- //-----------------------------------------------------------------------
- ButtonEvents events ;
- alias events this;
- this(Window pParent){
- with(this){
- mText = format("Button%s", btnNumber.to!string());
- mWidth = 100;
- mHeight = 35;
- mXpos = 30;
- mYpos = 30;
- mParent = pParent;
- this.mFont = this.mParent.font;
- this.mControlType = ControlType.button;
- ++btnNumber;
- }
- }
- //--------------------------------------------------------------------------------------------
- this(Window pParent, string btnText, int x, int y, int pWidth = 100, int pHeight = 35){
- with(this){
- mText = btnText;
- mWidth = pWidth;
- mHeight = pHeight;
- mXpos = x;
- mYpos = y;
- mParent = pParent;
- this.mFont = this.mParent.font;
- ++btnNumber;
- this.create();
- }
- }
- //------------------------------------------------------------------------------------------
- ~this() {
- RemoveWindowSubclass(this.handle, this.btnWindProc, this.ownSubClsId);
- writeln("A button's subclass data is removed");
- }
- //------------------------------------------------------------------------
- HWND create() {
- this.mHandle = CreateWindowEx( btnExStyle,
- cast(LPCWSTR) WC_BUTTON,
- toUString(this.mText),
- btnStyle,
- this.mXpos,
- this.mYpos,
- this.mWidth,
- this.mHeight,
- this.mParent.handle,
- cast(HMENU) mIdNum,
- hInst,
- null);
- this.misHandleCreated = true;
- this.setFont();
- ++mIdNum;
- setSubClass(); \\-------------------This is the function which causes error.
- this.mParent.controls ~= this;
- buttonList~= this;
- return this.mHandle;
- }
- //-------------------------------------------------------------------------
- protected MsgHandler[] msgHandlerList;
- /// Adding event handler functions to button events.
- void addHandler(uint evt, EvtFuncPtr fnp){
- auto mh = MsgHandler();
- mh.handle = this.mHandle;
- mh.message = evt;
- mh.fnPtr = fnp;
- mh.isActive = true;
- this.msgHandlerList ~= mh;
- }
- //---------------------------------------------------------------------------------
- private :
- Button me;
- SUBCLASSPROC btnWindProc;
- int ownSubClsId ;
- //-----------------------------------------------------------------------
- /// Find the correct MsgHandler struct for given message and hwnd.
- MsgHandler findHandler(HWND hw, uint uMsg) {
- MsgHandler result ;
- foreach (MsgHandler mh; this.msgHandlerList) {
- if (mh.handle == hw && mh.message == uMsg) {
- result = mh;
- break;
- }
- }
- return result;
- }
- //------------------------------------------------------------------------
- void setSubClass() {
- this.btnWindProc = &btnWndProc;
- this.ownSubClsId = subClsID;
- SetWindowSubclass(this.mHandle, SUBCLASSPROC(&btnWndProc), UINT_PTR(subClsID), cast(DWORD_PTR) &this);
- ++subClsID;
- }
- //------------------------------------------------------------------------
- }// End Button Class---------------------------------------------------------
- extern(Windows)
- private LRESULT btnWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR scID, DWORD_PTR refData) {
- try {
- Button thisBtn = *cast(Button*) refData; //findButton(hWnd);
- if (thisBtn.msgHandlerList.length > 0) {
- MsgHandler mh = thisBtn.findHandler(hWnd, message);
- if(mh.isActive) {
- EventArgs e = EventArgs(hWnd, message, wParam, lParam);
- with(e) {
- // msg = message;
- // handle = hWnd;
- wpm = wParam;
- lpm = lParam;
- }
- mh.fnPtr(e);
- return 0 ;
- }
- }
- }
- catch (Exception e) {}
- return DefSubclassProc(hWnd, message, wParam, lParam);
- }
- struct ButtonEvents {
- uint click = WM_LBUTTONDOWN;
- uint mouseEnter;
- uint mouseMove = WM_MOUSEMOVE;
- uint mouseLeave = WM_MOUSELEAVE;
- }
Add Comment
Please, Sign In to add comment