Thread: Problem in message handling VC++

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Arrow Problem in message handling VC++

    Hello friends,


    First off, please check below code. Well, I am developing printing functionality from the windows mobile device using vc++. (I am novice to the vc++ J). For printing, we are using third party DLLs. Below are important snippet required to explain the complete picture. Currently, the problem I am more concerning is printing multiple pages. For this, we have API and everything. While printing using “mpPreviewDialog” API (provided by third party in form of DLL) and when it recognize more than one pages required, it raises/passes “MP_EVENT_REQ_NEW_PAGE” message from printing library, ie mpPreviewDialog itself, to the handle hWnd of “mpPreviewDialog(hWnd,__)”. Now, to handle this message, we have put all logic portion in one class which is inherited from CWnd so that we can override “DefWindowProc” function to achieve our goal. But while I run the application and command print for multiple page, it only prints the first page perfectly and then just get hanged. Thus, it fails to handle the “MP_EVENT_REQ_NEW_PAGE” message of “DefWindowProc” function. What all I need is to handle this message and execute the case MP_EVENT_REQ_NEW_PAGE: while printing and multiple page required. Because in this case it issues the message but appropriate case never get executed. I hope you got my point. Please let me know how to solve this. Am I missing something? Thank you very much for your time in advance.


    Code:
    class CFxPrinterWnd : public CWnd
    
    {
    
                    DECLARE_DYNCREATE(CFxPrinterWnd)
    
     
    
    public:
    
                    CFxPrinterWnd();           // protected constructor used by dynamic creation
    
                    virtual ~CFxPrinterWnd();
    
     
    
    public:
    
    #ifdef _DEBUG
    
                    virtual void AssertValid() const;
    
    #ifndef _WIN32_WCE
    
                    virtual void Dump(CDumpContext& dc) const;
    
    #endif
    
    #endif
    
     
    
    protected:
    
                    DECLARE_MESSAGE_MAP()
    
    public:
    
                    int PrintReport(HWND hwnd, int flg);
    
    protected:
    
                    virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    
    };
    
     
    
    Int CFxPrinterWnd::PrintReport (HWND hWnd, int flg)
    
    {
    
                    _______
    
     
    
                    This contains “mpPreviewDialog” API call which starts printing and issue “MP_EVENT_REQ_NEW_PAGE” message if more than one page required.
    
    }
    
     
    
    LRESULT CFxPrinterWnd::DefWindowProc(___,___,___)
    
    {
    
                    Switch (message)
    
                    {
    
                                    ___
    
     
    
                                    Case MP_EVENT_REQ_NEW_PAGE:       //This debug point never get focused while printing and new page required.
    
                                                    ___This point is never get executed
    
                                                    ___
    
                    }
    
    }
    
     
    
    Button1 Click Event
    
    {
    
                    hWnd = ::FindWindow(NULL, _T(“del”));
    
                    CFxPrinterWnd wnd;
    
                    Wnd.Create(_______________________);
    
                    Ret = wnd.PrintReport(hWnd, 0);
    
                    ___
    
                    ___
    
                    mpPreviewDialog (hWnd____);                //This is the printing API where MP_EVENT_REQ_NEW_PAGE message get raised while printing if new page            
    
                                                                                                    //required.
    
                    wnd.DestroyWindow();
    
    }
    Let me put the scenario in this way:
    The window containing "Print" button in our application is designed by some other tool, and we triggered its click event and call vc++ functions in this way (In the first section, I have already put the code). Now our report to be printed contains more than one page. In third party provided printing library, it raise "MP_EVENT_REQ_NEW_PAGE" after printing the first page. (So, it;s like: Click on Print button > It will print the first page > If next page is pending then it will reaise this message > then from that point the next page get started for printing > so on...)This message is raised because there are page(s) pending to be printed. To handle this, we have created a class inherited from CWnd. But, now you please let me know where and how can I handle this message and start process for printing the next page? You inform me your design idea to accomodate this.


    ---

    Kind Regards,

    Sachin Patel

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Looks like MFC....

    In MFC you do not need a callback.

    Is MP_EVENT_REQ_NEW_PAGE a windows message sent via the Windows msg queue?

    If so...

    In the CFxPrinterWnd cpp file, under BEGIN_MESSAGE_MAP add a line to handle the msg.

    ON_COMMAND(MP_EVENT_REQ_NEW_PAGE, OnNewPageReq)


    This tells the class/window that MP_EVENT_REQ_NEW_PAGE msgs are handled by the OnNewPageReq method.

    You will have to code the OnNewPageReq() method

    LRESULT CFxPrinterWnd::OnNewPageReq( WPARAM wParam, LPARAM lParam )
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Arrow Problem in message handling VC++

    Hello,
    Thank you for the reply.
    Well, "mpPreviewDialog" API raises the "MP_EVENT_REQ_NEW_PAGE" message after printing the first page and if the next page is required. So, it is printing DLLs that raises this message.

    --
    Regards,
    Sachin.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    And 'mpPreviewDialog' documentation tells you to do what?

    I suspect draw the next page on the supplied DC.

    Why don't you contact the third party SW supplier for instructions?

    From the info you have supplied I can not help further.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Arrow Problem in message handling VC++

    Hello,
    Thank you for the replies.
    From third party side, everything is okay. Their sample application is also working fine. In our application, printing is performed successfully. The problem occurs only in case of printing the sub-sequent page. I need a proper mechanism in VC++ (as I am novice in this area) that handle that message raised from the API to the window. To achieve this, I have created a class that is inherited from CWnd, so that I can handle messages.
    Below is the latest design I am using. Please check it.

    Code:
    class CFxPrinterWnd : public CWnd
    {
    	DECLARE_DYNCREATE(CFxPrinterWnd)
    
    public:
    	CFxPrinterWnd();           // protected constructor used by dynamic creation
    	virtual ~CFxPrinterWnd();
    
    public:
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    #ifndef _WIN32_WCE
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    #endif
    
    protected:
    	DECLARE_MESSAGE_MAP()
    public:
    	int PrintReport(int flg);
    protected:
    	virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    };
    
    
    
    LRESULT CFxPrinterWnd::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch( message )
    	{
    		case WM_DESTROY:
    			// e.g. Stop the printing if it was still running and the window or app is closed.
    			mpUninitialize();
    			Sleep(1000);
    			break;
    
    		case MP_EVENT_REQ_NEW_PAGE:
    			int ret;
    			ret = PrintReport(1);
    			if (ret == 0)
    			{
    				mpReqPrintNewPage(NULL, FALSE );
    			}
    			else if (ret == 1)
    			{
    				mpReqPrintNewPage(NULL, TRUE);
    			}
    	}
    
    	return CWnd::DefWindowProc(message, wParam, lParam);
    }
    
    
    void CAboutDlg::OnBnClickedButton1()
    {
    	int ret;
    
    	ret = wnd.PrintReport(0);
    }
    
    int CFxPrinterWnd::PrintReport(int flg)
    {
    		... ... ...
    		... ... ...
    		if (flg == 0)
    		{
    			CString title;
    			title = _T("del");
    			hWnd = ::FindWindow(NULL, title);
    			CFxPrinterWnd::Attach(hWnd); 
    
    			if (hWnd == NULL)
    			{
    				::AfxMessageBox(_T("FindWindow failed.")); 
    				return -1;
    			}
    			//Initializing the print library
    			gl_hPrintDC = mpInitialize(hWnd, sizePaper, 1);
    
    			if (gl_hPrintDC == NULL)
    			{
    				::AfxMessageBox(_T("mpInitialize failed.")); 
    				return -1;
    			}
    	
    			... ... ...
    			... ... ...
    
    		}
    		
    }
    Now, in the Attach function, I am getting "Debug Assertion failed!" error message at wincore.cpp Line: 380 which is like "ASSERT(FromHandlePermanent(hWndNew) == NULL);".

    Can you please let me know how to solve this? If you can find any other way to implement this, please let me know.

    --
    Regards,
    Sachin.

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    My guess would be that

    hWnd = ::FindWindow(NULL, title);

    is failing to find the window, returning NULL and the Attach is ASSERTing.

    Instead use (EDIT: remove FindWindow() and Attach() )

    hWnd = GetSafeHwnd();


    class CFxPrinterWnd : public CWnd
    This looks like MFC.

    If it is MFC you can add a handler for the MP_EVENT_REQ_NEW_PAGE msg as I described in my previous post so you do not need a DefWndProc().
    Last edited by novacain; 01-16-2009 at 09:29 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classic Linking SDL in VC Problem
    By IdioticCreation in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 08:39 AM
  2. Error message in tic tac toe problem please help
    By Kross7 in forum C++ Programming
    Replies: 17
    Last Post: 04-10-2007, 01:50 PM
  3. MFC Message Map Problem
    By durban in forum Windows Programming
    Replies: 1
    Last Post: 11-02-2005, 08:18 AM
  4. MFC Message Map Problem
    By durban in forum C++ Programming
    Replies: 10
    Last Post: 10-31-2005, 01:55 PM
  5. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 11:59 AM