• a window


      1 //Include necessary Headers//
      2 #include <windows.h>
      3 
      4 //Define variables/constants//
      5 LPCTSTR WndClassName = L"firstwindow";    //Define our window class name
      6 HWND hwnd = NULL;    //Sets our windows handle to NULL
      7 
      8 const int Width  = 800;    //window width
      9 const int Height = 600;    //window height
     10 
     11 //Functions//
     12 bool InitializeWindow(HINSTANCE hInstance,    //Initialize our window
     13                       int ShowWnd,
     14                       int width, int height,
     15                       bool windowed);
     16 
     17 int messageloop();    //Main part of the program
     18 
     19 LRESULT CALLBACK WndProc(HWND hWnd,    //Windows callback procedure
     20                          UINT msg,
     21                          WPARAM wParam,
     22                          LPARAM lParam);
     23 
     24 int WINAPI WinMain(HINSTANCE hInstance,    //Main windows function
     25                    HINSTANCE hPrevInstance, 
     26                    LPSTR lpCmdLine,
     27                    int nShowCmd)
     28 {
     29     //Initialize Window//
     30     if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
     31     {
     32         //If initialization failed, display an error message
     33         MessageBoxW(0, L"Window Initialization - Failed",
     34             L"Error", MB_OK);
     35         return 0;
     36     }
     37 
     38     messageloop();    //Jump into the heart of our program
     39 
     40     return 0;
     41 }
     42 
     43 bool InitializeWindow(HINSTANCE hInstance,    //Initialize our window
     44                       int ShowWnd,
     45                       int width, int height,
     46                       bool windowed)
     47 {
     48     //Start creating the window//
     49 
     50     WNDCLASSEX wc;    //Create a new extended windows class
     51 
     52     wc.cbSize = sizeof(WNDCLASSEX);    //Size of our windows class
     53     wc.style = CS_HREDRAW | CS_VREDRAW;    //class styles
     54     wc.lpfnWndProc = WndProc;    //Default windows procedure function
     55     wc.cbClsExtra = NULL;    //Extra bytes after our wc structure
     56     wc.cbWndExtra = NULL;    //Extra bytes after our windows instance
     57     wc.hInstance = hInstance;    //Instance to current application
     58     wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);    //Title bar Icon
     59     wc.hCursor = LoadCursor(NULL, IDC_ARROW);    //Default mouse Icon
     60     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);    //Window bg color
     61     wc.lpszMenuName = NULL;    //Name of the menu attached to our window
     62     wc.lpszClassName = WndClassName;    //Name of our windows class
     63     wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //Icon in your taskbar
     64 
     65     if (!RegisterClassEx(&wc))    //Register our windows class
     66     {
     67         //if registration failed, display error
     68         MessageBoxW(NULL, L"Error registering class",    
     69             L"Error", MB_OK | MB_ICONERROR);
     70         return 1;
     71     }
     72 
     73     hwnd = CreateWindowExW(    //Create our Extended Window
     74         NULL,    //Extended style
     75         WndClassName,    //Name of our windows class
     76         L"Window Title ----Hello",    //Name in the title bar of our window
     77         WS_OVERLAPPEDWINDOW,    //style of our window
     78         CW_USEDEFAULT, CW_USEDEFAULT,    //Top left corner of window
     79         width,    //Width of our window
     80         height,    //Height of our window
     81         NULL,    //Handle to parent window
     82         NULL,    //Handle to a Menu
     83         hInstance,    //Specifies instance of current program
     84         NULL    //used for an MDI client window
     85         );
     86 
     87     if (!hwnd)    //Make sure our window has been created
     88     {
     89         //If not, display error
     90         MessageBoxW(NULL, L"Error creating window",
     91             L"Error", MB_OK | MB_ICONERROR);
     92         return 1;
     93     }
     94 
     95     ShowWindow(hwnd, ShowWnd);    //Shows our window
     96     UpdateWindow(hwnd);    //Its good to update our window
     97 
     98     return true;    //if there were no errors, return true
     99 }
    100 
    101 int messageloop(){    //The message loop
    102 
    103     MSG msg;    //Create a new message structure
    104     ZeroMemory(&msg, sizeof(MSG));    //clear message structure to NULL
    105 
    106     while(true)    //while there is a message
    107     {
    108         //if there was a windows message
    109         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    110         {
    111             if (msg.message == WM_QUIT)    //if the message was WM_QUIT
    112                 break;    //Exit the message loop
    113 
    114             TranslateMessage(&msg);    //Translate the message
    115 
    116             //Send the message to default windows procedure
    117             DispatchMessage(&msg);
    118         }
    119         else
    120         {    //Otherewise, keep the flow going
    121 
    122         }
    123 
    124 
    125     }
    126 
    127     return (int)msg.wParam;        //return the message
    128 
    129 }
    130 
    131 LRESULT CALLBACK WndProc(HWND hwnd,    //Default windows procedure
    132                          UINT msg,
    133                          WPARAM wParam,
    134                          LPARAM lParam)
    135 {
    136     switch( msg )    //Check message
    137     {
    138 
    139     case WM_KEYDOWN:    //For a key down
    140         //if escape key was pressed, display popup box
    141         if( wParam == VK_ESCAPE ){
    142             if(MessageBoxW(0, L"Are you sure you want to exit?",
    143                 L"Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
    144 
    145                 //Release the windows allocated memory  
    146                 DestroyWindow(hwnd);
    147         }
    148         return 0;
    149 
    150     case WM_DESTROY:    //if x button in top right was pressed
    151         PostQuitMessage(0);
    152         return 0;
    153     }
    154     //return the message for windows to handle it
    155     return DefWindowProc(hwnd,    
    156         msg,
    157         wParam,
    158         lParam);
    159 }
    View Code

    学习地址:

    https://www.braynzarsoft.net/viewtutorial/q16390-directx-11-an-introduction-to-the-win32-api

    本文由博主(YinaPan)原创或者转载,如若转载请务必注明出处,谢谢合作!
  • 相关阅读:
    HDU 1025 Constructing Roads In JGShining's Kingdom (DP+二分)
    HDU 1158 Employment Planning
    HDU 2059 龟兔赛跑
    Csharp 简单操作Word模板文件
    Csharp windowform datagridview Clipboard TO EXCEL OR FROM EXCEL DATA 保存datagridview所有數據
    Csharp 讀寫文件內容搜索自動彈出 AutoCompleteMode
    Csharp windowform controls clear
    CSS DIV大图片右上角叠加小图片
    Csharp DataGridView自定义添加DateTimePicker控件日期列
    Csharp 打印Word文件默認打印機或選擇打印機設置代碼
  • 原文地址:https://www.cnblogs.com/YinaPan/p/awindow.html
Copyright © 2020-2023  润新知