Select Different Printer Without Document/View Framework

I want to have my MFC program to select a printer other than the default 
printer.  I have see several examples on how to do this in the Document/View 
Framework, but that's  not what i need.  Here are the first couple lines of 
the print class that I'm using:

void CMyPrintClass::Print(bool Default) 
{
	m_nPrintPos = NULL;
	CString strPrintString;
	
	m_nPrintPos = GetFirstStrPosition();

	//Do Print Job
	CDC dc;
	CPrintDialog printDlg(FALSE);

	//Get printer settings from user
	if (Default==FALSE)
	{
		if (printDlg.DoModal() == IDCANCEL)	     
		return;
	}					
	if(Default==TRUE)
	{
		printDlg.GetDefaults();
	}


Instead of calling GetDefaults(), I'd just like to specify which defaults to 
get.
0
Rtek (3)
3/4/2005 5:19:04 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
476 Views

Similar Articles

[PageSpeed] 14

You need to step through the MFC print cycle to see what's going on in 
particular the DoPreparePrinting(). It is not a simple process. You will 
have to determine if the printer is installed on the system and then create 
all of the necessary requirement to create a printer dc to pass on to your 
Draw functions

"Rtek" <Rtek@discussions.microsoft.com> wrote in message 
news:9E5AD979-413D-47CB-831B-706AAB8E1095@microsoft.com...
>I want to have my MFC program to select a printer other than the default
> printer.  I have see several examples on how to do this in the 
> Document/View
> Framework, but that's  not what i need.  Here are the first couple lines 
> of
> the print class that I'm using:
>
> void CMyPrintClass::Print(bool Default)
> {
> m_nPrintPos = NULL;
> CString strPrintString;
>
> m_nPrintPos = GetFirstStrPosition();
>
> //Do Print Job
> CDC dc;
> CPrintDialog printDlg(FALSE);
>
> //Get printer settings from user
> if (Default==FALSE)
> {
> if (printDlg.DoModal() == IDCANCEL)
> return;
> }
> if(Default==TRUE)
> {
> printDlg.GetDefaults();
> }
>
>
> Instead of calling GetDefaults(), I'd just like to specify which defaults 
> to
> get. 


0
MAO
3/4/2005 6:10:53 PM
I have no idea how to do that.  Is there an example somewhere that will show 
me an example on how to do this?  Can I send you my code and have you give me 
some help?  Or where can I got help with this?
0
Rtek (3)
3/4/2005 7:07:03 PM
I also don't have a DoPreparePrinting() cause there isn't any default 
functions for Dialog based application without the Document/View Framework.  
I have to use the CPrintDialog to do anything (ie:get printer defaults or 
DoModal() to ask the user what they want to use).  What I want is instead of 
calling:

CPrintDialog printDlg;
printDlg.GetDefaults();

I want to just load the printer that I want.

I've tried using the following function:

bool CEnumPrinters::SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, const 
CString& PrinterName, const CString& PrinterSpooler, const CString& 
PrinterPort)
// hDeMode - Handle to the current DEVMODE structure
// hDevNames - Handle to the current DEVNAMES structure
// PrinterName - E.g. HP LaserJet 4L
// PrinterSpooler - e.g. "winspool"
// PrinterPort - e.g. "LPT1:"
{
	// we only update the existing hDevMode and hDevNames objects if we can 
successfgully setup the
	// new hDevMode and hDevNames objects
	HANDLE	local_hDevMode = INVALID_HANDLE_VALUE ;
	HANDLE	local_hDevNames = INVALID_HANDLE_VALUE ;
#ifdef _DEBUG
	DumpHandles(hDevMode, hDevNames) ;
#endif

	// To setup the new local_hDevMode object we need to open the printer name 
to get the information
	HANDLE	hPrinter;
	char	*pPrinter = new char[PrinterName.GetLength() + 1] ;
	ASSERT(pPrinter) ;
	strcpy(pPrinter, PrinterName) ;
	if (!OpenPrinter(pPrinter, &hPrinter, NULL))
		{
		delete []pPrinter ;
		return false ;
		}

	// A zero for last param returns the size of buffer needed for the 
information to be returned
	int nSize = DocumentProperties(NULL, hPrinter, pPrinter, NULL, NULL, 0);
	ASSERT(nSize >= 0);
	local_hDevMode = ::GlobalAlloc(GHND, nSize) ;							// allocate on heap
	LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock(local_hDevMode);		// lock it

	// Fill in the rest of the structure.
	if (DocumentProperties(NULL, hPrinter, pPrinter, lpDevMode, NULL, 
DM_OUT_BUFFER) != IDOK)
		{
		// failed to read printer properties, abort
		ASSERT(::GlobalFlags(local_hDevMode) != GMEM_INVALID_HANDLE);
		UINT nCount = ::GlobalFlags(local_hDevMode) & GMEM_LOCKCOUNT;
		while (nCount--)
			::GlobalUnlock(local_hDevMode);

		// finally, really free the handle
		::GlobalFree(local_hDevMode);
		local_hDevMode = NULL;
		ClosePrinter(hPrinter);
		delete []pPrinter ;
		return false ;
		}
	// finsihed interrogating for DEVMODE structure
	::GlobalUnlock(local_hDevMode) ;
	ClosePrinter(hPrinter);
	delete []pPrinter ;

	// we need to allocate a new DEVNAMES object on the global heap
	// we also need the size to include the strings PrinterName, PrinterSpooler 
and PrinterPort
	// Layout is:
	// DEVNAMES structure
	// PrinterSpooler\0
	// PrinterName\0
	// PrinterPort\0
	int	size = sizeof(DEVNAMES) + PrinterName.GetLength() + 1 + 
PrinterSpooler.GetLength() + 1 + PrinterPort.GetLength() + 1 ;
	local_hDevNames = ::GlobalAlloc(GHND, size) ;								// allocate on heap
	LPDEVNAMES	pNewDevNames = (LPDEVNAMES)::GlobalLock(local_hDevNames) ;	// 
lock it
	memset(pNewDevNames, 0, size) ;											// init to 0
	
	// add the 3 strings to the end of the structure
	strcpy((char*)((char*)pNewDevNames + sizeof(DEVNAMES)), PrinterSpooler) ;
	pNewDevNames->wDriverOffset = sizeof(DEVNAMES) ;
	strcpy((char*)((char*)pNewDevNames + sizeof(DEVNAMES)) + 
PrinterSpooler.GetLength() + 1, PrinterName) ;
	pNewDevNames->wDeviceOffset = sizeof(DEVNAMES) + PrinterSpooler.GetLength() 
+ 1 ;
	strcpy((char*)((char*)pNewDevNames + sizeof(DEVNAMES)) + 
PrinterSpooler.GetLength() + 1 + PrinterName.GetLength() + 1, PrinterPort) ;
	pNewDevNames->wOutputOffset = sizeof(DEVNAMES) + PrinterSpooler.GetLength() 
+ 1 + PrinterName.GetLength() + 1 ;
	//pNewDevNames->wDefault = lpDevNames->wDefault ;
	::GlobalUnlock(local_hDevNames) ;											// free it
#ifdef _DEBUG
	DumpHandles(local_hDevMode, local_hDevNames) ;
#endif
	// now update the handles that were passed in
	// free the existing handles if they exist first
	if (hDevMode != NULL && hDevMode != INVALID_HANDLE_VALUE)
		{
		ASSERT(::GlobalFlags(hDevMode) != GMEM_INVALID_HANDLE);
		UINT nCount = ::GlobalFlags(hDevMode) & GMEM_LOCKCOUNT;
		while (nCount--)
			::GlobalUnlock(hDevMode);

		// finally, really free the handle
		::GlobalFree(hDevMode);
		hDevMode = INVALID_HANDLE_VALUE ;
		}
	if (hDevNames != NULL && hDevNames != INVALID_HANDLE_VALUE)
		{
		ASSERT(::GlobalFlags(hDevNames) != GMEM_INVALID_HANDLE);
		UINT nCount = ::GlobalFlags(hDevNames) & GMEM_LOCKCOUNT;
		while (nCount--)
			::GlobalUnlock(hDevNames);

		// finally, really free the handle
		::GlobalFree(hDevNames);
		hDevNames = INVALID_HANDLE_VALUE ;
		}
	hDevMode = local_hDevMode ;
	hDevNames = local_hDevNames ;

#ifdef _DEBUG
	DumpHandles(hDevMode, hDevNames) ;
#endif
	return true ;			// success!
}
///************************END*************


But this function doesn't change the printer system wide, and it appears to 
me that when i call printDlg.GetDefaults() I looks at the system wide 
defaults.  Am I making any sense?
0
Rtek (3)
3/4/2005 8:21:04 PM
Reply:

Similar Artilces:

Importing CSV
Hi All, I have to import CSV files into excel. A regular issue with this is that often the CSV files will contain data (in this case account codes) that excel interprets as dates (such as '3-8000' being converted to 1 Mar 8000). Nothing unusual so far, in that we handle that issue by importing manually (External Data - Text import), and specifying that the field in question is imported as TEXT not GENERAL. However, I have a file that, when I double click on it, it opens up perfectly (except with dates where it should be text as outlined above), but if I manually import that f...

CFileDialog hangs on "My Documents" click
Platform: Windows 2000 Internet Explorer: 6.0 All patches applied from Microsoft. The following code works fine in our multithread applications, except one thing. When the user clicks on the "My Documents" icon, the program hangs. The rest of the icons work fine. We have tried different machines and have gotten the same results. If we try the application on a Windows XP platform, the error goes away. Anybody got an idea on what the problem might be? static char BASED_CODE szFilter[] = "Ascot 5 Exercise Files (*.aef)|*.aef|Ascot 4 Exercise Files (*.exr)|*.exr|All Fil...

Toggle Button Selection
Hi there I am creating a questionnaire in access for children with disabilities and I am trying to make it as friendly as possible. I am making it so with some questions there are three options for a choice, and they will simply be a sad face, an ok face and a happy face. Somone made the suggestion of putting each of the images over a toggle button so when someone click the image they would actually e clicking the button. I figure this will mean for each question having to have three fields in a table, but I can cope with that. However, I am aware then that it could be possible for someon...

Personal folder size showing different
Hello all, Pls help me. I had a question. Why is outlook personal folder size showing 2706KB but in Windows explorer it shows only 2500KB... why does it show different and not the same... appreciate reply asap. thanks ...

Word 2007 show combined revisions in one document
I combined several sets of revisions in one document and noticed a peculiarity: when a word was replaced without deleting the space after the word and inserting a new space, the deleted word would show up twice in the combined-revisions document -- once before the inserted word and once after. So it looked like this: (replacing "dogs" with "cats" -- I'm USING caps to show the deleted word) I like DOGScatsDOGS. Where the space had been left in, it looked correct: I like DOGS cats. There was no problem in this respect with the individual redlined do...

Trouble sending word document to PC
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel I am trying to send word documents to a PC and it opens as a zip drive for the PC and is unreadable. I am using Microsoft word for mac and saving the file as a: Word Document (.docx). What can I do to make the file compatible? The problem is the system and/or the user at the other end. It could be either of several things but without knowing what their software version is or how it's being delivered it's hard to say exactly what they're doing wrong. The OXML (.docx) file is basically a zipped form...

Printer Selection
I have 2 printers tied to the RMS computer. One is the Epson T88iii and the other is an HP Officejet. When I back order, create a workorder, lay-a-way, etc, I want the print out full page on the HP. How do I set this up? This is a multi-part message in MIME format. ------=_NextPart_000_0077_01C6D670.BFB550D0 Content-Type: text/plain; charset="Utf-8" Content-Transfer-Encoding: quoted-printable Bill, SO Manager | Database | Registers | Receipt Format | select the format = you're using for your Epson | delete the formats for workorder, layaway, = etc with the red X SO Ma...

Issues Printing Bubble Chart on Canon Printer
We just installed a new Canon printer and when we printed Excel Bubble Charts on the printer, they are rendered as squares, with inscribed circles. The bubbles are formatted to be shaded, so the squares are shaded. Has anyone had this problem and what did you do to fix it? We PDF'd the document and found that the printing problem has disappeared in the PDF'd version. Now we're missing a font on the computer that has the Adobe Acrobat application. If it's not one thing, it's another. I'm still interested in understanding why there's a problem however. "...

Questions on format of Select statement
SELECT tblCalls.ColTime, tblCalls.colProblem, tblCalls.colResolution from tblCalls, in dbCallLogs WHERE tblCalls.colResolution from tblCalls, in dbCallLogs where tblCalls.ColTime >= #12/1/2006# <= #3/2/2007# ORDER BY tblCalls.ColTime DESC; I want to know if this is right? I notice in some queries the table name before the column name is missing? Also when do you have to put in the database name? Is it the same in Access as SQL Server? I want the most recent first. I want those records betweeb 12.1.06 and 3.01.07. thanks, On Wed, 21 Mar 2007 13:30:07 -0700, Janis <Janis...

List views
I have a list view in which I have selected grid lines in report mode. The grid lines show ok until I scroll down and then the horizontal line fall out of step colliding with the text. Does anyone know what is happening here and how to fix it? >I have a list view in which I have selected grid lines in report mode. The >grid lines show ok until I scroll down and then the horizontal line fall out >of step colliding with the text. > >Does anyone know what is happening here and how to fix it? Sadly, the fix is to switch off the grid lines :( PSS ID Number: 813791 "B...

Document not Saved error
Occasionally, a user will get the error message, Document not Saved, when trying to save a spreadsheet. Save as, saving to a different drive results in the same message. To recover, you can Copy the whole sheet, exit then go back into Excel, then past, then save it. We use Excel 97. Does anyone know why this is happening? When you save a file, excel will save it as a temporary file (8 characters, no extension). Then if that save is successful, excel will delete the original and rename this funny named file to the original's name. You see the results when something interferes wi...

selecting records
Hi I have a database with over 200 records containing several names with various times taken recorded against each record. I want to be able to sort, then select the best perfroming (fastest) and compile into a report (by name) preferably via Macro. I can do this manually but it will need doing on a month to month basis so want to automate, how canI get excel to select the fastest times from a range and report in another worksheet -- thanks Roy Hi, You don't give enough detail but if you want the n smallest(fastest) times from a ranges of times then assuming the ...

How to restore the line of: File, Edit, View, in excel
My excel 2003 now does not show the top line with the options: File, edit, View, Insert, Format, Tools, Chart, Windows, Help Dont find how to retore it, if press F11, they appear in a chart that automatically is add to workbook, but other sheets will not show it. Without this cannot use excel. Please advise how to restore it. alt/t>customize>toolbars>check worksheet menu bar> -- Don Guillett SalesAid Software dguillett1@austin.rr.com "RPM55" <RPM55@discussions.microsoft.com> wrote in message news:CF511981-107E-4B50-A06B-0A7F6ACC9606@microsoft.com... > My ...

Cell Drag & Drop without Incrementing
Hi, Simple question really. Is it possibe to drag a cell without it incrementing automatically. Eg if cell a1 contains TR321 I want to be able to drag it down to fill cells a2:a10 without any change, ie A10 is also TR321. Cheers, thanks for your help. Paul Hi Paul use an absolute reference. change the cell reference TR321 to $T$321 also have a look at http://www.cpearson.com/excel/relative.htm -- Regards Frank Kabel Frankfurt, Germany Paul wrote: > Hi, > > Simple question really. Is it possibe to drag a cell > without it incrementing automatically. Eg if cell a1 > co...

Printing legal-paper documents
I am unable to print legal-paper sized documents. All other print jobs work fine. Attempted switching the drivers, though printing to any available printer produces the same problem. Other systems aren't running into this problem on the same printers. It isn't an issue with the type of paper feed, in fact, it doesn't seem to even get beyond the program that is trying to send the print job. In addition, there are no errors produced. I'd appreciate any advice, especially if you could email it to me. Thanks, Nathan Sones natesones@yahoo.com ...

Improved Error Handling Messages/Supporting documentation
The level of error handling messages within CRM 3.0 is mostly at a summary level and means that troubleshooting and investigation timeframes becomes unnecessarily lengthened. For example in trying to import campaign responses the error message under Activities\Bulk Import\Failures\"Data in row did not match the import type" 0x8004032d Unfortunately this error message does not indicate which field is erroring out on so trying to troubleshoot somethign which in principle appears fairly simple becomes an onerous task - and is hit/miss in terms of success. Is it possible to prov...

Best way to view demographic data?
I've just received a spreadsheet with 3 columns and about 150 rows. The data includes Gender, Age & Marital Status, which really is a lot of info. Is there an easy (or not easy) way to view the data in relevant terms? For example, how many males / females between the age of 35 - 45 are single, or divorced, or widowed, etc... I can think of at least a 1/2 dozen ways I'd like to view the data. Certainly there's a better way than sorting it each time for a different view? Thanks, David... Seems to me, this is the perfect reason to use charts. ******************* ~A...

User selects file location
I have a macro that formats data and saves certain portions of a worksheet into a csv file. The macro prompts the user for a path to save the file to. Instead of the user typing in the path, is there a way to have them select it somehow...similar to what happens when you select File - Save As from the main Excel menu? I appreciate your feedback. Monica a drop down box perhaps. http://www.contextures.com/tiptech.html -- Don Guillett SalesAid Software donaldb@281.com "Monica" <anonymous@discussions.microsoft.com> wrote in message news:941e01c4336e$c3b64ce0$a1012...

kill all documents
How to kill all documents (active or inactive) when an mdi application is closed? Thanks. Jess I am assuming you want to close all documents when the application closes. But thats exactly what happens by default. It the document has changed the user gets a choice to close the document by default. What exactly do you want to do. -------- Ajay Kalra ajaykalra@yahoo.com You can call SaveAllModified() any time you'd like. I also wrote this code to loop through open documents that might be interesting to you: POSITION pos = GetFirstDocTemplatePosition(); while (pos) { CDocTempla...

How to add document found to hyperlink url
I have a hyperlink "url" which points to where internet document files are stored. However, I don't know the name of the actual documents until I get to the path and then click on the document that I want. How do I code to pick up the document name and "ADD" it to the url path so that the next time someone clicks on the hyperlink it takes them directly to the document and opens the document? Any suggestions or help appreciated. Thanks, Preston -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-formscoding/200706/1 Check ...

Microsoft Framework X library
When trying to open any program in Office X, I get an error statement that "An unexpected error occurred while trying to load the Microsoft Framework X library." The program seems to work fine, but it's a tad annoying. The only clue I have is that I loaded Office as Root and am trying to use it under another account, which does have system administrator privileges. When logged in as root, I don't get the error statement. Anyone have any ideas? On 10/4/03 7:09 PM, in article 61c45105.0310041809.2d74c552@posting.google.com, "Chris" <christofteberg@alaska.net> w...

2 users access calendar online with different rights
hi user A wants to give user B permission to see, add and edit entries in calendar online http://www.exchangedomain.ch/usersname/kalender/ user B is able to see, but not to add or edit entries at the moment in outlook itself its working. user B can open the folder calendar of user A and make entries and change entries what have to be done that this is possible also with web access exchange ? thankx mike schwarz On Thu, 26 Oct 2006 14:09:29 +0200, "Mike Schwarz" <ctek@ctek.ch> wrote: >hi > >user A wants to give user B permission to see, add and edit entries in &...

Printing document references
Hello, We have a number of forms which we have produced here at work which require numbered references on them so each document is unique. Can this be batch driven ? EG. I have designed a printed sheet in Excel for our stock take this year. We need 200 sheets to be issued out to the guys doing the counting. Each sheet needs its own unique documents reference number on it so it can be traced. How can I automate this so I can print 200 copies of the document, and the document references increase incrementally on each separate page. EG. ABC0001, ABC0002, ABC0003... I dont want to have t...

Making the default printer, the true default
I have an HP that is set in XP as the default printer; I also often save a doc as a PDF, which means using the print function and selecting CutePDF as my "printer." Is there any way of getting XP to recognize the HP as my default printer every time? I find that after selecting CutePDF to "print" a page (in reality, it doesn't print, but just gets saved as a PDF), that the next time I do a quick print in MS Word, that Windows thinks I want to save as PDF again, and I have to click out of that dialog and find the HP printer. How can I get Windows to always thin...

How to get the selected video codec ? #3
Hello, in my webcam program I open the compression dialog from the webcam driver to select the codec for the video recording! Then I record with capFileSetCaptureFile(window->GetHWND(), filename); ! OK ! The video is generated with the selected codec. If I start the program again. The previous selected codec is lost and a default codec is set. To prevent this I want to get the selected codec, save it and at next start select the codec by program. But I don't know how to find out which codec was set in the driver based dialog. I would be very glad if someone could help me! Regar...