有谁能解决?用程序修改了默人打印设置后,当前程序仍读不出来这个新的设置!

zyjnb 2003-09-28 10:28:49
有谁能解决?用程序修改了默人打印设置后,当前程序仍读不出来这个新的设置!
以下是我的部分代码:
BOOL CCoRP::DoPrinterSetup(HWND ParenthWnd)
{
PRINTDLG pd;


// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(PRINTDLG));
pd.lStructSize = sizeof(PRINTDLG);
pd.hwndOwner = ParenthWnd;
pd.hDevMode = NULL; // Don't forget to free or store hDevMode
pd.hDevNames = NULL; // Don't forget to free or store hDevNames
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE;
pd.nCopies = 1;
pd.nFromPage = 0xFFFF;
pd.nToPage = 0xFFFF;
pd.nMinPage = 1;
pd.nMaxPage = 0xFFFF;

if (PrintDlg(&pd)==TRUE)
{
// Lock memory handle.
DEVMODE FAR* pDevMode =
(DEVMODE FAR*)::GlobalLock(pd.hDevMode);
LPDEVNAMES lpDevNames;
LPTSTR lpszDriverName, lpszDeviceName, lpszPortName;
//HANDLE hPrinter;


if (pDevMode)
{
// Unlock memory handle.
lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
lpszDriverName = (LPTSTR )lpDevNames + lpDevNames->wDriverOffset;
lpszDeviceName = (LPTSTR )lpDevNames + lpDevNames->wDeviceOffset;
lpszPortName = (LPTSTR )lpDevNames + lpDevNames->wOutputOffset;

//m_hDevMode=pd.hDevMode;

DWORD nBufferSize=255;
CString szPrinterName;
LPTSTR lpszPrinterName=szPrinterName.GetBufferSetLength(nBufferSize);
BOOL bRet = RPGetDefaultPrinter(lpszPrinterName, &nBufferSize);

RPSetPrinter(ParenthWnd,lpszDeviceName,pDevMode);

if (szPrinterName.CompareNoCase(CString(lpszDeviceName))!=0)
RPSetDefaultPrinter(lpszDeviceName);

//RPSetPrinter(ParenthWnd,lpszDeviceName,pDevMode);
//SetPrinterDeviceDefaults(pd.hDevNames,pd.hDevMode);
/*
::OpenPrinter(lpszDeviceName, &hPrinter, NULL);
::DocumentProperties(NULL,hPrinter,lpszDeviceName,pDevMode,
pDevMode, DM_IN_BUFFER|DM_OUT_BUFFER);

// Sync the pDevMode.
// See SDK help for DocumentProperties for more info.

::ClosePrinter(hPrinter);*/
::GlobalUnlock(m_hDevNames);
::GlobalUnlock(m_hDevMode);
}

// GDI calls to render output.
return (TRUE);
}
return (FALSE);
}


// RPSetPrinter
//
// Demonstrates how to use the SetPrinter API. This particular function changes the orienation
// for the printer specified in pPrinterName to the orientation specified in dmOrientation.
//
// Valid values for dmOrientation are:
// DMORIENT_PORTRAIT (1)
// or
// DMORIENT_LANDSCAPE (2)
BOOL CCoRP::RPSetPrinter(HWND ParenthWnd, LPTSTR pPrinterName, DEVMODE *pDevModeSet)
{
HANDLE hPrinter = NULL;
DWORD dwNeeded = 0;
PRINTER_INFO_2 *pi2 = NULL;
DEVMODE *pDevMode = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;

// Open printer handle (on Windows NT, you need full-access because you
// will eventually use SetPrinter)...
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
bFlag = OpenPrinter(pPrinterName, &hPrinter, &pd);
if (!bFlag || (hPrinter == NULL))
return FALSE;

// The first GetPrinter tells you how big the buffer should be in
// order to hold all of PRINTER_INFO_2. Note that this should fail with
// ERROR_INSUFFICIENT_BUFFER. If GetPrinter fails for any other reason
// or dwNeeded isn't set for some reason, then there is a problem...
SetLastError(0);
bFlag = GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if ((!bFlag) && (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
(dwNeeded == 0))
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
pi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (pi2 == NULL)
{
ClosePrinter(hPrinter);
return FALSE;
}

// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
if (pi2->pDevMode == NULL)
{
dwNeeded = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
NULL, NULL, 0);
if (dwNeeded <= 0)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

pDevMode = (DEVMODE *)GlobalAlloc(GPTR, dwNeeded);
if (pDevMode == NULL)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

lFlag = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
pDevMode, NULL,
DM_OUT_BUFFER);
if (lFlag != IDOK || pDevMode == NULL)
{
GlobalFree(pDevMode);
GlobalFree(pi2);
ClosePrinter(hPrinter);
return FALSE;
}

pi2->pDevMode = pDevMode;
}

// Driver is reporting that it doesn't support this change...
if (!(pi2->pDevMode->dmFields & DM_ORIENTATION))
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Specify exactly what we are attempting to change...
//pi2->pDevMode->dmFields = DM_ORIENTATION;
//pi2->pDevMode->dmOrientation = dmOrientation;

//pi2->pDevMode = pDevModeSet;
memcpy(pi2->pDevMode,pDevModeSet,
(size_t)::GlobalSize(pDevModeSet));

// Do not attempt to set security descriptor...
pi2->pSecurityDescriptor = NULL;

// Make sure the driver-dependent part of devmode is updated...
lFlag = DocumentProperties(ParenthWnd, hPrinter,
pPrinterName,
pi2->pDevMode, pi2->pDevMode,
DM_IN_BUFFER | DM_OUT_BUFFER);
if (lFlag != IDOK)
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Update printer information...
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pi2, 0);
if (!bFlag)
// The driver doesn't support, or it is unable to make the change...
{
GlobalFree(pi2);
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
return FALSE;
}

// Tell other apps that there was a change...
SendMessageTimeout(HWND_BROADCAST, WM_DEVMODECHANGE, 0L,
(LPARAM)(LPCSTR)pPrinterName,
SMTO_NORMAL, 1000, NULL);

// Clean up...

if (hPrinter)
ClosePrinter(hPrinter);
if (pDevMode)
GlobalFree(pDevMode);
if (pi2)
GlobalFree(pi2);

return TRUE;
}
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyjnb 2003-09-28
  • 打赏
  • 举报
回复
最后一部分:

// CoRP.cpp: implementation of the CCoRP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CoRP.h"

const size_t BUFSIZE = 255;
static TCHAR g_szBuf[BUFSIZE];

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCoRP::CCoRP()
{

}

CCoRP::~CCoRP()
{

}

// Size of internal buffer used to hold "printername,drivername,portname"
// string. Increasing this may be necessary for huge strings.
#define MAXBUFFERSIZE 250

/*----------------------------------------------------------------*/
/* RPGetDefaultPrinter */
/* */
/* Parameters: */
/* pPrinterName: Buffer alloc'd by caller to hold printer name. */
/* pdwBufferSize: On input, ptr to size of pPrinterName. */
/* On output, min required size of pPrinterName. */
/* */
/* NOTE: You must include enough space for the NULL terminator! */
/* */
/* Returns: TRUE for success, FALSE for failure. */
/*----------------------------------------------------------------*/
BOOL CCoRP::RPGetDefaultPrinter(LPTSTR pPrinterName, LPDWORD pdwBufferSize)
{
BOOL bFlag;
OSVERSIONINFO osv;
TCHAR cBuffer[MAXBUFFERSIZE];
PRINTER_INFO_2 *ppi2 = NULL;
DWORD dwNeeded = 0;
DWORD dwReturned = 0;

// What version of Windows are you running?
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);

// If Windows 95 or 98, use EnumPrinters...
if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
// The first EnumPrinters() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 4th
// parameter) was not filled in. You don't want it filled in here...
EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, NULL, 0, &dwNeeded, &dwReturned);
if (dwNeeded == 0)
return FALSE;

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
return FALSE;

// The second EnumPrinters() will fill in all the current information...
bFlag = EnumPrinters(PRINTER_ENUM_DEFAULT, NULL, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded, &dwReturned);
if (!bFlag)
{
GlobalFree(ppi2);
return FALSE;
}

// If given buffer too small, set required size and fail...
if ((DWORD)lstrlen(ppi2->pPrinterName) >= *pdwBufferSize)
{
*pdwBufferSize = (DWORD)lstrlen(ppi2->pPrinterName) + 1;
GlobalFree(ppi2);
return FALSE;
}

// Copy printer name into passed-in buffer...
lstrcpy(pPrinterName, ppi2->pPrinterName);

// Set buffer size parameter to min required buffer size...
*pdwBufferSize = (DWORD)lstrlen(ppi2->pPrinterName) + 1;
}

// If Windows NT, use the GetDefaultPrinter API for Windows 2000,
// or GetProfileString for version 4.0 and earlier...
else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#if(WINVER >= 0x0500)
if (osv.dwMajorVersion >= 5) // Windows 2000 or later
{
bFlag = GetDefaultPrinter(pPrinterName, pdwBufferSize);
if (!bFlag)
return FALSE;
}

else // NT4.0 or earlier
#endif
{
// Retrieve the default string from Win.ini (the registry).
// String will be in form "printername,drivername,portname".
if (GetProfileString("windows", "device", ",,,", cBuffer, MAXBUFFERSIZE) <= 0)
return FALSE;

// Printer name precedes first "," character...
strtok(cBuffer, ",");

// If given buffer too small, set required size and fail...
if ((DWORD)lstrlen(cBuffer) >= *pdwBufferSize)
{
*pdwBufferSize = (DWORD)lstrlen(cBuffer) + 1;
return FALSE;
}

// Copy printer name into passed-in buffer...
lstrcpy(pPrinterName, cBuffer);

// Set buffer size parameter to min required buffer size...
*pdwBufferSize = (DWORD)lstrlen(cBuffer) + 1;
}
}

// Cleanup...
if (ppi2)
GlobalFree(ppi2);

return TRUE;
}

#undef MAXBUFFERSIZE






zyjnb 2003-09-28
  • 打赏
  • 举报
回复
这是另一部分代码

/*-----------------------------------------------------------------*/
/* RPSetDefaultPrinter */
/* */
/* Parameters: */
/* pPrinterName: Valid name of existing printer to make default. */
/* */
/* Returns: TRUE for success, FALSE for failure. */
/*-----------------------------------------------------------------*/
BOOL CCoRP::RPSetDefaultPrinter(LPTSTR pPrinterName)
{
BOOL bFlag;
OSVERSIONINFO osv;
DWORD dwNeeded = 0;
HANDLE hPrinter = NULL;
PRINTER_INFO_2 *ppi2 = NULL;
LPTSTR pBuffer = NULL;
LONG lResult;

// What version of Windows are you running?
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);

if (!pPrinterName)
return FALSE;

// If Windows 95 or 98, use SetPrinter...
if (osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
// Open this printer so you can get information about it...
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if (!bFlag || !hPrinter)
return FALSE;

// The first GetPrinter() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 3rd
// parameter) was not filled in. You don't want it filled in here...
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if (dwNeeded == 0)
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
ClosePrinter(hPrinter);
return FALSE;
}

// The second GetPrinter() will fill in all the current information
// so that all you need to do is modify what you're interested in...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Set default printer attribute for this printer...
ppi2->Attributes |= PRINTER_ATTRIBUTE_DEFAULT;
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)ppi2, 0);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Tell all open applications that this change occurred.
// Allow each application 1 second to handle this message.
lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L,
(LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);
}

// If Windows NT, use the SetDefaultPrinter API for Windows 2000,
// or WriteProfileString for version 4.0 and earlier...
else if (osv.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#if(WINVER >= 0x0500)
if (osv.dwMajorVersion >= 5) // Windows 2000 or later...
{
bFlag = SetDefaultPrinter(pPrinterName);
if (!bFlag)
return FALSE;
}

else // NT4.0 or earlier...
#endif
{
// Open this printer so you can get information about it...
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if (!bFlag || !hPrinter)
return FALSE;

// The first GetPrinter() tells you how big our buffer should
// be in order to hold ALL of PRINTER_INFO_2. Note that this will
// usually return FALSE. This only means that the buffer (the 3rd
// parameter) was not filled in. You don't want it filled in here...
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if (dwNeeded == 0)
{
ClosePrinter(hPrinter);
return FALSE;
}

// Allocate enough space for PRINTER_INFO_2...
ppi2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if (!ppi2)
{
ClosePrinter(hPrinter);
return FALSE;
}

// The second GetPrinter() fills in all the current<BR/>
// information...
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)ppi2, dwNeeded, &dwNeeded);
if ((!bFlag) || (!ppi2->pDriverName) || (!ppi2->pPortName))
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Allocate buffer big enough for concatenated string.
// String will be in form "printername,drivername,portname"...
pBuffer = (LPTSTR)GlobalAlloc(GPTR,
lstrlen(pPrinterName) +
lstrlen(ppi2->pDriverName) +
lstrlen(ppi2->pPortName) + 3);
if (!pBuffer)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
return FALSE;
}

// Build string in form "printername,drivername,portname"...
lstrcpy(pBuffer, pPrinterName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, ppi2->pDriverName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, ppi2->pPortName);

// Set the default printer in Win.ini and registry...
bFlag = WriteProfileString("windows", "device", pBuffer);
if (!bFlag)
{
ClosePrinter(hPrinter);
GlobalFree(ppi2);
GlobalFree(pBuffer);
return FALSE;
}
}

// Tell all open applications that this change occurred.
// Allow each app 1 second to handle this message.
lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L,
SMTO_NORMAL, 1000, NULL);
}

// Cleanup...
if (hPrinter)
ClosePrinter(hPrinter);
if (ppi2)
GlobalFree(ppi2);
if (pBuffer)
GlobalFree(pBuffer);

return TRUE;
}
ac1998 2003-09-28
  • 打赏
  • 举报
回复



up.

up.
同步电机与构网型变流器的频率稳定性研究(Simulink仿真、Matlab代码)内容概要:本文围绕“同步电机与构网型变流器的频率稳定性研究”展开,结合Simulink仿真与Matlab代码实现,深入探讨了同步电机与构网型变流器在电力系统频率稳定中的动态交互机制。研究重点包括构网型变流器(Grid-Forming Inverter, GFM)如何模拟同步电机的惯量与阻尼特性,提升低惯量系统的稳定性,并通过仿真模型对比分析两者在频率响应、暂态稳定性及系统惯量支撑方面的性能差异。文中提供了完整的建模仿真代码,涵盖控制策略设计、小信号建模、稳定性分析等内容,适用于科研复现与教学实践。; 适合人群:具备电力系统分析、自动控制理论基础,熟悉Matlab/Simulink仿真环境,从事能源并网、电力电子与电网稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于理解构网型变流器如何替代或补充传统同步电机的频率调节功能;②支撑科研论文复现、课程项目设计或实际工程中对高比例能源系统稳定性的分析与优化;③掌握Simulink搭建电力系统动态模型与Matlab进行稳定性分析的核心方法。; 阅读建议:建议读者结合文中提供的仿真模型与代码逐步操作,重点关注控制参数设置对系统稳定性的影响,同时参考相关文献深化对构网控制策略的理解,以实现从理论到仿真的完整闭环。
【复现】并_离网风光互补制氢合成氨系统容量-调度优化分析【Cplex求解】(Matlab代码实现)内容概要:本文围绕“并_离网风光互补制氢合成氨系统”的容量配置与调度优化问题展开研究,利用Cplex求解器在Matlab环境中实现优化模型的构建与求解。研究整合风能、光伏等可再生能源,通过电解水制氢并进一步合成氨,形成绿色能源转化与储存路径。重点解决系统在并网与离网两种运行模式下的容量规划与运行调度协同优化问题,考虑风光出力不确定性、设备效率、能量平衡及经济性等多重约束,旨在提升系统能源利用率与经济性。文中提供了完整的Matlab代码实现,便于科研人员复现和改进相关模型。; 适合人群:具备一定电力系统、可再生能源或优化建模背景的研究生、科研人员及工程技术人员,熟悉Matlab编程与数学优化方法者更佳。; 使用场景及目标:①用于风光储氢氨多能互补系统的容量规划与运行调度研究;②支撑科研论文复现、毕业课题设计或实际项目前期仿真分析;③掌握Cplex在能源系统优化中的应用方法,提升复杂系统建模能力。; 阅读建议:建议结合文中提供的Matlab代码与优化模型公式同步阅读,重点关注目标函数构建、约束条件设置及Cplex调用流程,可尝试修改参数或引入约束进行扩展研究,以深化对多能系统协同优化的理解。
内容概要:本文介绍了一个基于Python开发的“PDF产品彩页批量转换成图片并插入Word”的多线程工具,能够高效地将指定文件夹内的多个PDF文件批量转换为高清图像,并自动插入到一个Word文档中,生成图文并茂的产品彩页合集。该工具采用PyMuPDF(fitz)、Pillow、python-docx等库实现PDF转图与文档生成,利用ThreadPoolExecutor实现智能多线程处理,根据CPU核心数和PDF数量动态分配线程,提升处理效率。界面使用Tkinter构建,支持选择PDF文件夹、设置输出路径、选择图像质量(标准/高/超清),并具备日志记录、进度条显示、错误提示等功能。转换后的Word文档无任何页码或页眉页脚干扰,包含自动生成的目录和处理摘要,图片自适应页面宽度,排版整洁美观。; 适合人群:具备一定Python编程基础的技术人员、办公自动化需求者、需要批量处理PDF与Word文档的企业用户或个体工作者;尤其适用于市场、设计、行政等岗位人员。; 使用场景及目标:①将多个产品手册PDF整合为一份可编辑的Word图文文档;②实现办公流程自动化,提高大批量文件处理效率;③避免手动截图粘贴的繁琐操作,确保输出格式统一、清晰无水印;④利用多线程技术缩短处理时间,提升用户体验。; 阅读建议:使用前需安装所需依赖库(PyMuPDF、Pillow、python-docx),建议在性能较好的设备上运行以充分发挥多线程优势;可通过修改源码进一步定制输出样式或增加功能(如水印添加、格式导出等),适合用于学习GUI编程、多线程应用及文档自动化处理的实践项目。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧