没有显示窗口,只有文字

没有显示窗口,只有文字


#include         

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    PSTR szCmdLine,
    int iCmdShow)
{
    typedef struct tagPAINTSTRUCT {
        HDC hdc;                      // 设备环境句柄
        BOOL fErase;               // 是否要擦除背景
        RECT rcPaint;              //无效矩形
        BOOL fRestore;         // 系统保留
        BOOL fIncUpdate;        //系统保留BYTE     rgbReserved[32];             //系统保留
        BOOL Ellipse(HDC hdc, int x1, int y1, int x2, int y2);

    } PAINTSTRUCT, * PPAINTSTRUCT;


    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND   hwnd;
    MSG    msg;
    WNDCLASS wndclass;

    wndclass.style = 0;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = TEXT("MYDemo");

    if (!RegisterClass(&wndclass))//(注册窗口类的API是RegisterClass)
    {
        MessageBox(NULL, TEXT("RegisterClass fail!"), szAppName, MB_ICONERROR);
        return 0;
    }
    //窗口起始位置(100,100),大小300*400   
    hwnd = CreateWindow(
        TEXT("MYDemo"),    // window class name
        szAppName,        // window caption
        WS_OVERLAPPEDWINDOW,    // window style
        100,            // initial x position
        100,            // initial y position
        300,            // initial x size
        400,            // initial y size
        NULL,                // parent window handle
        NULL,                // window menu handle
        NULL,        // program instance handle
        NULL);            // creation parameters


    if (!hwnd)
    {
        MessageBox(NULL, TEXT("CreateWindow fail!!"), szAppName, MB_ICONERROR);
        return 0;
    }


    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{
    HDC hdc;
    PAINTSTRUCT ps;

    TCHAR str_1[100] = TEXT("红色字体颜色,绿色背景的文本");
    TCHAR str_2[100] = TEXT("被遮盖的文本,背景透明");
    TCHAR str_3[100] = TEXT("显示在前面的文本,背景透明");
    int len_1 = wcslen(str_1);
    int len_2 = wcslen(str_2);
    int len_3 = wcslen(str_3);
    
    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);

        SetTextColor(hdc, RGB(255, 0, 0));
        SetBkColor(hdc, RGB(0, 255, 0));

        TextOut(hdc, 20, 20, str_1, len_1); SetBkMode(hdc, TRANSPARENT);
        TextOut(hdc, 20, 60, str_2, len_2);

        TextOut(hdc, 20, 66, str_3, len_3);
        EndPaint(hwnd, &ps);
        return 0;



    case  WM_DESTROY: {
        PostQuitMessage(0);
        return 0;

    }
                      
    return DefWindowProc(hwnd, message, wParam, lParam);
    }
}

img

return DefWindowProc(hwnd, message, wParam, lParam); 放到switch (message)外面。