黑白的记忆1997 2017-11-18 08:46 采纳率: 0%
浏览 850

底层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条回答 默认 最新

  • threenewbee 2017-11-18 08:48
    关注

    不应该是LPSTR应该是LPWSTR,你的头文件有问题或者你的定义不对,为什么不用vc++

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!