none
What does excel do in OnDraw()? - for printing RRS feed

  • Question

  • I hooked EndPage() of GDI API.

    And I draw a mark before EndPage().

    What I want is to draw a mark on excel document(each page).

    But when I print a excel document, first page has a content of doc and second page has a mark I drew.

    It works well in other programs.

    How do I print a mark on excel doc? ( by hookiing )

    Please answer quickly.


    Summury : Why is excel file printed twice as draw a mark before EndPage()?
    OS: Windows 7 64bits
    Office Ver : 2013

    Thursday, October 23, 2014 5:52 AM

All replies

  • Hi,

    According to your description, I would like to know the details below:

    1. How do you hook EndPage() function of GDI API?
    2. How do you draw a mark?

    Please provide the detail code and detail steps.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 24, 2014 8:02 AM
    Moderator
  • Thanks for your answer.


    First, I use EasyHook API(http://easyhook.codeplex.com/releases/view/85599) to hook.

    I Hook EndPage of gdi32. And MyEndPage is performed instead of it.


    Second, I use originally gdi+ code. And I used gdi basic functions in tests, but Excel documents is printed twice.
    This is my test code.

    int WINAPI MyEndPage(HDC hdc)
    {
     MoveToEx(hdc, (int) 0, (int) 0, (LPPOINT) NULL);
     LineTo(hdc, (int) GetDeviceCaps(hdc, HORZRES), (int)GetDeviceCaps(hdc, VERTRES));
     return EndPage(hdc);
    }


    Additionally, This is my result of some test.

    1. Just hook StartPage() and EndPage(). And not Drawing.
    - StartPage() 1 times, EndPage() 1 times.
    - Normal output

    2. Hook StartPage() and EndPage(). And Draw the line.

    i. EXCEL 2010 in Windows XP 32bits
    - StartPage(), DrawLine(), EndPage(), StartPage(), DrawLine(), EndPage() : StartPage() 2 times, EndPage() 2 times.
    - 2 page printed. First page has data and line I drew. Second page has line only.

    ii. EXCEL 2013 in Windows 7 64bits
    - StartPage(), DrawLine(), EndPage(), StartPage(), DrawLine(), EndPage() : StartPage() 2 times, EndPage() 2 times.
    - 2 page printed. First page has data only. Second page has line only.


    Please answer me again. Thanks a lot.

    Tuesday, October 28, 2014 1:33 AM
  • Hi idsj,

    First, hook is not recommend in Office development it will cause Office application unstable. I suggest that you could use the custom Add-In. (Visual Studio Tools for office)

    Secondly, about programmatically print worksheets (for VSTO), you could refer to:

    # Programmatically Print Worksheets

    http://msdn.microsoft.com/en-us/library/czhz96h7.aspx

    Secondly, I don’t familiar to that hook (EasyHook). For VSTO add-in, we could draw the mark too. For example:

    internal void Addmark()
            {
             Globals.ThisWorkbook.Application.ActiveWindow.View = Excel.XlWindowView.xlPageLayoutView;
             
            Excel.Worksheet s = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
             Excel.PageSetup ps= s.PageSetup;
              ps.CenterHeaderPicture.Filename=@"path\2.jpg";
     Globals.ThisWorkbook.Application.PrintCommunication = false;
                  ps.LeftHeader = "";
            ps.CenterHeader = "&G";
            ps.RightHeader = "";
            ps.LeftFooter = "";
            ps.CenterFooter = "";
            ps.RightFooter = "";
            ps.LeftMargin = Application.InchesToPoints(0.7);
            ps.RightMargin = Application.InchesToPoints(0.7);
            ps.TopMargin = Application.InchesToPoints(0.75);
            ps.BottomMargin = Application.InchesToPoints(0.75);
            ps.HeaderMargin = Application.InchesToPoints(0.3);
            ps.FooterMargin = Application.InchesToPoints(0.3);
            ps.Zoom = 100;
            ps.PrintErrors =Excel.XlPrintErrors.xlPrintErrorsDisplayed;
            ps.OddAndEvenPagesHeaderFooter = false;
           ps .DifferentFirstPageHeaderFooter = false;
            ps.ScaleWithDocHeaderFooter =true;
            ps.AlignMarginsHeaderFooter = true;
            ps.EvenPage.LeftHeader.Text = "";
            ps.EvenPage.CenterHeader.Text = "";
            ps.EvenPage.RightHeader.Text = "";
            ps.EvenPage.LeftFooter.Text = "";
            ps.EvenPage.CenterFooter.Text = "";
            ps.EvenPage.RightFooter.Text = "";
            ps.FirstPage.LeftHeader.Text = "";
            ps.FirstPage.CenterHeader.Text = "";
            ps.FirstPage.RightHeader.Text = "";
            ps.FirstPage.LeftFooter.Text = "";
            ps.FirstPage.CenterFooter.Text = "";
            ps.FirstPage.RightFooter.Text = "";
              Globals.ThisWorkbook.Application.PrintCommunication = true;
            }

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 30, 2014 5:46 AM
    Moderator