• 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;
    }
  • 相关阅读:
    zabbix server 给agent 添加 CPU 监听笔记
    解决windows文件名过长无法删除的问题
    一个大的OpenAPI Specification yaml 分割成小的yaml文件
    win10 移除2345输入法
    树莓派登录ssh 很慢和开机启动tightvncserver
    docker gitlab-ce 容器root密码重置小记
    Arduino Nokia 5110 LCD屏幕使用小记
    Docker Compose文件下载慢的处理笔记
    Ubuntu 18.04 安装rtorrent笔记
    vagrant Which interface should the network bridge to?
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/6799282.html
Copyright © 2020-2023  润新知