|
Hi,
I am getting around to using a printer direct. It is a Steep Learning Curve! Prior to this, I produced Text Files, which could be Printed via Notepad.
Grabbed the Bull by the Horns, Want to print Straight from the Program.
I am using MFC42. The Target Machines are Win XP-SP1. (None are Internet Enabled, The System is Outside the Internet)
The Second Hand Win 8 computer I use to write on, has a Device Driver for an HP Printer. All goes well, until I hit the Print button in the Print Dlg. Then everything crashes.
End up After the Crash with a Dlg to tell me in very many words that the printer device could not be found!
Now, I know that there is no physical printer attached to my machine, but, there should be a more graceful idea of notifying me about that fact, other than crashing.
For Instance, Can I query the CDC about a Printer being Plugged In, etc.
Kind Regards,
Bram van Kampen
|
|
|
|
|
Are you using CPrintDialog Class[^] to get the details of the printer? If you do not have a physical printer attached you can use the MS XPS writer, or if you have a PDF print application installed you can use that.
|
|
|
|
|
Well, Thanks Richard.
I worked the sample given by Chris Mauder on the MFC Forum about Printing without the Document-View Architecture. I use the CPrintDialog to obtain a pointer to a device Context, presumably in my case pointing to a HP Printer,(Apparently Installed by the Previous Owner of the Laptop) and all appears to work well. I retrieve a (pointer to) a CDC from the CPrintDialog, and the Pointer is Not NULL.
The Only thing is that there is no HP, (or, any) Printer Plugged In. (It is my Coding Laptop) My experience with say 'MS Office' is, that when the printer is not actually plugged in at the time, that the job gets spun off to the print spooler where it remains until that printer becomes available.
Is this a Feature of the Windows Print Spooler, or, a Feature of 'MS Office'?
The result in my case is a very major Foul Up, and, Minutes Later, a Message from the HP Driver that the Device cannot be found, complete with Major Advice to check Cable Connections, and a Range of Other Things.
Now, on Further Reflection, (time for quiet reflection is something that we are unlikely (and Unwisely) allow to afford ourselves when hunting for Bugs.)
It may well be that the HP Printer Driver is corrupted. My Coding Laptop is not on the Internet for obvious reasons.(Installation of the "Ms Dev Studio97" on a Win7 Computer gives an Immediate alarm under 'IsGenuine' for the OS)
I'll try to install another printer driver, (from a CD), and see how it goes.
I am ashamed to say that I have avoided Printing for the last 25 years, actually, since my Dos Days.
I have decided to bite the bullet on this Application.
I Dealt with printing in the past by sending things to a Text File, and to use NotePad, when a Printout was needed.
Also, seeing that I only need to print only a few pages, mostly One A4, sometimes 2 or 3, I did not go to the complication of doing this in a Worker Thread. I want to learn how to Crawl, before trying to learn how to Run.
Would this have an Impact. Chris Mauder's example did not mention threads.
The thing is, when things go wrong, and, especially in a for me a New and crucial Field, is: Am 'I' doing something Wrong, or Misunderstanding Something.
Once Again, thanks for your response, Richard.
I'll keep you posted on how I get on.
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: Is this a Feature of the Windows Print Spooler, or, a Feature of 'MS Office'? That's the spooler, which will try to send the file to the print driver. The driver may then pop up a dialog to say the printer is offline. In all my experience the file will remain in the spooler until it gets printed, or the user cancels it. What you call a major Foul Up sounds like normal behaviour. As I said before, you should have the Windows XPS print driver (creates an XPS file) installed so you can test with that.
|
|
|
|
|
He is using XP SP1. That does not has the XPS driver and installing it requires probably a higher SP version.
But using a PDF driver as already suggested by you should do the trick.
|
|
|
|
|
But using Win 8 for development.
|
|
|
|
|
You are right. I missed that.
|
|
|
|
|
That's OK, I missed the XP bit. 
|
|
|
|
|
|
Bram van Kampen wrote: The XPS File I got was,in Binary. As you would expect, since it also includes formatting information about the content. I have used this to test printing in my applications, and never had a problem. I guess you need to look at your actual printing code.
|
|
|
|
|
Well,
The Binary I get refuses to open.
Here is my Printing Code:-
void CConfirmOrdersStep1Dlg::OnPrint()
{
if(CreateViewForPrinting()==false){
AfxMessageBox("No Orders have been Marked for Printing");
return;
}
CDC dc;
CPrintDialog printDlg(FALSE);
<pre>
if (printDlg.DoModal() == IDCANCEL) return;
dc.Attach(printDlg.GetPrinterDC()); dc.m_bPrinting = TRUE;
CString strTitle="Old Orders";
// strTitle.Format("Date:Confirming Old Orders By:- %s-%s", UserName,Date);
DOCINFO di;
memset(&di, 0,sizeof (DOCINFO));
di.cbSize = sizeof (DOCINFO);
di.lpszDocName = strTitle;
BOOL bPrintingOK = dc.StartDoc(&di);
CPrintInfo Info;
Info.m_rectDraw.SetRect(0,0,
dc.GetDeviceCaps(HORZRES),
dc.GetDeviceCaps(VERTRES));
OnBeginPrinting(&dc, &Info);
UINT page;
for (page=Info.GetMinPage(); page <= Info.GetMaxPage() && bPrintingOK;page++){
dc.StartPage();
Info.m_nCurPage = page;
PrintPage(&dc, &Info);
bPrintingOK = (dc.EndPage() > 0);
}
OnEndPrinting(&dc, &Info);
if (bPrintingOK)
dc.EndDoc();
else
dc.AbortDoc();
dc.DeleteDC();
}
// As from Chris Mauder.
// Instead of Global I made it a member
//
// m_pView (Type COrderPageView) contains a list of PageFragments
// The Fragment at index 0 is the header, to be printed at the Top
// of each Page (Customer Details, etc)
// CondensePages() does Nothing for Now.
//
// Anyways, This Works, as I can successfully print the Page to a Text
// File.
void CConfirmOrdersStep1Dlg::OnBeginPrinting(CDC * pDC, CPrintInfo * pInfo)
{
// Set the Font to "CourierNew"
CFont PrinterFont;
if(!PrinterFont.CreatePointFont(
100,
"Courier New",
pDC)){
ASSERT(NULL);
return;
}
CSize Size=pDC->GetTextExtent("AAAA",4);
m_nCurrentPrintRectWidth=Size.cx*10;
m_nCurrentLineHeight=Size.cy;
int PageHeight=pInfo->m_rectDraw.Height();
m_nLinesPerPage=PageHeight/m_nCurrentLineHeight;
m_pView->CondensePages(m_nLinesPerPage);
}
/////////////////////////////////////////////////////////////////
//
// This is where we print, First the Header, and thereafter the Page Content
void CConfirmOrdersStep1Dlg::PrintPage(CDC * pDC, CPrintInfo * pInfo)
{
int PageNumber=pInfo->m_nCurPage;
m_nCurrentPrintRectTop=0;
COrderPageView* pView=(COrderPageView*)m_pView->GetDataFromIndex(0);
PrintDocFragment(pView,pDC);
COrderPageView* pOrder=(COrderPageView*)m_pView->GetDataFromIndex(PageNumber);
PrintDocFragment(pOrder,pDC);
}
void CConfirmOrdersStep1Dlg::OnEndPrinting(CDC * pDC, CPrintInfo * pInfo)
{
}
// We Create a List of Set of lists of 40 char wide strings
// The First Item in this List is the Header, specifying the
// Customer Details
// The remaining items contain Order Details for the Same Customer
// This Part Works, As is shown by 'DumpToTextFile()'
bool CConfirmOrdersStep1Dlg::CreateViewForPrinting()
{
delete m_pView;
m_pView=new COrderPageList();
int i;
int ItemCount=0;
for(i=0;i<m_clist.getcount();i++){
if(m_clist.getcheck(i)="=1)ItemCount++;
" }
="" if(itemcount="=0)return" false;
="" tag_str_customer*="" pcust="m_pNode-">pCustomer;
COrderPageView* pPageHeader=new COrderPageView(NULL,40);
pPageHeader->m_pCustomer=m_pNode->pCustomer;
pPageHeader->PrintPageHeader();
m_pView->AddReference(pPageHeader);
for(i=0;i<m_clist.getcount();i++){
if(m_clist.getcheck(i)!="1)continue;
" int="" index="((int)m_cList.GetItemData(i))-BIAS;
" lpstr_order="" po="(LPSTR_ORDER)m_pOrderList-">GetDataFromIndex(Index);
COrderPageView* pPageView=new COrderPageView(pO,40);
pPageView->PrintOrderBody();
m_pView->AddReference(pPageView);
}
ifdef _DEBUG
m_pView->DumpToTextFile();
endif
return true;
}
///////////////////////////////////////////////////////////////////////////////
//
// THIS IS WHERE THE RUBBER MEETS THE ROAD!
//
// We Are Committing a Set of Lines of Text in an Incrementing
// List of adjusting Rectangles.
int CConfirmOrdersStep1Dlg::PrintDocFragment(COrderPageView * pView, CDC * pDC)
{
ASSERT(pView);
ASSERT(pDC);
int i;
for(i=0;i<pview->GetItemCount();i++){
RECT DrawingRect;
DrawingRect.left=0;
DrawingRect.top=m_nCurrentPrintRectTop;
m_nCurrentPrintRectTop+=m_nCurrentLineHeight;
DrawingRect.right=m_nCurrentPrintRectWidth;
DrawingRect.bottom=m_nCurrentPrintRectTop-1;
LPCSTR DrawingText=(LPCSTR)pView->GetDataFromIndex(i);
pDC->DrawText(DrawingText,-1,&DrawingRect,DT_LEFT);
}
return i;
}
Getting Desparate at this stage. I Guess I am doing something trivial wrong.
Thanks + Regards,
Bram van Kampen
|
|
|
|
|
Well I just Notice, I forgot to Select the Font into the Printer Device Context.
That would of course have an impact!
Can anyone spot other errors?
Bram van Kampen
|
|
|
|
|
Bram van Kampen wrote: The Binary I get refuses to open. Not sure what that means, or what is missing. If you do not select the font into the DC then it will just use a default, so you should still see some output. Given that this is Chris's original code, you may want to try talking to him.
|
|
|
|
|
Wel,
I get a Binary File not recognised by the XPS Reader.
I can open it as Binary in a HexVieuwer, and get nothing recognisable.
Bram van Kampen
|
|
|
|
|
When I look at an XPS print file it looks something like:
PK µC'I Metadata/Job_PT.xmlíW]s¢H}ߪý)Þ' &Ùh%NÝ4ZÁØ }Cè`|
´¢óë÷¶ã̬‰™™§éòš{Îážþ¸póqGgK–<Mn•ʹ¦œ±ÄOž„·Š3l¸V>6ÿþë&+žƒœ'bÈý9gKŠöÞ*3!²†ªþŒÅ^qs?O‹ôIœûi¬–< Ò²P«šVSµk5"H¾=Øbžr/fešÏ•íªà{Ú²,ÏËÚyš‡’£¢>š÷öõ'…🽠‚·QÊA²ÏÀ¤Ð°žÈ•ŠŸÿJdÎÖ˜FP¨{è*
which is fine, and the XPS reader opens it OK.
As to why you cannot read yours is a total mystery. If you are using all the standard APIs (or their MFC equivalents) to create your print files, then they should at least be readable.
Try changing the extension from .xps to .zip and have a look at the content then.
|
|
|
|
|
Hi, Richard,
Well, if it opens in your machine , there is evidently something wrong with my reader, and nothing wrong with the code I wrote to generate same.
I'm away on a break for a few days. I had my 62nd Birthday 3 weeks ago, but, were unable to escape. We are trying enjoy life in semi retirement, with a small important money spinning business running in the background, powered by my software.
I appreciate your comment, and will try changing the XPS ext to ZIP when I'm back.
I Think that you know that I write software for Laundrettes and Dry-Cleaners, specifically for our own chain.
There are No Trade or Other secrets buried in the Text Example I Sent. It is a Fictitious Customer with a Fictitious ancient Order. (The Bane of a laundrette is people not coming back to pay and collect, It Clogs up the shop, the work is done, but, the Till does not ring. )
The Program I am writing now, is a concerted attempt to attack this, by contacting customers. The First step there is, to ensure that ancient Orders that appear on the database, are still present in the shop. Whereas we Trust our staff, an order could have been given out, without the order being rang in, the staff pocketing the sale, and the Item remaining on the Database. At any rate, we do not want to contact customers over long forgotten items, when we no longer poses them, for whatever reason.
It also gives staff an other activity at quiet Times. Instead of drinking cups of tea, they have a Screen of alternative, and More Productive activity. Making Phone Calls, and finding People's goods. If people's goods can not be found, then there is no point in ringing the customer. Let sleeping dogs lie, and Management writes the item off. We tried it manually, and got things like "Oh I forgot about that, I will collect it next Saturday" Saturday came and went, we all forgot, order still here. This new program will pursue only say 10 old orders at the time, but these orders will be pursued vigorously.
Items Promised to be Collected By a date, will disappear of the ToDo List, but, will re-appear there after that date, if at that stage not collected and paid.
The Interface is being designed so that Junior Staff can handle it, kicking major decisions back upstairs. The Document I tried to print was a form, for staff to go around the shop, and find the goods. The First Step.
When you Opened the XPS File, could you read the Text as Follows:-
Finding Old Orders in the Shop
Printed by <????> on 01 09 16 - 23:40:14
Customer:-
TONY LOUGHLIN
If The Item(s) are Found you should
Update the Computer Record, and
Destroy this Page!
Otherwise Inform Management of the
Discrepancies, by Returning this Page
Marked Up Accordingly!
Order Nr 214058
Received on: 27/07/12 - 13:18:21
Not Confirmed Present since Completion
Qty Description Amount
1 lb Wash +Dry £11.00
========
Total For Order £11.00
Due for This Order £11.00
Packaging:- -1Wb
========================================
<pre>
Actually Found:-
Hangers Pastics Flatpacks WashBags
+-+ +-+ +-+ +-+
| | | | | | | |
| | | | | | | |
| | | | | | | |
+-+ +-+ +-+ +-+
========================================
Please let me know If you Did.
It means in that case: "Programming Problem Solved"
Thanks and Regards,
Bram van Kampen
|
|
|
|
|
Hi Bram, well I'm a few years older than you, so in theory I should have more knowledge locked in my brain. But you know what they say about theory and practice.
When I said I could open the XPS file, I meant one that I generated here. If you can send me the one you generate I could have a look at it for you. Send me a private email and I can give you an address, or put it up on one of the public staging sites.
|
|
|
|
|
I am not sure that you should be calling CView::OnBeginPrinting[^] in your code. That function will be called by the framework when printing is initiated.
|
|
|
|
|
I have a custom rotary slider control inheriting from CSliderCtrl. Its own behaviour is fine, it displays as I want, responds to mouse dragging and rotates just as it should. Then I want to interact with it from its parent dialog and the problems start.
I had imagined that I would just have to trap the ON_VSCROLL message to obtain its value as it changes. (It is set up with Orientation: Vertical though I have tried Horizontal too.) I can't trap ON_VSCROLL or ON_HSCROLL with either setting. I have tried catching NM_RELEASEDCAPTURE and can't get that either. I have:
BEGIN_MESSAGE_MAP(CNewPatchDlg, CDialog)
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_VOLUME, OnReleasedCapture)
END_MESSAGE_MAP()
in my dialog message map and
void CNewPatchDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
void CNewPatchDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
void CGm36NewPatchDlg::OnReleasedCapture(NMHDR* pNMHDR, LRESULT* pResult)
declared and defined in my dialog .h and .cpp files. They exist as temporary function stubs with a little junk code in there to just see whether I can hit breakpoints in the functions. None get hit.
In the dialog editor the wizard shows only four control events available for the control, NM_CUSTOMDRAW, NM_OUTOFMEMORY, NM_RELEASEDCAPTURE, NM_THEMECHANGED. I expected to see more than that! I would have thought at least I could have accessed ON_VSCROLL via the wizard though a standard CSliderCtrl shows the same four options.
I know sometimes these controls can be a little eccentric but I thought this would be pretty plain sailing. I've dug around and found a couple of suggestions that I'm not the only person to have this issue with a Custom slider but no one seems to have a solution, only to use ON_HSCROLL or ON_VSCROLL which I can't seem to do. This must be something very simple I am seeing wrong. Can anyone suggest what I am missing?
|
|
|
|
|
|
I found that Richard, MSDN is the first thing I go to. There is a wealth of stuff with examples (that actually are only part complete) in there. None of it seems to work!
From your link:
Quote: A slider control notifies its parent window of user actions by sending the parent WM_HSCROLL or WM_VSCROLL messages, depending on the orientation of the slider control. To handle these messages, add handlers for the WM_HSCROLL and WM_VSCROLL messages to the parent window. The OnHScroll and OnVScroll member functions will be passed a notification code, the position of the slider, and a pointer to the CSliderCtrl object.
It just seems to back up everything I have done so far. I am trying unsuccessfully to catch the ON_HSCROLL and ON_VSCROLL messages in the parent wiondow just as MS says. I know I can parse out the info once I have caught the message, it's getting the message to land that I can't do even though I seem to be doing exactly what they say.
|
|
|
|
|
AlbertB wrote: MSDN is the first thing I go to Unfortunately so many of our posters do not.
Looking at CDialog, it does not look like it implements the OnXScroll methods, so one wonders whether it just ignores scroll messages. I'm not sure quite how to get round that (years since I did MFC), but in Win32 I would probably subclass the Dialog control and use my own message handler.
|
|
|
|
|
I seem to have at least found a way of doing what I want, even though it does not explain to me what I am really doing wrong here. I have defined a custom message and can send this on to the dialog from within the custom slider at an opportune point. I pass the ID and the value I want as its parameters. I can then catch the custom message in my dialog message handler just as I should be able to and process it from there.
I would still like to know why I can't get to grips with the ON_VSCROLL way of doing this as every link I find tells me I should be able to do this as the standard method. I take your point that CDialog doesn't seem to support OnXScroll, but it's a mystery as to why everyone seems to tell you that is the way to do it. Thanks for your time and efforts Richard, even though we still seem to have only half a picture it's greatly appreciated.
|
|
|
|
|
Interesting,
Can you give us fragments of the Code that resolved it, so that we all may learn?
Regards,
Bram van Kampen
|
|
|
|
|
In stdafx.h:
#define IDM_SLIDER_CHANGE WM_USER+8
In the derived CRotarySlider class at a relevant point in my positioning calculation:
int iID = ::GetDlgCtrlID(::GetFocus());
GetParent()->SendMessage(IDM_SLIDER_CHANGE, iID, nPos);
In the Dialog class that the control is used in:
Added to the message map:
ON_MESSAGE(IDM_SLIDER_CHANGE, OnSliderChange)
And handler function:
LRESULT CGm36NewPatchDlg::OnSliderChange(WPARAM wParam, LPARAM lParam)
{
switch (wParam)
{
case IDC_VOLUME:
{
break;
}
case IDC_GAIN:
{
break;
}
}
LRESULT lr = 0;
return lr;
}
There are still aspects to tidy up which may not be best coding practice but that works at the moment.
|
|
|
|
|