📄 prntwait.c
字号:
GetDlgItemText(hDlg, IDC_EDIT_TEXT, szText, 256);
PrintText(hDlg, szText);
break;
case IDC_DISPLAY:
GetDlgItemText(hDlg, IDC_EDIT_TEXT, szText, 256);
PrintToDisplay(hDlg, szText);
break;
case IDCANCEL:
case IDM_EXIT:
PostMessage(GetParent(hDlg),WM_DESTROY,
(WPARAM)0, (LPARAM)0);
DestroyWindow(hDlgMain);
hDlgMain = NULL;
break;
default:
break;
}
}
void PrintDlg_OnPaint( HWND hwnd )
{
PAINTSTRUCT paint;
HWND hwndCtrl;
HDC hdc;
HDC hDcMem;
HBITMAP bmpOld;
RECT rect;
POINT point;
if (!gbmpDisplay)
return;
hwndCtrl = GetDlgItem(hwnd, IDC_OUTPUT);
hdc = BeginPaint(hwnd, &paint);
GetWindowRect(hwndCtrl, &rect);
point = *((POINT *)&rect);
ScreenToClient(hwnd, &point);
hDcMem = CreateCompatibleDC(NULL);
bmpOld = SelectObject(hDcMem, gbmpDisplay);
// Copy bitmap to screen
MTVERIFY( BitBlt(hdc, point.x+10, point.y+40,
gDisplayRect.right-gDisplayRect.left, gDisplayRect.bottom-gDisplayRect.top,
hDcMem, iHeight, 0, SRCCOPY) );
SelectObject(hDcMem, bmpOld);
DeleteDC(hDcMem);
EndPaint(hwnd, &paint);
}
//
// Asks user which printer to use, then creates
// background printing thread.
//
void PrintText(HWND hwndParent, char *pszText)
{
ThreadPrintInfo *pInfo;
HANDLE hThread;
DWORD dwThreadId;
int result;
DOCINFO docInfo;
PRINTDLG dlgPrint;
// Put up Common Dialog for Printing and get hDC.
memset(&dlgPrint, 0, sizeof(PRINTDLG));
dlgPrint.lStructSize = sizeof(PRINTDLG);
dlgPrint.hwndOwner = hwndParent;
dlgPrint.Flags = PD_ALLPAGES | PD_USEDEVMODECOPIES
| PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
dlgPrint.hInstance = hInst;
if (!PrintDlg(&dlgPrint))
return;
// Initialize Printer device
docInfo.cbSize = sizeof(DOCINFO);
docInfo.lpszDocName = "Background Printing Example";
docInfo.lpszOutput = NULL;
docInfo.lpszDatatype = NULL;
docInfo.fwType = 0;
result = StartDoc(dlgPrint.hDC, &docInfo);
result = StartPage(dlgPrint.hDC);
pInfo = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(ThreadPrintInfo));
pInfo->hDlg = hwndParent;
pInfo->hWndParent = hwndParent;
pInfo->hDc = dlgPrint.hDC;
pInfo->bPrint = TRUE;
strcpy(pInfo->szText, pszText);
MTVERIFY( hThread = CreateThread(NULL, 0,
BackgroundPrintThread, (LPVOID)pInfo,
0, &dwThreadId ));
// keep track of all background printing threads
gPrintJobs[gNumPrinting++] = hThread;
SendMessage(hwndParent, WM_THREADCOUNT, gNumPrinting, 0L);
}
//
// Shows output on the dialog box.
//
void PrintToDisplay(HWND hwndParent, char *pszText)
{
ThreadPrintInfo *pInfo;
DWORD dwThreadId;
HANDLE hThread;
pInfo = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
sizeof(ThreadPrintInfo));
pInfo->hDlg = hwndParent;
pInfo->hWndParent = GetDlgItem(hwndParent, IDC_OUTPUT);
pInfo->hDc = GetDC(pInfo->hWndParent);
pInfo->bPrint = FALSE;
strcpy(pInfo->szText, pszText);
MTVERIFY( hThread = CreateThread(NULL, 0,
BackgroundPrintThread,
(LPVOID)pInfo,
0, &dwThreadId ));
// keep track of all background printing threads
gPrintJobs[gNumPrinting++] = hThread;
SendMessage(hwndParent, WM_THREADCOUNT, gNumPrinting, 0L);
}
//---------------------------------------------------------
// About Box Handling
//---------------------------------------------------------
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_COMMAND:
if (LOWORD(wParam) == IDOK
|| LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
default:
return (DefWindowProc(hDlg, message, wParam, lParam));
}
return FALSE;
}
//---------------------------------------------------------
// Background Printing Code
//---------------------------------------------------------
DWORD WINAPI BackgroundPrintThread(LPVOID pVoid)
{
ThreadPrintInfo *pInfo = (ThreadPrintInfo*) pVoid;
RECT rect;
RECT rectMem;
HDC hDcMem;
HBITMAP bmpMem;
HBITMAP bmpOld;
int x, y;
int counter = 0;
int nHeight;
HFONT hFont;
HFONT hFontOld;
// Get dimensions of paper into rect
rect.left = 0;
rect.top = 0;
rect.right = GetDeviceCaps(pInfo->hDc, HORZRES);
rect.bottom = GetDeviceCaps(pInfo->hDc, VERTRES);
nHeight = -MulDiv(36, GetDeviceCaps(pInfo->hDc, LOGPIXELSY), 72);
// Create Font
hFont = CreateFont(nHeight, 0,
0, 0, FW_DONTCARE,
FALSE, FALSE, FALSE,
ANSI_CHARSET,
OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
VARIABLE_PITCH,
"Arial");
MTASSERT( hFont != 0);
// Draw into memory device context
hDcMem = CreateCompatibleDC(pInfo->hDc);
hFontOld = SelectObject(hDcMem, hFont);
iHeight = DrawText(hDcMem, pInfo->szText, -1, &rect, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK | DT_CALCRECT);
rectMem = rect;
rectMem.left = rect.left + iHeight;
rectMem.right = rect.right + (iHeight*2);
bmpMem = CreateCompatibleBitmap(hDcMem,
rectMem.right, rect.bottom);
bmpOld = SelectObject(hDcMem, bmpMem);
OffsetRect(&rect, iHeight, 0);
DrawText(hDcMem, pInfo->szText, -1, &rect,
DT_LEFT | DT_NOPREFIX | DT_WORDBREAK);
// Italicize bitmap. We use GetPixel and
// SetPixel because they are horribly inefficient,
// thereby causing the thread to run for awhile.
for (y = 0; y < iHeight; y++)
{ // Italicize line y
for (x = rectMem.right; x > iHeight; x--)
{ // Move specified pixel to the right.
COLORREF color;
int offset;
offset = y - iHeight;
color = GetPixel(hDcMem, x + offset, y);
if (color != 0)
counter++;
SetPixel(hDcMem, x, y, color);
} // end for x
} // end for y
MTASSERT( counter > 0);
// Copy bitmap of italicized text from memory to device
if (pInfo->bPrint)
{
BitBlt(pInfo->hDc, 50, 50, rectMem.right-rect.left, rectMem.bottom-rect.top,
hDcMem, iHeight, 0, SRCCOPY);
}
SelectObject(hDcMem, hFontOld);
SelectObject(hDcMem, bmpOld);
DeleteDC(hDcMem);
if (!pInfo->bPrint)
{
// We can't just write to the global variable where the
// bitmap is kept or we might overwrite the work of
// another thread, thereby "losing" a bitmap
// Also, if we used PostMessage instead of SendMessage, then
// the rectangle could have been deleted (it's on the stack)
// by the time the main message loop is reached.
SendMessage(pInfo->hDlg, WM_SHOWBITMAP, (WPARAM)&rectMem, (LPARAM) bmpMem);
}
if (pInfo->bPrint)
{ // Finish printing
int result;
result = EndPage(pInfo->hDc);
MTASSERT (result != SP_ERROR);
result = EndDoc(pInfo->hDc);
MTASSERT (result != SP_ERROR);
DeleteDC(pInfo->hDc);
// If we are printing, we are done with the bitmap.
DeleteObject(bmpMem);
}
else
{
ReleaseDC(pInfo->hWndParent, pInfo->hDc);
}
// free data structure passed in.
HeapFree(GetProcessHeap(), 0, pInfo);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -