window实验

来源:互联网 发布:江宁人才网最新域名 编辑:程序博客网 时间:2024/06/07 06:14

第一次window实验:

实验代码:

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_LBUTTONDOWN:    // <-
        {
            char szFileName[MAX_PATH];
            HINSTANCE hInstance = GetModuleHandle(NULL);

            GetModuleFileName(hInstance, szFileName, MAX_PATH);
            MessageBox(hwnd, "This program is:",szFileName, MB_OK | MB_ICONINFORMATION);
        }
              // <-     we just added this stuff
        break;

        case WM_CLOSE:
           {if(MessageBox(NULL, "Do you want to save your file?", "Warning", MB_OKCANCEL)==IDCANCEL)
            {
              //DestroyWindow(hwnd);
            }
            else
            {DestroyWindow(hwnd);}
           }
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{


    //MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OKCANCEL);


    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 2;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL,IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+9);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

实现的功能:实现简单窗口类的注册和显示,在此次实验中我大致了解了用C语言编写简单窗口的实现代码,对于弹窗函数MessageBox的参数设置和控制语句的实现,

例如,在一个窗口打开过程中,如果点击了窗口的『x』按钮,则会弹窗警告(Warning)窗口,如果点击“是”,将destory窗口,如果点击“否”,将什么也不做,

具体是通过循环控制语句来实现的。代码如下:

case WM_CLOSE:
           {if(MessageBox(NULL, "Do you want to save your file?", "Warning", MB_OKCANCEL)==IDCANCEL)
            {
              //DestroyWindow(hwnd);
            }
            else
            {DestroyWindow(hwnd);}
           }
        break;case WM_CLOSE:
{if(MessageBox(NULL, "Do you want to save your file?", "Warning", MB_OKCANCEL)==IDCANCEL)
{
//DestroyWindow(hwnd);
}
else
{DestroyWindow(hwnd);}
}
break;



0 0
原创粉丝点击