Windoows窗口程序六

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>

HINSTANCE g_hInstance=0;
HANDLE g_hOutput=0;///接收标准输出句柄
HWND g_hWndChild = 0;//子窗口句柄

void OnCreate(HWND hWnd,LPARAM lParam)
{
    CREATESTRUCT *cs = (CREATESTRUCT *)lParam;
    char *pText = (char *)cs->lpCreateParams;
    //MessageBox(NULL, pText, "info", MB_OK);
    //创建子窗口(子窗口的起始位置0,0是相对于父窗口而言的,并非指屏幕)
    g_hWndChild=CreateWindowEx(0, "EDIT", "OK", WS_CHILD | WS_VISIBLE | WS_BORDER,
        0, 0, cs->cx, cs->cy, hWnd, NULL, g_hInstance, NULL);
}

void OnSize(HWND hWnd, LPARAM lParam)
{
    int nWidth = LOWORD(lParam);
    int nHight = HIWORD(lParam);
    CHAR buf[256] = { 0 };
    sprintf(buf, "width=%d,hight=%d\n", nWidth, nHight);
    WriteConsole(g_hOutput, buf, strlen(buf),NULL,NULL);//输出到DOS窗口
    //排除窗口刚创建时的WM_SIZE消息
    if (NULL == g_hWndChild)
        return;
    //移动子窗口
    MoveWindow(g_hWndChild, 0, 0, nWidth, nHight, true);
}

LRESULT WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_SIZE:
        //窗口创建时会接收到WM_SIZE消息
        OnSize(hWnd,lParam);
        break;
    case WM_SYSCOMMAND:
        if (SC_CLOSE == wParam)
        {
            int nRet=MessageBox(NULL, "是否退出!", "info", MB_YESNO);
            if (nRet!=IDYES)
            {
                return 0;
            }
        }
        break;
    case WM_CREATE://在窗口生成之前执行
        OnCreate(hWnd,lParam);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int Register(HINSTANCE hInstance,LPCTSTR lpClassName)
{
    int ret = 1;
    WNDCLASSEX wce = { 0 };
    wce.cbSize = sizeof(wce);
    wce.style = CS_HREDRAW | CS_VREDRAW;
    wce.lpfnWndProc = (WNDPROC)WndProc;
    wce.cbClsExtra = 0;

    wce.cbWndExtra = 0;
    wce.hInstance = hInstance;
    wce.hIcon = NULL;
    wce.hCursor = NULL;

    wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wce.lpszMenuName = NULL;
    wce.lpszClassName = lpClassName;
    wce.hIconSm = NULL;
    ATOM nAtom = RegisterClassEx(&wce);
    ret = nAtom == 0 ? 0 : 1;
    return ret;
}

void Display(HWND hWnd)
{
    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);
}

void MyMessage()
{
    MSG nMsg = { 0 };
    while (GetMessage(&nMsg, NULL, 0, 0))
    {
        TranslateMessage(&nMsg);
        DispatchMessage(&nMsg);
    }
}

HWND CreateWnd(LPSTR lpClsssName, LPSTR lpWndName, HINSTANCE hInstance)
{
    HWND hWnd = CreateWindowEx(0, lpClsssName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT
        , 500, 600, NULL, NULL, hInstance,"HELL");
    return hWnd;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
    AllocConsole();//打开DOS窗口
    g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出句柄
    g_hInstance = hInstance;
    if (!Register(hInstance, "Main"))
    {
        return -1;
    }
    HWND hWnd=CreateWnd("Main", "hello", hInstance);
    Display(hWnd);
    MyMessage();
    return 0;
}

 

posted on 2017-05-02 23:33  寒魔影  阅读(336)  评论(0编辑  收藏  举报

导航