控件练习

  1 // LandSystem.cpp : 定义应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "LandSystem.h"
  6 #include  <CommCtrl.h>
  7 #include  <windowsx.h>
  8 #pragma comment(lib,"Comctl32.lib")
  9 #include"resource.h"
 10 #define MAX_LOADSTRING 100
 11 
 12 // 全局变量: 
 13 HINSTANCE hInst;                                // 当前实例
 14 TCHAR szTitle[MAX_LOADSTRING];                    // 标题栏文本
 15 TCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名
 16 
 17 // 此代码模块中包含的函数的前向声明: 
 18 ATOM                MyRegisterClass(HINSTANCE hInstance);
 19 BOOL                InitInstance(HINSTANCE, int);
 20 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
 21 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 22 
 23 int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
 24                      _In_opt_ HINSTANCE hPrevInstance,
 25                      _In_ LPTSTR    lpCmdLine,
 26                      _In_ int       nCmdShow)
 27 {
 28     UNREFERENCED_PARAMETER(hPrevInstance);
 29     UNREFERENCED_PARAMETER(lpCmdLine);
 30 
 31      // TODO:  在此放置代码。
 32     MSG msg;
 33     HACCEL hAccelTable;
 34 
 35     // 初始化全局字符串
 36     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 37     LoadString(hInstance, IDC_LANDSYSTEM, szWindowClass, MAX_LOADSTRING);
 38     MyRegisterClass(hInstance);
 39 
 40 
 41     // 执行应用程序初始化: 
 42     if (!InitInstance (hInstance, nCmdShow))
 43     {
 44         return FALSE;
 45     }
 46 
 47     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LANDSYSTEM));
 48 
 49     // 主消息循环: 
 50     while (GetMessage(&msg, NULL, 0, 0))
 51     {
 52         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
 53         {
 54             TranslateMessage(&msg);
 55             DispatchMessage(&msg);
 56         }
 57     }
 58 
 59     return (int) msg.wParam;
 60 }
 61 
 62 
 63 
 64 //
 65 //  函数:  MyRegisterClass()
 66 //
 67 //  目的:  注册窗口类。
 68 //
 69 ATOM MyRegisterClass(HINSTANCE hInstance)
 70 {
 71     WNDCLASSEX wcex;
 72 
 73     wcex.cbSize = sizeof(WNDCLASSEX);
 74 
 75     wcex.style            = CS_HREDRAW | CS_VREDRAW;
 76     wcex.lpfnWndProc    = WndProc;
 77     wcex.cbClsExtra        = 0;
 78     wcex.cbWndExtra        = 0;
 79     wcex.hInstance        = hInstance;
 80     wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LANDSYSTEM));
 81     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
 82     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
 83     wcex.lpszMenuName = NULL;
 84     wcex.lpszClassName    = szWindowClass;
 85     wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
 86 
 87     return RegisterClassEx(&wcex);
 88 }
 89 
 90 //
 91 //   函数:  InitInstance(HINSTANCE, int)
 92 //
 93 //   目的:  保存实例句柄并创建主窗口
 94 //
 95 //   注释: 
 96 //
 97 //        在此函数中,我们在全局变量中保存实例句柄并
 98 //        创建和显示主程序窗口。
 99 //
100 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
101 {
102    HWND hWnd;
103 
104    hInst = hInstance; // 将实例句柄存储在全局变量中
105 
106    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
107       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
108 
109    if (!hWnd)
110    {
111       return FALSE;
112    }
113 
114    ShowWindow(hWnd, nCmdShow);
115    UpdateWindow(hWnd);
116 
117    return TRUE;
118 }
119 
120 //
121 //  函数:  WndProc(HWND, UINT, WPARAM, LPARAM)
122 //
123 //  目的:    处理主窗口的消息。
124 //
125 //  WM_COMMAND    - 处理应用程序菜单
126 //  WM_PAINT    - 绘制主窗口
127 //  WM_DESTROY    - 发送退出消息并返回
128 //
129 //
130 LRESULT CALLBACK WndDlog(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
131 {
132     int wmId, wmEvent;
133     PAINTSTRUCT ps;
134 
135 
136     switch (message)
137     {
138     case WM_INITDIALOG:
139     {
140         HWND hList = GetDlgItem(hWnd, IDC_LIST1);
141         RECT rt = {};
142         GetClientRect(hList, &rt);
143 
144         //插入多列
145         LVCOLUMN  lc;
146         lc.mask = LVCF_WIDTH | LVCF_TEXT;
147         lc.pszText = TEXT("年份");
148         lc.cx = (rt.right - rt.left) / 4;
149         lc.cchTextMax = 3;
150 
151         ListView_InsertColumn(hList, 0, &lc);
152         lc.pszText = TEXT("时间");
153         ListView_InsertColumn(hList, 1, &lc);
154         lc.pszText = TEXT("经历");
155         ListView_InsertColumn(hList, 2, &lc);
156         lc.pszText = TEXT("备注");
157         ListView_InsertColumn(hList, 3, &lc);
158         //插入多行
159         LVITEM  li = {};
160         //         li.mask = LVIF_TEXT;
161         //         li.pszText = TEXT("123");
162         ListView_InsertItem(hList, &li);
163         ListView_InsertItem(hList, &li);
164         ListView_InsertItem(hList, &li);
165 
166         ListView_SetItemText(hList, 0, 0, TEXT("2009"));
167         ListView_SetItemText(hList, 0, 1, TEXT("9月5日"));
168         ListView_SetItemText(hList, 0, 2, TEXT("清华大学附中"));
169         ListView_SetItemText(hList, 0, 3, TEXT("班长"));
170     }
171     break;
172     case WM_DESTROY:
173         PostQuitMessage(0);
174         break;
175 
176     default:
177         return DefWindowProc(hWnd, message, wParam, lParam);
178     
179     }
180     return 0;
181 }
182 
183 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
184 {
185     int wmId, wmEvent;
186     PAINTSTRUCT ps;
187     HDC hdc;
188 
189     switch (message)
190     {
191 
192     case WM_CREATE:{
193         //创建一个静态图片
194         HWND hStaticPic = CreateWindow(TEXT("static"), TEXT("15PB"),
195             WS_VISIBLE | WS_CHILD | SS_BITMAP,
196             30, 20, 50, 30, hWnd, (HMENU)10001, hInst, NULL);
197         HBITMAP  hmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));
198         SendMessage(hStaticPic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hmap);
199        //创建静态文本用户名
200         HWND hStaticText1 = CreateWindow(TEXT("static"), TEXT("UserName:"),
201             WS_CHILD | WS_VISIBLE, 100, 400, 90, 20,
202             hWnd, (HMENU)10002, hInst, NULL);
203         HWND hText1 = CreateWindow(TEXT("edit"), TEXT(""),
204             WS_BORDER | WS_CHILD | WS_VISIBLE, 190, 400, 120, 20,
205             hWnd, (HMENU)10003, hInst, NULL);
206         //创建静态文本密码
207         HWND hStaticText2 = CreateWindow(TEXT("static"), TEXT("PassWord:"),
208             WS_CHILD | WS_VISIBLE, 100, 430, 90, 20,
209             hWnd, (HMENU)10004, hInst, NULL);
210         HWND hText2 = CreateWindow(TEXT("edit"), TEXT(""),
211             WS_BORDER | WS_CHILD | WS_VISIBLE, 190, 430, 120, 20,
212             hWnd, (HMENU)10005, hInst, NULL);
213         //创建多选按钮
214         HWND hRadio1=CreateWindow(TEXT("button"), TEXT("网通"),
215             WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON,
216             600, 400, 50, 30, hWnd, (HMENU)10006, hInst, NULL);
217         HWND hRadio2 = CreateWindow(TEXT("button"), TEXT("联通"),
218             WS_VISIBLE | WS_CHILD |BS_AUTORADIOBUTTON,
219             600, 430, 50, 30, hWnd, (HMENU)10007, hInst, NULL);
220         HWND hRadio3 = CreateWindow(TEXT("button"), TEXT("电信"),
221             WS_VISIBLE | WS_CHILD |BS_AUTORADIOBUTTON,
222             600, 450, 50, 30, hWnd, (HMENU)10008, hInst, NULL);
223         ////下拉列表
224         //InitCommonControls();
225         //HWND hList = CreateWindow(WC_LISTVIEW, _T(""),
226         //    WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER,
227         //    330, 400, 200, 100, hWnd, (HMENU)10009, hInst, NULL);
228         ////获取窗口大小
229         //RECT rt = {};
230         //GetClientRect(hList, &rt);
231 
232         HWND hComb = CreateWindow(TEXT("combobox"),
233             TEXT("网络"),
234             WS_CHILD | WS_VISIBLE|LBS_COMBOBOX|CBS_DROPDOWN,
235             330, 400, 200, 100, hWnd, (HMENU)10011, hInst, NULL);
236         ComboBox_AddString(hComb, TEXT("北京"));
237         ComboBox_AddString(hComb, TEXT("上海"));
238         ComboBox_AddString(hComb, TEXT("天津"));
239         ComboBox_AddString(hComb, TEXT("南京"));
240 
241 
242         HWND hbutton = CreateWindow(TEXT("button"), TEXT("登录"),
243             WS_VISIBLE | WS_CHILD ,
244             700, 400, 50, 30, hWnd, (HMENU)10012, hInst, NULL);
245 
246 
247         HWND hbutton1 = CreateWindow(TEXT("button"), TEXT("取消"),
248             WS_VISIBLE | WS_CHILD,
249             700, 450, 50, 30, hWnd, (HMENU)10013, hInst, NULL);
250 
251 
252 
253 
254     }break;
255     case WM_COMMAND:
256         wmId    = LOWORD(wParam);
257         wmEvent = HIWORD(wParam);
258         // 分析菜单选择: 
259         switch (wmId)
260         {
261         case 10012:{
262             DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, (DLGPROC)WndDlog);
263         }
264         case 10006:{
265             HWND hCheck = GetDlgItem(hWnd, 10006);
266             BOOL bIsCheck = SendMessage(hCheck, BM_GETCHECK, 0, 0);
267             if (bIsCheck == 1)
268             {
269                 SendMessage(hCheck, BM_SETCHECK, 0, 0);
270             }
271             else
272             {
273                 SendMessage(hCheck, BM_SETCHECK, 1, 0);
274             }    
275         
276         }break;
277 
278         case IDM_EXIT:
279             DestroyWindow(hWnd);
280             break;
281         default:
282             return DefWindowProc(hWnd, message, wParam, lParam);
283         }
284         break;
285     case WM_PAINT:
286         hdc = BeginPaint(hWnd, &ps);
287         // TODO:  在此添加任意绘图代码...
288         EndPaint(hWnd, &ps);
289         break;
290     case WM_DESTROY:
291         PostQuitMessage(0);
292         break;
293     default:
294         return DefWindowProc(hWnd, message, wParam, lParam);
295     }
296     return 0;
297 }
让数据变得更安全!
原文地址:https://www.cnblogs.com/Alyoyojie/p/5262556.html