zoukankan      html  css  js  c++  java
  • MFC学习之窗口基础

                              WinMain函数 

     

    1、句柄(HANDLE):{

    1. 定义:资源的标识

    2. 句柄的作用: 操作系统通过句柄来找到对应的资源,从而对这些资源进行管理和操作。

    3句柄的分类:(按资源)

           1.图标句柄(HICON) ,

           2.光标句柄(HCURSOR) ,

                3. 窗口句柄(HWND) ,

               4.应用程序实列句柄(HINSTANCE.

    2Windows应用程序,操作系统,计算机硬件之间的相互关系

    Windows程序的入口函数:

     

     

    窗口应用程序入口:

    Int WINAPI WinMain(

     HINSTANCE hinstance ;  // 应用程序实列句柄

     HINSTANCE hPrevInstance ;  // 基本都设置为0                    

     LPSTR   ipCmdLine ; /commandLLine  LPSTRLP(long point 长指针)

    int   nCmdSbow   ;   //显示状态

    )  

    3、 窗口的创建:

    大致来说,如果要创建一个完整的窗口需要经过下面四个操作步骤:{

    (1)、设计一个窗口类;

    (2)、 注册窗口类 ;

    (3)、创建窗口;

    (4)、显示及更新窗口。

     }

     

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<windows.h>
     4 
     5 /*声明winSunProc*/
     6 LRESULT CALLBACK WinSunProc(  HWND hwnd,      // handle to window
     7   UINT uMsg,      // message identifier
     8   WPARAM wParam,  // first message parameter
     9   LPARAM lParam   // second message parameter);
    10 );
    11 int WINAPI WinMain(  HINSTANCE hInstance,       // handle to current instance
    12   HINSTANCE hPrevInstance,                // handle to previous instance
    13   LPSTR lpCmdLine,          // command line
    14   int nCmdShow              // show state);
    15 )
    16 {
    17     WNDCLASS wndclass;
    18     /*声明定义什么的*/    
    19     wndclass.cbClsExtra = NULL;  
    20     wndclass.cbWndExtra = NULL;
    21     wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    22     wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
    23     wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
    24     wndclass.hInstance = hInstance;
    25     wndclass.lpfnWndProc = WinSunProc ;
    26     wndclass.lpszClassName = "Gxjun";
    27     wndclass.lpszMenuName =NULL;
    28     wndclass.style= CS_HREDRAW|CS_VREDRAW ;
    29     RegisterClass(&wndclass);    //注册窗口函数
    30     HWND hwnd;
    31     hwnd =  CreateWindow("Gxjun","龚细军的第一个窗口程序",WS_OVERLAPPEDWINDOW,0,0,400,600,NULL,NULL,hInstance,NULL);  /*创建窗口*/
    32     ShowWindow(hwnd,SW_SHOWNORMAL);    /*显示窗口*/
    33     UpdateWindow(hwnd);     
    34     MSG msg;
    35     while(GetMessage(&msg,NULL,0,0))   
    36     {
    37        TranslateMessage(&msg);       /*该函数将虚拟键消息转换为字符消息*/
    38 
    39        DispatchMessage(&msg);        /*该函数分发一个消息给窗口程序。通常消息从GetMessage函数获得。
    40                                      消息被分发到回调函数(过程函数),作用是消息传递给操作系统,
    41                                      然后操作系统去调用我们的回调函数,也就是说我们在窗体的过程函
    42                                      数中处理消息*/
    43     }
    44 
    45     return 0;
    46 }
    47 LRESULT CALLBACK WinSunProc(  HWND hwnd,      // handle to window
    48   UINT uMsg,      // message identifier
    49   WPARAM wParam,  // first message parameter
    50   LPARAM lParam   // second message parameter);
    51   )
    52 {
    53     
    54     switch(uMsg)
    55     {
    56       case WM_CHAR:  
    57           char str[26];
    58           sprintf(str,"char is %d",wParam);
    59           MessageBox(hwnd,str,"Gxjun",0);
    60           break;
    61       case WM_LBUTTONDOWN: 
    62           MessageBox(hwnd,"mouse clicked","Gxjun",0);
    63           HDC hdc;
    64           /*PAINTSTRUCT  ps;*/
    65           hdc=GetDC(hwnd);
    66           TextOut(hdc,200,400,"我是胡萝卜头,呼叫北极站",strlen("我是胡萝卜头,呼叫北极站"));
    67           ReleaseDC(hwnd,hdc);    /*函数释放设备上下文环境(DC)供其他应用程序使用。*/
    68             break;
    69       case WM_PAINT:
    70            HDC hdc_1;
    71            PAINTSTRUCT paints;
    72            hdc_1 = BeginPaint(hwnd,&paints);
    73            TextOut(hdc_1,0,0,"我是长城好哇",strlen("我是长城好哇"));
    74           break;
    75       case WM_CLOSE:
    76          if(IDYES==MessageBox(hwnd,"哇哈哈,你丫的真的打算关掉吗?","卖萌之家",MB_YESNO))  {
    77             DestroyWindow(hwnd);
    78           }
    79            break;
    80       case WM_DESTROY:
    81           PostQuitMessage(0);
    82           break;
    83       default:
    84           return DefWindowProc(hwnd,uMsg,wParam, lParam);
    85     }
    86     return 0;
    87 }

    效果图:

  • 相关阅读:
    centos8 将SSSD配置为使用LDAP并要求TLS身份验证
    Centos8 搭建 kafka2.8 .net5 简单使用kafka
    .net core 3.1 ActionFilter 拦截器 偶然 OnActionExecuting 中HttpContext.Session.Id 为空字符串 的问题
    Springboot根据不同环境加载对应的配置
    VMware Workstation12 安装 Centos8.3
    .net core json配置文件小结
    springboot mybatisplus createtime和updatetime自动填充
    .net core autofac依赖注入简洁版
    .Net Core 使用 redis 存储 session
    .Net Core 接入 RocketMQ
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3886012.html
Copyright © 2011-2022 走看看