码迷,mamicode.com
首页 > Windows程序 > 详细

a window

时间:2017-12-21 18:12:20      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:idc   created   headers   draw   dcl   mes   reg   not   variable   

技术分享图片

 

 

技术分享图片
  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

a window

标签:idc   created   headers   draw   dcl   mes   reg   not   variable   

原文地址:http://www.cnblogs.com/YinaPan/p/awindow.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!