• windows新建一个窗口程序示例


    程序步骤:

    1,winmain函数定义

    2,创建一个窗口

    3,进行消息循环

    4,编写窗口过程函数。

    看代码,如下:

    #include <windows.h>
    #include <stdio.h>

    LRESULT CALLBACK WinSunProc(
    HWND hwnd, // handle to window
    UINT uMsg, // message identifier
    WPARAM wParam, // first message parameter
    LPARAM lParam // second message parameter
    );

    int WINAPI WinMain(
    HINSTANCE hInstance, // handle to current instance
    HINSTANCE hPrevInstance, // handle to previous instance
    LPSTR lpCmdLine, // command line
    int nCmdShow // show state
    )
    {
    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
    wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=WinSunProc;
    wndcls.lpszClassName="Weixin2003";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wndcls);

    HWND hwnd;
    hwnd=CreateWindow("Weixin2003","北京维新科学技术培训中心",WS_OVERLAPPEDWINDOW&~WS_MINIMIZEBOX,
    300,100,500,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

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

    LRESULT CALLBACK WinSunProc(
    HWND hwnd, // handle to window
    UINT uMsg, // message identifier
    WPARAM wParam, // first message parameter
    LPARAM lParam // second message parameter
    )
    {
    switch(uMsg)
    {
    case WM_CHAR:
    char szChar[20];
    sprintf(szChar,"char is %d",wParam);
    MessageBox(hwnd,szChar,"weixin",MB_OK );
    break;
    case WM_LBUTTONDOWN: //鼠标左键按下的消息
    MessageBox(hwnd,"mouse clicked","weixin",0);
    HDC hdc; //a handle to device context ,dc
    hdc=GetDC(hwnd);
    TextOut(hdc,20,50,"计算机编程语言培训",strlen("计算机编程语言培训"));
    ReleaseDC(hwnd,hdc);
    break;
    case WM_PAINT : //窗口重绘时发生消息.
    HDC hDC;
    PAINTSTRUCT ps;//系统自己维护
    hDC=BeginPaint(hwnd,&ps);
    TextOut(hDC,0,0,"维新培训",strlen("维新培训"));
    EndPaint(hwnd,&ps);
    break;

    case WM_CLOSE: //Sent as a signal that a window or an application should terminate.

    {
    DestroyWindow(hwnd); //destroy teh specified windows
    } /*Destroys the specified window. The function sends WM_DESTROY
    and WM_NCDESTROY messages to the window to deactivate it and
    remove the keyboard focus from it.只是销毁窗口,并没有退出程序
    */
    break;
    case WM_DESTROY: //
    if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd,uMsg,wParam,lParam);//对不感兴趣缺省处理 不能少这行,否则不显示.
    }
    return 0;
    }

     注意WinMain相当于main函数,跟c一样,在main后面定义的函数有先声明,否则出错

    运行结果如图;

    注意:若写成


    HWND hwnd;
    hwnd=CreateWindow("Weixin2003","北京维新科学技术培训中心",WS_OVERLAPPEDWINDOW&~WS_MINIMIZEBOX,
    300,100,500,400,NULL,NULL,hInstance,NULL);

    MSG msg;

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

    你只想获取hwnd窗口的句柄,关闭程序,发现程序还在后台运行,没有退出,为什么?

    GetMessage当hwnd为无效的窗口句柄或lpMsg参数为无效的指针,返回-1,程序并没有退出。

      当你退出时,窗口被销毁,hwnd为无效句柄,所以一直是-1,陷入了死循环,机器变慢了。

    msdn建议:

    The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:

    BOOL bRet;
    
    while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error and possibly exit 
           写成return -1;
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
    
  • 相关阅读:
    手机网页唤醒app,
    h5 audio进度条
    js判断字符串与字符串相互包含,以及数组是否包含某个元素;
    vue cli 打包项目造成css背景图路径错误
    vue-cli webpack打包不.map文件,iview 项目打包完,图标路径有问题
    常用的活动榜单
    vue-cli的webpack模板项目配置文件分析,配置信息详解
    eclipse怎么debug项目
    什么是前置机,前置机的作用是什么
    jsp中连接数据库及实现增删改查
  • 原文地址:https://www.cnblogs.com/youxin/p/2424749.html
Copyright © 2020-2023  润新知