zoukankan      html  css  js  c++  java
  • 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
    ", 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;
    }
  • 相关阅读:
    把一件简单的事情做好你就不简单了
    一个经验尚浅的码农五年软件开发的一点自我总结,对工作五年的反思~
    我就是一名房地产经纪人!不是中介,谁能明白我们呢?
    我与父辈的酒局
    郎意难坚,侬情自热(文/王路)
    红灯须硬闯,马路要横穿(文/王路)
    孩子,你慢慢来
    职场六年后的一点点感言
    有幸见到一朵花的绽放
    当你遇到她
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/6799282.html
Copyright © 2011-2022 走看看