64,687
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
HWND SureCmd;
HWND CancelCmd;
LONG PrintDlgBottom;
LONG SureCmdY;
LONG CancelCmdY;
UINT CALLBACK PrintHookProc(HWND hdlg,UINT uiMsg,WPARAM wParam,LPARAM lParam)
{
switch(uiMsg)
{
case WM_PAINT:
// 修改窗口大小
RECT rc;
ZeroMemory(&rc,sizeof(rc));
GetWindowRect(hdlg,&rc);
if(PrintDlgBottom == 0)
{
rc.bottom += 200;
PrintDlgBottom = rc.bottom;
SetWindowPos(
hdlg,
0,0,0,
rc.right - rc.left,
rc.bottom - rc.top,
// SWP_NOMOVE不移动,X,Y参数被忽略
// SWP_NOZORDER不改变Z序列,hWndInsertAfter参数被忽略
SWP_NOMOVE | SWP_NOZORDER);
}
// 获取确认和取消按钮句柄
if((SureCmd == 0) || (CancelCmd == 0))
{
HWND NowWnd ;
WCHAR WindowCaption[256] = {0};
WCHAR SureCmdCap[256] = L"确定";
WCHAR CancelCmdCap[256] = L"取消";
while(wcscmp(WindowCaption,SureCmdCap) != 0)
{
NowWnd= FindWindowEx(hdlg,NowWnd,L"Button",0);
GetWindowText(NowWnd,WindowCaption,256);
}
SureCmd = NowWnd;
NowWnd = 0;
while(wcscmp(WindowCaption,CancelCmdCap) != 0)
{
NowWnd= FindWindowEx(hdlg,NowWnd,L"Button",0);
GetWindowText(NowWnd,WindowCaption,256);
}
CancelCmd = NowWnd;
}
// 修改确认和取消按钮位置
ZeroMemory(&rc,sizeof(rc));
GetWindowRect(SureCmd,&rc);
if(SureCmdY == 0)
{
rc.top += 130;
SureCmdY = rc.top;
SetWindowPos(
SureCmd,
0,
rc.left-52, // 52是一个按钮的长度,不知道为什么拿到的left总是大了52
rc.top,
0,
0,
// SWP_NOSIZE不改变大小,cx,cx参数被忽略
// SWP_NOZORDER不改变Z序列,hWndInsertAfter参数被忽略
SWP_NOSIZE | SWP_NOZORDER);
}
ZeroMemory(&rc,sizeof(rc));
GetWindowRect(CancelCmd,&rc);
if(CancelCmdY == 0)
{
rc.top += 130;
CancelCmdY = rc.top;
SetWindowPos(
CancelCmd,
0,
rc.left-52, // 52是一个按钮的长度,不知道为什么拿到的left总是大了52
rc.top,
0,
0,
// SWP_NOSIZE不改变大小,cX,cY参数被忽略
// SWP_NOZORDER不改变Z序列,hWndInsertAfter参数被忽略
SWP_NOSIZE | SWP_NOZORDER);
}
break;
default:
break;
}
return 0;
}
int APIENTRY _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow
)
{
PRINTDLG pd = {0};
LPDEVMODE devM = nullptr;
LPDEVNAMES devN = nullptr;
pd.lStructSize = sizeof( pd );
pd.Flags = PD_RETURNDC | PD_ENABLEPRINTHOOK;
pd.lpfnPrintHook = PrintHookProc;
// 取得打印机DC
PrintDlg(&pd);
// 置空打印设置对话框的公共变量
PrintDlgBottom = 0;
SureCmd = 0;
CancelCmd = 0;
SureCmdY = 0;
CancelCmdY = 0;
// devM将包含打印机名
devM = (LPDEVMODE)::GlobalLock(pd.hDevMode);
devN = (LPDEVNAMES)::GlobalLock(pd.hDevNames);
GlobalUnlock(devM);
return 0;
}