qq_26591851
黑白的记忆1997
2017-11-18 08:46

底层C语言编程…………未果

  • c
  • 操作系统
  • 编程

#include
#include
#include
#include
// Global variables
// The main window class name.
static TCHAR szWindowClass[] = _T("Basic Print");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Basic Print Application");
HINSTANCE hInst;
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void PrintRectangle();

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc= WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName= NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if (!RegisterClassEx(&wcex))
{
    MessageBox(NULL,_T("Call to RegisterClassEx failed!"), szTitle,NULL);
    return 1;
}
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindow(
    szWindowClass, 
    szTitle, 
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 
    CW_USEDEFAULT, 500, 100,
    NULL, 
    NULL,
    hInstance, 
    NULL 
);

if (!hWnd)
{
    MessageBox(NULL,_T("Call to CreateWindow failed!"), szTitle,NULL);
    return 1;
}

// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain 
ShowWindow(hWnd,nCmdShow); 
UpdateWindow(hWnd);

// Main message loop: 
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg); 
    DispatchMessage(&msg);
}

return (int) msg.wParam;

}
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR Print[] = _T("Click in the window to print.");

switch (message)
{
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        TextOut(hdc,5, 5,Print, _tcslen(Print));
        // End application-specific layout section.

        EndPaint(hWnd, &ps); 
        break;
    case WM_LBUTTONDOWN:
        PrintRectangle() ; 
        return 0;
    case WM_DESTROY: 
        PostQuitMessage(0); 
        break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam); 
    break;
}
return 0;

}
void PrintRectangle()
{
HDC PrintDC; DOCINFO docInfo;
TCHAR docName[] = _T("RectangleDoc") ;
int result;
DWORD size;

// 第 1 步 : 获 得 打 印 机 的 DC 
GetDefaultPrinter(NULL,&size); 
TCHAR *PrintName = new TCHAR[size]; 
GetDefaultPrinter(PrintName,&size); 
PrintDC = CreateDC(NULL,PrintName, NULL, NULL) ;

// 第 2 步 : 调 用 StartDoc() 
docInfo.cbSize = sizeof(docInfo) ; 
docInfo.lpszDocName = (LPCWSTR)docName; 
docInfo.lpszOutput = NULL; 
docInfo.lpszDatatype = NULL; 
docInfo.fwType = 0;
result = StartDoc(PrintDC, &docInfo) ; 
if (result <= 0)
{
    MessageBox(0, _T("StartDoc() failed") , szTitle , MB_OK|MB_ICONERROR) ;
    return;
}
// 第 3 步:调用 StartPage() 
result = StartPage(PrintDC) ; 
if (result <= 0)
{
    MessageBox(0, _T("StartPage() failed") , szTitle , MB_OK|MB_ICONERROR) ;
    return;
}
// 第 4 步:打印数据
Rectangle(PrintDC, 20, 20, 1000, 200) ;
TextOut(PrintDC, 100, 90,_T("Windows API"), 11) ;
// 第 5 步:调用 EndPage() 
result = EndPage(PrintDC) ; 
if (result <= 0)
{
    MessageBox(0, _T("EndPage() failed"), szTitle , MB_OK|MB_ICONERROR) ;
    return;
}
// 第 6 步:调用 EndDOC() 
EndDoc(PrintDC) ;
MessageBox(0, _T("Document printed") ,szTitle , MB_OK|MB_ICONINFORMATION) ;

}图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换