3.1.2 Win32_Code

  1. // 窗口搭建**
  2. // wincreate.cpp : Defines the entry point for the application.
  3. \#include "stdafx.h"
  4. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  5. //窗口处理函数
  6. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  7. {
  8. switch ( nMsg )
  9. {
  10. case WM_DESTROY:
  11. PostQuitMessage( 0 );//能够使getMessage返回0
  12. break;
  13. }
  14. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  15. }
  16. //注册窗口类
  17. void Register(LPSTR lpClassName,WNDPROC wndproc )
  18. {
  19. WNDCLASSEX wce = { 0 };
  20. wce.cbSize = sizeof( wce ); //结构体的大小
  21. wce.cbClsExtra = 0;//窗口类缓冲区
  22. wce.cbWndExtra = 0;//窗口缓冲区
  23. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  24. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  25. wce.hIcon = NULL;//窗口大图标句柄
  26. wce.hIconSm = NULL;//窗口小图标句柄
  27. wce.hInstance = g_hInstance;//当前模块句柄
  28. wce.lpfnWndProc = wndproc;//窗口处理函数
  29. wce.lpszClassName = lpClassName;//窗口类名称
  30. wce.lpszMenuName = NULL;//菜单名
  31. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  32. RegisterClassEx( &wce );//注册,向系统写入
  33. }
  34. //创建主窗口
  35. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  36. {
  37. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  38. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  39. NULL );
  40. return hWnd;
  41. }
  42. //显示(绘制)窗口
  43. void Display(HWND hWnd )
  44. {
  45. ShowWindow( hWnd,SW_SHOW );
  46. UpdateWindow( hWnd);
  47. }
  48. //消息循环
  49. void Message()
  50. {
  51. MSG uMsg = { 0 };
  52. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  53. {
  54. TranslateMessage( &uMsg ); // 翻译,分析
  55. DispatchMessage( &uMsg ); // 调度,派发
  56. }
  57. }
  58. int APIENTRY WinMain(HINSTANCE hInstance,
  59. HINSTANCE hPrevInstance,
  60. LPSTR lpCmdLine,
  61. int nCmdShow)
  62. {
  63. g_hInstance = hInstance;
  64. Register("Main",WndProc);
  65. HWND hWnd = CreateMain( "Main","Windows");
  66. Display( hWnd);
  67. Message();
  68. return 0;
  69. }
  70. **//窗口附加数据、子窗口**
  71. // wincreate.cpp : Defines the entry point for the application.
  72. //
  73. \#include "stdafx.h"
  74. \#include "stdio.h"
  75. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  76. //窗口处理函数(处理消息)
  77. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  78. {
  79. switch ( nMsg )
  80. {
  81. case WM_SYSCOMMAND:
  82. MessageBox( hWnd, "WM_SYSCOMMAND", "Infor",MB_OK );
  83. break;
  84. case WM_DESTROY:
  85. PostQuitMessage( 0 );//能够使getMessage返回0
  86. break;
  87. }
  88. return DefWindowProc( hWnd, nMsg, wParam,lParam );//默认处理函数
  89. }
  90. //注册窗口类
  91. void Register(LPSTR lpClassName,WNDPROC wndproc )
  92. {
  93. WNDCLASSEX wce = { 0 };
  94. wce.cbSize = sizeof( wce ); //结构体的大小
  95. wce.cbClsExtra = 200;//窗口类缓冲区
  96. wce.cbWndExtra = 200;//窗口缓冲区
  97. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  98. wce.hCursor = NULL;//光标
  99. wce.hIcon = NULL;//窗口大图标句柄
  100. wce.hIconSm = NULL;//窗口小图标句柄
  101. wce.hInstance = g_hInstance;//当前模块句柄
  102. wce.lpfnWndProc = wndproc;//窗口处理函数
  103. wce.lpszClassName = lpClassName;//窗口类名称
  104. wce.lpszMenuName = NULL;//菜单名
  105. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  106. RegisterClassEx( &wce );// 向系统写入,注册
  107. }
  108. //创建主窗口
  109. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  110. {
  111. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  112. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  113. NULL );
  114. return hWnd;
  115. }
  116. //创建子窗口
  117. HWND CreateChild( LPSTR lpClassName,LPSTR lpWndName,HWND hParent )
  118. {
  119. HWND hChild = CreateWindowEx( 0,lpClassName, lpWndName,
  120. WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,100,100,200,200,hParent,
  121. NULL,g_hInstance,NULL);
  122. return hChild;
  123. }
  124. //显示(绘制)窗口
  125. void Display(HWND hWnd )
  126. {
  127. ShowWindow( hWnd,SW_SHOW );
  128. UpdateWindow( hWnd);
  129. }
  130. //消息循环
  131. void Message()
  132. {
  133. MSG uMsg = { 0 };
  134. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  135. {
  136. TranslateMessage( &uMsg ); // 翻译,分析
  137. DispatchMessage( &uMsg ); // 调度,派发
  138. }
  139. }
  140. //写入附加数据
  141. void SetExtra( HWND hWnd )
  142. {
  143. char *pszText = "hello data";
  144. SetClassLong( hWnd, 196, (LONG)pszText);
  145. SetWindowLong( hWnd, 0, 1000);
  146. }
  147. //读入附件数据
  148. void GetExtra( HWND hWnd )
  149. {
  150. LONG nClassExtra = GetClassLong(hWnd, 196);
  151. char* pszText = (char*)nClassExtra;
  152. LONG nWndExtra = GetWindowLong( hWnd, 0);
  153. CHAR seText[256] = { 0 };
  154. sprintf( seText, "窗口类:%s,窗口:%d\n",pszText,nWndExtra);
  155. MessageBox( NULL, seText,"Infor", MB_OK ); //提示框(显示)
  156. }
  157. int APIENTRY WinMain(HINSTANCE hInstance,
  158. HINSTANCE hPrevInstance,
  159. LPSTR lpCmdLine,
  160. int nCmdShow)
  161. {
  162. g_hInstance = hInstance;
  163. Register("Main",WndProc);
  164. HWND hWnd = CreateMain( "Main","WINDOWS");
  165. Register( "CHILD",DefWindowProc);
  166. HWND hChildl = CreateChild("CHILD", "C1", hWnd );
  167. HWND hChild2 = CreateChild("CHILD", "C2", hWnd );
  168. //窗口重绘
  169. MoveWindow(hChildl, 300,100,200,200,TRUE);
  170. MoveWindow(hChild2, 500,100,200,200,TRUE);
  171. SetExtra( hChildl );
  172. GetExtra( hChild2 );
  173. Display( hWnd);
  174. Message();
  175. return 0;
  176. }
  177. **//** **窗口消息**
  178. \#include "stdafx.h"
  179. \#include "stdio.h"
  180. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  181. HANDLE g_hOutput = 0;//接收标准输出句柄
  182. HWND g_hEdit = 0;//用于接收窗口句柄(MoveMessage())
  183. \#define WM_MYMESSAGE WM_USER + 1001 //自定义消息(宏)
  184. //WM_CREATE 消息(创建子窗口)
  185. void OnCreate( HWND hWnd, LPARAM lParam)
  186. {
  187. CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
  188. char* pszText = (char*)pcs->lpCreateParams;
  189. MessageBox( hWnd,pszText, "Infor",MB_OK );
  190. g_hEdit = CreateWindowEx( 0, "EDIT","HELLO",WS_CHILD|WS_VISIBLE|WS_BORDER,
  191. 0,0,200,200,hWnd,NULL,g_hInstance,NULL );
  192. //向自定义消息发送
  193. PostMessage( hWnd, WM_MYMESSAGE, 1,2 );
  194. }
  195. //WM_SIZE 消息
  196. void OnSize(HWND hWnd, LPARAM lParam )
  197. {
  198. char szText[256] = { 0 };
  199. sprintf( szText, "WM_SIZE:宽=%d,高=%d\n",LOWORD(lParam),HIWORD(lParam));
  200. WriteConsole( g_hOutput,szText, strlen(szText),NULL,NULL );
  201. MoveWindow( g_hEdit,0,0,LOWORD(lParam)-100,HIWORD(lParam)-100, TRUE );//重新绘制子窗口
  202. //char *pszText = "WM_SIZE\n";
  203. //WriteConsole( g_hOutput,pszText, strlen(pszText),NULL,NULL );
  204. }
  205. //窗口处理函数
  206. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  207. {
  208. switch ( nMsg )
  209. {
  210. case WM_MYMESSAGE: //处理自定义消息
  211. {
  212. char szText[256] = { 0 };
  213. sprintf( szText,"wParam=%d,lParam=%d",wParam,lParam );
  214. MessageBox( hWnd, szText, "Infor", MB_OK );
  215. }
  216. case WM_SIZE:
  217. OnSize( hWnd, lParam);
  218. break;
  219. case WM_CREATE:
  220. OnCreate( hWnd, lParam );
  221. break;
  222. case WM_DESTROY:
  223. // PostQuitMessage( 0 );//能够使getMessage返回0 //参数随意
  224. // SendMessage( hWnd, WM_QUIT, 0, 0 );
  225. PostMessage( hWnd, WM_QUIT, 0, 0 );
  226. break;
  227. }
  228. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  229. }
  230. //注册窗口类
  231. void Register(LPSTR lpClassName,WNDPROC wndproc )
  232. {
  233. WNDCLASSEX wce = { 0 };
  234. wce.cbSize = sizeof( wce ); //结构体的大小
  235. wce.cbClsExtra = 0;//窗口类缓冲区
  236. wce.cbWndExtra = 0;//窗口缓冲区
  237. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  238. wce.hCursor = NULL;//光标
  239. wce.hIcon = NULL;//窗口大图标句柄
  240. wce.hIconSm = NULL;//窗口小图标句柄
  241. wce.hInstance = g_hInstance;//当前模块句柄
  242. wce.lpfnWndProc = wndproc;//窗口处理函数
  243. wce.lpszClassName = lpClassName;//窗口类名称
  244. wce.lpszMenuName = NULL;//菜单名
  245. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  246. RegisterClassEx( &wce );
  247. }
  248. //创建主窗口
  249. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  250. {
  251. char *pszText = "hello data";
  252. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  253. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  254. NULL,NULL,g_hInstance,pszText );
  255. return hWnd;
  256. }
  257. //显示(绘制)窗口
  258. void Display(HWND hWnd )
  259. {
  260. ShowWindow( hWnd,SW_SHOW );
  261. UpdateWindow( hWnd);
  262. }
  263. //消息循环
  264. void Message()
  265. {
  266. MSG nMsg = { 0 };
  267. /*
  268. ​ while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  269. ​ {
  270. ​ TranslateMessage( &uMsg ); // 翻译,分析
  271. ​ DispatchMessage( &uMsg ); // 调度,派发
  272. ​ }
  273. */
  274. while(1)
  275. {
  276. if(PeekMessage(&nMsg,0,NULL,NULL,PM_NOREMOVE))//侦查作用
  277. {
  278. //有消息
  279. if(GetMessage( &nMsg,0,NULL,NULL))
  280. {
  281. TranslateMessage( &nMsg );
  282. DispatchMessage( &nMsg );
  283. }
  284. else
  285. {
  286. //WM_QUIT
  287. return;
  288. }
  289. }
  290. else
  291. {
  292. //没消息
  293. //空闲处理
  294. WriteConsole( g_hOutput, "OnIdle",6,NULL,NULL);
  295. }
  296. }
  297. }
  298. //主函数
  299. int APIENTRY WinMain(HINSTANCE hInstance,
  300. HINSTANCE hPrevInstance,
  301. LPSTR lpCmdLine,
  302. int nCmdShow)
  303. {
  304. AllocConsole();//dos窗口
  305. g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );//获得dos窗口的句柄
  306. g_hInstance = hInstance;
  307. Register("Main",WndProc);
  308. HWND hWnd = CreateMain( "Main","WINDOWS");
  309. Display( hWnd);
  310. Message();
  311. return 0;
  312. }
  313. **//****键盘消息**
  314. \#include "stdafx.h"
  315. \#include "stdio.h"
  316. HANDLE g_hOutput = 0;//用于(dos)标准输出句柄
  317. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  318. int g_xPos = 100; //记录坐标
  319. int g_yPos = 100;
  320. // VM_PAINT 绘制消息
  321. void OnPaint( HWND hWnd)
  322. {
  323. char *pszText = "WM_PAINT\n";
  324. //WriteConsole( g_hOutput, pszText, strlen(pszText),NULL,NULL );
  325. //绘图
  326. PAINTSTRUCT ps = { 0 };
  327. HDC hdc = BeginPaint( hWnd, &ps);
  328. TextOut( hdc, g_xPos, g_yPos,"hello",strlen("hello"));//画字符串
  329. EndPaint( hWnd, &ps );
  330. }
  331. //键盘消息(按下/松开)
  332. void OnKeyDown( HWND hWnd, WPARAM wParam )
  333. {
  334. char szText[256]={0};
  335. sprintf( szText, "WM_KEYDOWN:%08X\n",wParam);
  336. WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );
  337. switch( wParam )
  338. {
  339. case VK_UP://虚拟键码
  340. g_yPos--;
  341. break;
  342. case VK_DOWN://虚拟键码
  343. g_yPos++;
  344. break;
  345. case VK_LEFT://虚拟键码
  346. g_xPos--;
  347. break;
  348. case VK_RIGHT://虚拟键码
  349. g_xPos++;
  350. break;
  351. }
  352. InvalidateRect( hWnd,NULL, TRUE );//重画
  353. }
  354. void OnKeyUp( HWND hWnd, WPARAM wParam )
  355. {
  356. char szText[256]={0};
  357. sprintf( szText, "WM_KEYUP:%08X\n",wParam);
  358. WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );
  359. }
  360. // WM_CHAR消息
  361. void OnChar( HWND hWnd, WPARAM wParam )
  362. {
  363. char szText[256]={ 0 };
  364. sprintf( szText, "WM_CHAR:%08X\n",wParam);
  365. WriteConsole( g_hOutput,szText,strlen(szText),NULL,NULL );
  366. }
  367. //窗口处理函数
  368. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  369. {
  370. switch ( nMsg )
  371. {
  372. case WM_CHAR:
  373. OnChar( hWnd,wParam );
  374. break;
  375. case WM_KEYDOWN:
  376. OnKeyDown( hWnd, wParam );
  377. break;
  378. case WM_KEYUP:
  379. OnKeyUp( hWnd, wParam );
  380. break;
  381. case WM_LBUTTONDOWN://申明重绘消息
  382. InvalidateRect( hWnd,NULL,TRUE );
  383. case WM_PAINT: //重绘消息
  384. OnPaint( hWnd );
  385. break;
  386. case WM_DESTROY:
  387. PostQuitMessage( 0 );//能够使getMessage返回0从而可以退出
  388. break;
  389. }
  390. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  391. }
  392. //注册窗口类
  393. void Register(LPSTR lpClassName,WNDPROC wndproc )
  394. {
  395. WNDCLASSEX wce = { 0 };
  396. wce.cbSize = sizeof( wce ); //结构体的大小
  397. wce.cbClsExtra = 0;//窗口类缓冲区
  398. wce.cbWndExtra = 0;//窗口缓冲区
  399. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  400. wce.hCursor = NULL;//光标
  401. wce.hIcon = NULL;//窗口大图标句柄
  402. wce.hIconSm = NULL;//窗口小图标句柄
  403. wce.hInstance = g_hInstance;//当前模块句柄
  404. wce.lpfnWndProc = wndproc;//窗口处理函数
  405. wce.lpszClassName = lpClassName;//窗口类名称
  406. wce.lpszMenuName = NULL;//菜单名
  407. // wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  408. wce.style = 0;//0非强制重绘
  409. RegisterClassEx( &wce );//注册,向系统写入
  410. }
  411. //创建主窗口
  412. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  413. {
  414. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  415. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  416. NULL );
  417. return hWnd;
  418. }
  419. //显示(绘制)窗口
  420. void Display(HWND hWnd )
  421. {
  422. ShowWindow( hWnd,SW_SHOW );
  423. UpdateWindow( hWnd);
  424. }
  425. //消息循环
  426. void Message()
  427. {
  428. MSG uMsg = { 0 };
  429. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  430. {
  431. TranslateMessage( &uMsg ); // 翻译,分析
  432. DispatchMessage( &uMsg ); // 调度,派发
  433. }
  434. }
  435. int APIENTRY WinMain(HINSTANCE hInstance,
  436. HINSTANCE hPrevInstance,
  437. LPSTR lpCmdLine,
  438. int nCmdShow)
  439. {
  440. AllocConsole();
  441. g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );//调用dos窗口
  442. g_hInstance = hInstance;//接收窗口句柄
  443. Register("Main",WndProc);
  444. HWND hWnd = CreateMain( "Main","WINDOWS");
  445. Display( hWnd);
  446. Message();
  447. return 0;
  448. }
  449. **//****鼠标消息**
  450. \#include "stdafx.h"
  451. \#include "stdio.h"
  452. HANDLE g_hOutput = 0;
  453. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  454. int g_yPos = 100;
  455. int g_xPos = 100;
  456. //鼠标按键事件
  457. void OnLButtonDown( HWND hWnd, WPARAM wParam,LPARAM lParam)
  458. {
  459. char szText[256] = { 0 };
  460. sprintf( szText,"WM_LBUTTOONDOWN:按键状态=%08X, x=%d,y=%d\n",
  461. wParam, LOWORD(lParam), HIWORD(lParam) );
  462. WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);
  463. }
  464. void OnLButtonUp( HWND hWnd, WPARAM wParam,LPARAM lParam)
  465. {
  466. char szText[256] = { 0 };
  467. sprintf( szText,"WM_LBUTTOONUP:按键状态=%08X, x=%d,y=%d\n",
  468. wParam, LOWORD(lParam), HIWORD(lParam) );
  469. WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);
  470. }
  471. //鼠标移动事件
  472. void OnMouseMove( HWND hWnd, LPARAM lParam)
  473. {
  474. char szText[256] = { 0 };
  475. sprintf( szText,"WM_MOUSEMoVE:x=%d,y=%d\n",LOWORD(lParam), HIWORD(lParam) );
  476. // WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL);
  477. g_xPos = LOWORD(lParam);
  478. g_yPos = HIWORD(lParam);
  479. InvalidateRect( hWnd, NULL, TRUE );
  480. }
  481. //绘制函数
  482. void OnPaint( HWND hWnd)
  483. {
  484. PAINTSTRUCT ps = { 0 };
  485. HDC hdc = BeginPaint( hWnd, &ps);
  486. TextOut( hdc,g_xPos,g_yPos,"hello",strlen("hello"));//画字符串
  487. EndPaint( hWnd, &ps );
  488. }
  489. //鼠标双击事件
  490. void OnLButtonDblClk( HWND hWnd )
  491. {
  492. char *pszText = "WM_LBUTTONDBLCLK\n";
  493. WriteConsole( g_hOutput,pszText,strlen(pszText),NULL,NULL );
  494. }
  495. void OnMouseWheel( HWND hWnd, WPARAM wParam )
  496. {
  497. short nDelta = HIWORD( wParam );
  498. char szText[256] = { 0 };
  499. sprintf( szText, "偏移量:%d\n",nDelta );
  500. WriteConsole( g_hOutput, szText,strlen(szText),NULL,NULL );
  501. }
  502. //窗口处理函数
  503. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  504. {
  505. switch ( nMsg )
  506. {
  507. case WM_MOUSEWHEEL:
  508. OnMouseWheel( hWnd, wParam );
  509. break;
  510. case WM_LBUTTONDBLCLK:
  511. OnLButtonDblClk( hWnd );
  512. break;
  513. case WM_PAINT:
  514. OnPaint( hWnd );
  515. break;
  516. case WM_MOUSEMOVE:
  517. OnMouseMove( hWnd,lParam);
  518. break;
  519. case WM_LBUTTONDOWN:
  520. OnLButtonDown( hWnd, wParam,lParam );
  521. break;
  522. case WM_LBUTTONUP:
  523. OnLButtonUp( hWnd, wParam, lParam );
  524. break;
  525. case WM_DESTROY:
  526. PostQuitMessage( 0 );//能够使getMessage返回0
  527. break;
  528. }
  529. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  530. }
  531. //注册窗口类
  532. void Register(LPSTR lpClassName,WNDPROC wndproc )
  533. {
  534. WNDCLASSEX wce = { 0 };
  535. wce.cbSize = sizeof( wce ); //结构体的大小
  536. wce.cbClsExtra = 0;//窗口类缓冲区
  537. wce.cbWndExtra = 0;//窗口缓冲区
  538. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  539. wce.hCursor = NULL;//光标
  540. wce.hIcon = NULL;//窗口大图标句柄
  541. wce.hIconSm = NULL;//窗口小图标句柄
  542. wce.hInstance = g_hInstance;//当前模块句柄
  543. wce.lpfnWndProc = wndproc;//窗口处理函数
  544. wce.lpszClassName = lpClassName;//窗口类名称
  545. wce.lpszMenuName = NULL;//菜单名
  546. wce.style = CS_VREDRAW | CS_VREDRAW | CS_DBLCLKS;//窗口风格
  547. //wce.style = 0;
  548. RegisterClassEx( &wce );//注册,向系统写入
  549. }
  550. //创建主窗口
  551. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  552. {
  553. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  554. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  555. NULL );
  556. return hWnd;
  557. }
  558. //显示(绘制)窗口
  559. void Display(HWND hWnd )
  560. {
  561. ShowWindow( hWnd,SW_SHOW );
  562. UpdateWindow( hWnd);
  563. }
  564. //消息循环
  565. void Message()
  566. {
  567. MSG uMsg = { 0 };
  568. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  569. {
  570. TranslateMessage( &uMsg ); // 翻译,分析
  571. DispatchMessage( &uMsg ); // 调度,派发
  572. }
  573. }
  574. int APIENTRY WinMain(HINSTANCE hInstance,
  575. HINSTANCE hPrevInstance,
  576. LPSTR lpCmdLine,
  577. int nCmdShow)
  578. {
  579. AllocConsole();
  580. g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );
  581. g_hInstance = hInstance;
  582. Register("Main",WndProc);
  583. HWND hWnd = CreateMain( "Main","WINDOWS");
  584. Display( hWnd);
  585. Message();
  586. return 0;
  587. }
  588. **//****定时器消息**
  589. \#include "stdafx.h"
  590. \#include "stdio.h"
  591. int g_xPos = 0;
  592. int g_yPos = 0;
  593. BOOL g_Right = TRUE; //g_left = FALSE;
  594. BOOL g_Bottom = TRUE;//g_Top = FALSE;
  595. //int nRightRect = 110;
  596. //int nBottomRect = 110;
  597. //nRightRect = nLeftRect+60;
  598. //nBottomRect = nTopRect+60;
  599. HANDLE g_hOutput = 0;
  600. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  601. //定时器处理函数
  602. void OnTimer( HWND hWnd, WPARAM wParam )
  603. {
  604. // char szText[256]={0};
  605. // sprintf( szText, "定时器:%d\n",wParam );
  606. // WriteConsole( g_hOutput, szText, strlen(szText),NULL,NULL );
  607. //获得边框大小
  608. RECT rc ={0};
  609. GetClientRect( hWnd, &rc);
  610. if(g_Right)
  611. {
  612. if( g_xPos+50 >= rc.right )
  613. {
  614. g_Right = FALSE;
  615. }
  616. else
  617. {
  618. g_xPos++;
  619. }
  620. }
  621. else
  622. {
  623. if ( g_xPos <= rc.left )
  624. {
  625. g_Right = TRUE;
  626. }
  627. else
  628. {
  629. g_xPos--;
  630. }
  631. }
  632. if ( g_Bottom)
  633. {
  634. if ( g_yPos+50 <= rc.bottom)
  635. {
  636. g_yPos++;
  637. }
  638. else
  639. {
  640. g_Bottom = FALSE;
  641. }
  642. }
  643. else
  644. {
  645. if ( g_yPos >= rc.top)
  646. {
  647. g_yPos--;
  648. }
  649. else
  650. {
  651. g_Bottom = TRUE;
  652. }
  653. }
  654. // InvalidateRect( hWnd, NULL,TRUE);
  655. InvalidateRect( hWnd, NULL,FALSE);
  656. }
  657. //自定义定时器处理函数
  658. void CALLBACK TimerProc ( HWND hWnd, UINT nMsg,UINT idTimer,DWORD dwCur )
  659. {
  660. char szText[256] = { 0 };
  661. sprintf( szText, "定时器处理函数处理:%d\n",idTimer);
  662. WriteConsole( g_hOutput, szText,strlen(szText),NULL,NULL );
  663. }
  664. //绘图函数
  665. void OnPaint( HWND hWnd)
  666. {
  667. PAINTSTRUCT ps = { 0 };
  668. HDC hdc = BeginPaint( hWnd,&ps);
  669. Ellipse ( hdc, g_xPos,g_yPos,g_xPos+50,g_yPos+50 );
  670. EndPaint ( hWnd, &ps);
  671. }
  672. //窗口处理函数
  673. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  674. {
  675. switch ( nMsg )
  676. {
  677. case WM_LBUTTONDOWN:
  678. KillTimer( hWnd ,1);//左键停止
  679. break;
  680. case WM_PAINT:
  681. OnPaint( hWnd );
  682. break;
  683. case WM_TIMER://触发定时器
  684. OnTimer( hWnd, wParam );
  685. break;
  686. case WM_CREATE:
  687. {
  688. //放定时器
  689. SetTimer( hWnd, 1, 10, NULL );
  690. // SetTimer( hWnd, 2, 2000, TimerProc );
  691. break;
  692. }
  693. case WM_DESTROY:
  694. PostQuitMessage( 0 );//能够使getMessage返回0
  695. break;
  696. }
  697. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  698. }
  699. //注册窗口类
  700. void Register(LPSTR lpClassName,WNDPROC wndproc )
  701. {
  702. WNDCLASSEX wce = { 0 };
  703. wce.cbSize = sizeof( wce ); //结构体的大小
  704. wce.cbClsExtra = 0;//窗口类缓冲区
  705. wce.cbWndExtra = 0;//窗口缓冲区
  706. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  707. wce.hCursor = NULL;//光标
  708. wce.hIcon = NULL;//窗口大图标句柄
  709. wce.hIconSm = NULL;//窗口小图标句柄
  710. wce.hInstance = g_hInstance;//当前模块句柄
  711. wce.lpfnWndProc = wndproc;//窗口处理函数
  712. wce.lpszClassName = lpClassName;//窗口类名称
  713. wce.lpszMenuName = NULL;//菜单名
  714. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  715. RegisterClassEx( &wce );//注册,向系统写入
  716. }
  717. //创建主窗口
  718. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  719. {
  720. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  721. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  722. NULL );
  723. return hWnd;
  724. }
  725. //显示(绘制)窗口
  726. void Display(HWND hWnd )
  727. {
  728. ShowWindow( hWnd,SW_SHOW );
  729. UpdateWindow( hWnd);
  730. }
  731. //消息循环
  732. void Message()
  733. {
  734. MSG uMsg = { 0 };
  735. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  736. {
  737. TranslateMessage( &uMsg ); // 翻译,分析
  738. DispatchMessage( &uMsg ); // 调度,派发
  739. }
  740. }
  741. int APIENTRY WinMain(HINSTANCE hInstance,
  742. HINSTANCE hPrevInstance,
  743. LPSTR lpCmdLine,
  744. int nCmdShow)
  745. {
  746. AllocConsole();
  747. g_hOutput = GetStdHandle( STD_OUTPUT_HANDLE );
  748. g_hInstance = hInstance;
  749. Register("Main",WndProc);
  750. HWND hWnd = CreateMain( "Main","WINDOWS");
  751. Display( hWnd);
  752. Message();
  753. return 0;
  754. }
  755. **//****创建菜单项**
  756. \#include "stdafx.h"
  757. \#include "stdio.h"
  758. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  759. HMENU hFile = 0;
  760. HANDLE g_hOutput = 0;
  761. //构建菜单
  762. void OnCreate ( HWND hWnd )
  763. {
  764. HMENU hMain = CreateMenu();
  765. hFile = CreatePopupMenu();
  766. AppendMenu ( hFile, MF_STRING|MF_CHECKED, 1003, "新建");
  767. AppendMenu ( hFile, MF_SEPARATOR, 0, "");// 分割线
  768. AppendMenu ( hFile, MF_STRING|MF_MENUBARBREAK, 1004, "退出");//并排
  769. HMENU hHelp = CreatePopupMenu();
  770. AppendMenu( hHelp,MF_STRING|MF_GRAYED,1005, "关于");
  771. AppendMenu ( hMain, MF_POPUP, (UINT)hFile, "文件");
  772. AppendMenu ( hMain, MF_POPUP, (UINT)hHelp, "帮助");
  773. SetMenu( hWnd, hMain ); //设置
  774. }
  775. //功能实现
  776. int g_state = 0;//全局变量(标志量)
  777. void OnCommand( HWND hWnd,WPARAM wParam )
  778. {
  779. switch( LOWORD(wParam) )
  780. {
  781. case 1003:
  782. if( g_state == 0 )
  783. {
  784. CheckMenuItem( hFile,0,MF_BYPOSITION|MF_UNCHECKED );
  785. }
  786. else
  787. {
  788. CheckMenuItem( hFile, 0,MF_BYPOSITION|MF_CHECKED );//菜单项位置
  789. }
  790. g_state = !g_state;//反向
  791. break;
  792. case 1004:
  793. MessageBox( hWnd, "退出被点击", "Infor",MB_OK );
  794. break;
  795. case 1005:
  796. MessageBox( hWnd, "关于被点击", "Infor",MB_OK );
  797. break;
  798. }
  799. }
  800. //附带信息
  801. void OnInitMenuPopup( HWND hWnd, WPARAM wParam, LPARAM lParam )
  802. {
  803. char szText[256] = { 0 };
  804. sprintf( szText,"即将显示菜单:%d,被点击的顶层菜单项索引:%d,\
  805. ​ 即将显示的菜单是否为窗口菜单:%d\n",wParam,LOWORD(lParam),HIWORD(lParam) );
  806. WriteConsole( g_hOutput,szText, strlen(szText),NULL,NULL );
  807. }
  808. //窗口处理函数
  809. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  810. {
  811. switch ( nMsg )
  812. {
  813. case WM_INITMENUPOPUP:
  814. OnInitMenuPopup( hWnd, wParam, lParam );
  815. break;
  816. case WM_COMMAND:
  817. OnCommand( hWnd, wParam );
  818. break;
  819. case WM_CREATE:
  820. OnCreate( hWnd );
  821. break;
  822. case WM_DESTROY:
  823. PostQuitMessage( 0 );//能够使getMessage返回0
  824. break;
  825. }
  826. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  827. }
  828. //注册窗口类
  829. void Register(LPSTR lpClassName,WNDPROC wndproc )
  830. {
  831. WNDCLASSEX wce = { 0 };
  832. wce.cbSize = sizeof( wce ); //结构体的大小
  833. wce.cbClsExtra = 0;//窗口类缓冲区
  834. wce.cbWndExtra = 0;//窗口缓冲区
  835. wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);//背景色
  836. wce.hCursor = NULL;//光标
  837. wce.hIcon = NULL;//窗口大图标句柄
  838. wce.hIconSm = NULL;//窗口小图标句柄
  839. wce.hInstance = g_hInstance;//当前模块句柄
  840. wce.lpfnWndProc = wndproc;//窗口处理函数
  841. wce.lpszClassName = lpClassName;//窗口类名称
  842. wce.lpszMenuName = NULL;//菜单名
  843. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  844. RegisterClassEx( &wce );//注册,向系统写入
  845. }
  846. //创建主窗口
  847. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  848. {
  849. AllocConsole();
  850. g_hOutput = GetStdHandle ( STD_OUTPUT_HANDLE );
  851. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  852. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  853. NULL );
  854. return hWnd;
  855. }
  856. //显示(绘制)窗口
  857. void Display(HWND hWnd )
  858. {
  859. ShowWindow( hWnd,SW_SHOW );
  860. UpdateWindow( hWnd);
  861. }
  862. //消息循环
  863. void Message()
  864. {
  865. MSG uMsg = { 0 };
  866. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  867. {
  868. TranslateMessage( &uMsg ); // 翻译,分析
  869. DispatchMessage( &uMsg ); // 调度,派发
  870. }
  871. }
  872. int APIENTRY WinMain(HINSTANCE hInstance,
  873. HINSTANCE hPrevInstance,
  874. LPSTR lpCmdLine,
  875. int nCmdShow)
  876. {
  877. g_hInstance = hInstance;
  878. Register("Main",WndProc);
  879. HWND hWnd = CreateMain( "Main","WINDOWS");
  880. Display( hWnd);
  881. Message();
  882. return 0;
  883. }
  884. **//****绘图**
  885. \#include "stdafx.h"
  886. \#include "resource.h"
  887. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  888. int g_kind = 0; // 标志量
  889. void OnCommand(HWND hWnd,WPARAM wParam)
  890. {
  891. g_kind = LOWORD(wParam);
  892. switch(LOWORD(wParam))
  893. {
  894. case ID_PIT:
  895. case ID_LINE:
  896. case ID_REC:
  897. case ID_ELL:
  898. case ID_ARC:
  899. //MessageBox(hWnd,"CTRL+Y被点击","Infor",MB_OK);
  900. InvalidateRect( hWnd,NULL,TRUE );//重绘
  901. break;
  902. }
  903. }
  904. //绘制点
  905. void DrawPit(HDC hdc)
  906. {
  907. //SetPixel(hdc,100,100,RGB(255,0,0));//点
  908. for(int i=0; i<256; i++ )//面
  909. {
  910. for(int j=0; j<256;j++ )
  911. {
  912. SetPixel(hdc,i,j,RGB(256,0,0));
  913. }
  914. }
  915. }
  916. //绘制线
  917. void DrawLine(HDC hdc)
  918. {
  919. MoveToEx(hdc,100,100,NULL);//设置当前点
  920. LineTo(hdc,300,300);//绘线
  921. LineTo(hdc,200,500);//接着画
  922. }
  923. //绘制矩形
  924. void DrawRec( HDC hdc)
  925. {
  926. Rectangle(hdc,100,100,300,300);//矩形
  927. //RoundRect(hdc,300,100,500,300,50,50);//圆角矩形
  928. RoundRect(hdc,300,100,500,300,200,200);//变成圆了
  929. }
  930. //绘制圆形
  931. void DrawEll(HDC hdc)
  932. {
  933. Ellipse(hdc,100,100,300,300);//圆
  934. Ellipse(hdc,300,100,600,300);//椭圆
  935. }
  936. //绘制弧线
  937. void DrawArc(HDC hdc)
  938. {
  939. SetArcDirection(hdc,AD_CLOCKWISE);//顺时针//设置取弧规则
  940. Arc(hdc,100,100,300,300,100,100,300,300);//画圆(默认逆时针取弧)
  941. }
  942. //绘图
  943. void OnPaint(HWND hWnd)
  944. {
  945. PAINTSTRUCT ps = { 0 };
  946. HDC hdc = BeginPaint(hWnd,&ps);
  947. //画笔
  948. //HPEN hPen = CreatePen(PS_SOLID,100,RGB(255,0,0));//做笔//实线
  949. HPEN hPen = CreatePen(PS_DASH,1,RGB(255,0,0));//做笔//虚线(必须一个像素宽)
  950. HGDIOBJ nOldPen = SelectObject(hdc,hPen);//送笔
  951. //画刷
  952. //HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));//实心画刷(填充单一色)
  953. //HBRUSH hBrush = CreateHatchBrush(HS_CROSS,RGB(0,255,0));//填充阴影线
  954. //HGDIOBJ hBrush = GetStockObject(NULL_BRUSH);//透明画刷(从系统获取)
  955. //位图
  956. HBITMAP hBmp = LoadBitmap( g_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
  957. HBRUSH hBrush = CreatePatternBrush( hBmp );
  958. HGDIOBJ nOldBrush = SelectObject(hdc,hBrush);//送刷
  959. switch(g_kind)
  960. {
  961. case ID_ARC:
  962. DrawArc(hdc);//绘制弧线
  963. break;
  964. case ID_ELL:
  965. DrawEll(hdc);//绘制圆形
  966. break;
  967. case ID_REC:
  968. DrawRec(hdc);//绘制矩形
  969. break;
  970. case ID_PIT:
  971. DrawPit(hdc);//绘制点
  972. break;
  973. case ID_LINE:
  974. DrawLine(hdc);//绘制线
  975. break;
  976. }
  977. SelectObject(hdc,nOldPen);//换回画笔
  978. DeleteObject( hPen);//释放画笔
  979. SelectObject(hdc,nOldBrush);//换回画刷
  980. DeleteObject( hBrush );//释放画刷
  981. EndPaint(hWnd,&ps);
  982. }
  983. //窗口处理函数
  984. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  985. {
  986. switch ( nMsg )
  987. {
  988. case WM_COMMAND:
  989. OnCommand(hWnd,wParam);
  990. break;
  991. case WM_DESTROY:
  992. PostQuitMessage( 0 );//能够使getMessage返回0
  993. break;
  994. }
  995. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  996. }
  997. //注册窗口类
  998. void Register(LPSTR lpClassName,WNDPROC wndproc )
  999. {
  1000. WNDCLASSEX wce = { 0 };
  1001. wce.cbSize = sizeof( wce ); //结构体的大小
  1002. wce.cbClsExtra = 0;//窗口类缓冲区
  1003. wce.cbWndExtra = 0;//窗口缓冲区
  1004. wce.hbrBackground =
  1005. CreateSolidBrush(RGB(0,0,255));//(HBRUSH)(COLOR_WINDOW+1);//背景色
  1006. wce.hCursor = NULL;//光标
  1007. wce.hIcon = NULL;//窗口大图标句柄
  1008. wce.hIconSm = NULL;//窗口小图标句柄
  1009. wce.hInstance = g_hInstance;//当前模块句柄
  1010. wce.lpfnWndProc = wndproc;//窗口处理函数
  1011. wce.lpszClassName = lpClassName;//窗口类名称
  1012. wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);//菜单名
  1013. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  1014. RegisterClassEx( &wce );//注册,向系统写入
  1015. }
  1016. //创建主窗口
  1017. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  1018. {
  1019. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  1020. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  1021. NULL );
  1022. return hWnd;
  1023. }
  1024. //显示(绘制)窗口
  1025. void Display(HWND hWnd )
  1026. {
  1027. ShowWindow( hWnd,SW_SHOW );
  1028. UpdateWindow( hWnd);
  1029. }
  1030. //消息循环
  1031. void Message()
  1032. {
  1033. MSG uMsg = { 0 };
  1034. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  1035. {
  1036. TranslateMessage( &uMsg ); // 翻译,分析
  1037. DispatchMessage( &uMsg ); // 调度,派发
  1038. }
  1039. }
  1040. int APIENTRY WinMain(HINSTANCE hInstance,
  1041. HINSTANCE hPrevInstance,
  1042. LPSTR lpCmdLine,
  1043. int nCmdShow)
  1044. {
  1045. g_hInstance = hInstance;
  1046. Register("Main",WndProc);
  1047. HWND hWnd = CreateMain( "Main","WINDOWS");
  1048. Display( hWnd);
  1049. Message();
  1050. return 0;
  1051. }
  1052. **//****对话框**
  1053. \#include "stdafx.h"
  1054. \#include "resource.h"
  1055. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  1056. //模式对话框过程函数
  1057. BOOL CALLBACK ModalDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
  1058. {
  1059. switch (uMsg)
  1060. {
  1061. case WM_INITDIALOG:
  1062. SetWindowText(hDlg,"模式对话框");
  1063. return TRUE;
  1064. case WM_COMMAND:
  1065. switch(LOWORD(wParam))
  1066. {
  1067. case IDOK:
  1068. EndDialog(hDlg,1);
  1069. return TRUE;
  1070. case IDCANCEL:
  1071. EndDialog(hDlg,2);
  1072. //DestroyWindow(hDlg);
  1073. return TRUE;
  1074. }
  1075. break;
  1076. case WM_CLOSE:
  1077. EndDialog(hDlg,3);
  1078. return TRUE;
  1079. case WM_DESTROY:
  1080. MessageBox(hDlg,"WM_DESTORY来了!","对话框",MB_OK);
  1081. return TRUE;
  1082. }
  1083. return FALSE;
  1084. }
  1085. //无模式对话框过程函数
  1086. BOOL CALLBACK ModelessProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
  1087. {
  1088. switch (uMsg)
  1089. {
  1090. case WM_INITDIALOG:
  1091. SetWindowText(hDlg,"无模式对话框");
  1092. return TRUE;
  1093. case WM_COMMAND:
  1094. switch(LOWORD(wParam))
  1095. {
  1096. case IDOK:
  1097. DestroyWindow(hDlg);
  1098. return TRUE;
  1099. case IDCANCEL:
  1100. DestroyWindow(hDlg);
  1101. //DestroyWindow(hDlg);
  1102. return TRUE;
  1103. }
  1104. break;
  1105. case WM_CLOSE:
  1106. return TRUE;
  1107. case WM_DESTROY:
  1108. SetActiveWindow(GetParent(hDlg));//激活父窗口
  1109. return TRUE;
  1110. }
  1111. return FALSE;
  1112. }
  1113. //窗口处理函数
  1114. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  1115. {
  1116. switch ( nMsg )
  1117. {
  1118. case WM_COMMAND:
  1119. switch(LOWORD(wParam))
  1120. {
  1121. case IDM_DLG_MODAL://模式
  1122. {
  1123. int nResult = DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG),
  1124. hWnd,ModalDlgProc);
  1125. switch (nResult)
  1126. {
  1127. case 1:
  1128. MessageBox(hWnd,"用户按了确定按钮!","对话框",MB_OK);
  1129. break;
  1130. case 2:
  1131. MessageBox(hWnd,"用户按了取消按钮!","对话框",MB_OK);
  1132. break;
  1133. case 3:
  1134. MessageBox(hWnd,"用户按了关闭按钮!","对话框",MB_OK);
  1135. break;
  1136. }
  1137. return 0;
  1138. }
  1139. case IDM_DLG_MODELESS://无模式
  1140. HWND hDlg = CreateDialog(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG),
  1141. hWnd,ModelessProc);
  1142. ShowWindow(hDlg,SW_NORMAL);
  1143. return 0;
  1144. }
  1145. break;
  1146. case WM_DESTROY:
  1147. PostQuitMessage( 0 );//能够使getMessage返回0
  1148. break;
  1149. }
  1150. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  1151. }
  1152. //注册窗口类
  1153. void Register(LPSTR lpClassName,WNDPROC wndproc )
  1154. {
  1155. WNDCLASSEX wce = { 0 };
  1156. wce.cbSize = sizeof( wce ); //结构体的大小
  1157. wce.cbClsExtra = 0;//窗口类缓冲区
  1158. wce.cbWndExtra = 0;//窗口缓冲区
  1159. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  1160. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  1161. wce.hIcon = NULL;//窗口大图标句柄
  1162. wce.hIconSm = NULL;//窗口小图标句柄
  1163. wce.hInstance = g_hInstance;//当前模块句柄
  1164. wce.lpfnWndProc = wndproc;//窗口处理函数
  1165. wce.lpszClassName = lpClassName;//窗口类名称
  1166. wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);//菜单名
  1167. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  1168. RegisterClassEx( &wce );//注册,向系统写入
  1169. }
  1170. //创建主窗口
  1171. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  1172. {
  1173. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  1174. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  1175. NULL );
  1176. return hWnd;
  1177. }
  1178. //显示(绘制)窗口
  1179. void Display(HWND hWnd )
  1180. {
  1181. ShowWindow( hWnd,SW_SHOW );
  1182. UpdateWindow( hWnd);
  1183. }
  1184. //消息循环
  1185. void Message()
  1186. {
  1187. MSG uMsg = { 0 };
  1188. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  1189. {
  1190. TranslateMessage( &uMsg ); // 翻译,分析
  1191. DispatchMessage( &uMsg ); // 调度,派发
  1192. }
  1193. }
  1194. int APIENTRY WinMain(HINSTANCE hInstance,
  1195. HINSTANCE hPrevInstance,
  1196. LPSTR lpCmdLine,
  1197. int nCmdShow)
  1198. {
  1199. g_hInstance = hInstance;
  1200. Register("Main",WndProc);
  1201. HWND hWnd = CreateMain( "Main","WINDOWS");
  1202. Display( hWnd);
  1203. Message();
  1204. return 0;
  1205. }
  1206. **//****按钮**
  1207. \#include "stdafx.h"
  1208. \#define IDC_BTN_PUSH 4001
  1209. \#define IDC_BTN_DEFPUSH 4002
  1210. \#define IDC_BTN_GROUP 4003
  1211. \#define IDC_BTN_CHECK 4004
  1212. \#define IDC_BTN_AUTOCHECK 4005
  1213. \#define IDC_BTN_3STATE 4006
  1214. \#define IDC_BTN_AUTO3STATE 4007
  1215. \#define IDC_BTN_RADIO1 4008
  1216. \#define IDC_BTN_RADIO2 4009
  1217. \#define IDC_BTN_AUTORADIO1 4010
  1218. \#define IDC_BTN_AUTORADIO2 4011
  1219. \#define IDC_BTN_AUTORADIO3 4012
  1220. \#define IDC_BTN_AUTORADIO4 4013
  1221. \#define IDC_BTN_PUSHLIKE 4014
  1222. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  1223. //窗口处理函数
  1224. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  1225. {
  1226. switch ( nMsg )
  1227. {
  1228. case WM_CREATE:
  1229. //创建按钮
  1230. {
  1231. //下压式按钮
  1232. int x = 4, y = 4;
  1233. CreateWindowEx(
  1234. 0,
  1235. "BUTTON",
  1236. "Push Button",
  1237. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  1238. x,
  1239. y,
  1240. 150,
  1241. 32,
  1242. hWnd,
  1243. (HMENU)IDC_BTN_PUSH,
  1244. g_hInstance,
  1245. NULL);
  1246. //缺省下压按钮
  1247. CreateWindowEx(
  1248. 0,
  1249. "BUTTON",
  1250. "Default Push Button",
  1251. WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
  1252. x,
  1253. y += 36,
  1254. 150,
  1255. 32,
  1256. hWnd,
  1257. (HMENU)IDC_BTN_PUSH,
  1258. g_hInstance,
  1259. NULL);
  1260. //分组框
  1261. CreateWindowEx(
  1262. 0,
  1263. "BUTTON",
  1264. "Group Box",
  1265. WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
  1266. x,
  1267. y += 36,
  1268. 150,
  1269. 96,
  1270. hWnd,
  1271. (HMENU)IDC_BTN_GROUP,
  1272. g_hInstance,
  1273. NULL);
  1274. //复选框(手动)
  1275. CreateWindowEx(
  1276. 0,
  1277. "BUTTON",
  1278. "Check Box",
  1279. WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
  1280. x,
  1281. y += 100,
  1282. 150,
  1283. 24,
  1284. hWnd,
  1285. (HMENU)IDC_BTN_CHECK,
  1286. g_hInstance,
  1287. NULL);
  1288. //复选框(自动)
  1289. CreateWindowEx(
  1290. 0,
  1291. "BUTTON",
  1292. "Auto Check Box",
  1293. WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX,
  1294. x,
  1295. y += 28,
  1296. 150,
  1297. 24,
  1298. hWnd,
  1299. (HMENU)IDC_BTN_AUTOCHECK,
  1300. g_hInstance,
  1301. NULL);
  1302. //三态复选框(手动)
  1303. CreateWindowEx(
  1304. 0,
  1305. "BUTTON",
  1306. "3 State",
  1307. WS_CHILD | WS_VISIBLE | BS_3STATE,
  1308. x,
  1309. y += 28,
  1310. 150,
  1311. 24,
  1312. hWnd,
  1313. (HMENU)IDC_BTN_3STATE,
  1314. g_hInstance,
  1315. NULL);
  1316. //三态复选框(自动)
  1317. CreateWindowEx(
  1318. 0,
  1319. "BUTTON",
  1320. "Auto 3 State",
  1321. WS_CHILD | WS_VISIBLE | BS_AUTO3STATE,
  1322. x,
  1323. y += 28,
  1324. 150,
  1325. 24,
  1326. hWnd,
  1327. (HMENU)IDC_BTN_AUTO3STATE,
  1328. g_hInstance,
  1329. NULL);
  1330. //复选框1
  1331. SendMessage( CreateWindowEx(/* 创建并初始化*/
  1332. 0,
  1333. "BUTTON",
  1334. "Radio Button 1",
  1335. WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_GROUP,/* 设为组长*/
  1336. x,
  1337. y += 28,
  1338. 150,
  1339. 24,
  1340. hWnd,
  1341. (HMENU)IDC_BTN_RADIO1,
  1342. g_hInstance,
  1343. NULL),BM_SETCHECK,BST_CHECKED,0);
  1344. //复选框2
  1345. CreateWindowEx(
  1346. 0,
  1347. "BUTTON",
  1348. "Radio Button 2",
  1349. WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
  1350. x,
  1351. y += 28,
  1352. 150,
  1353. 24,
  1354. hWnd,
  1355. (HMENU)IDC_BTN_RADIO2,
  1356. g_hInstance,
  1357. NULL);
  1358. //复选框1(自动)
  1359. SendMessage( CreateWindowEx(/* 创建并初始化*/
  1360. 0,
  1361. "BUTTON",
  1362. "Auto Radio Button 1",
  1363. WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON| WS_GROUP,/* 设为组长*/
  1364. x,
  1365. y += 28,
  1366. 150,
  1367. 24,
  1368. hWnd,
  1369. (HMENU)IDC_BTN_AUTORADIO1,
  1370. g_hInstance,
  1371. NULL),BM_SETCHECK,BST_CHECKED,0);
  1372. //复选框2(自动)
  1373. CreateWindowEx(
  1374. 0,
  1375. "BUTTON",
  1376. "Auto Radio Button 2",
  1377. WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
  1378. x,
  1379. y += 28,
  1380. 150,
  1381. 24,
  1382. hWnd,
  1383. (HMENU)IDC_BTN_AUTORADIO2,
  1384. g_hInstance,
  1385. NULL);
  1386. //复选框3(自动)
  1387. SendMessage( CreateWindowEx(/* 创建并初始化*/
  1388. 0,
  1389. "BUTTON",
  1390. "Auto Radio Button 3",
  1391. WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON| WS_GROUP,/* 设为组长*/
  1392. x,
  1393. y += 28,
  1394. 150,
  1395. 24,
  1396. hWnd,
  1397. (HMENU)IDC_BTN_AUTORADIO3,
  1398. g_hInstance,
  1399. NULL),BM_SETCHECK,BST_CHECKED,0);
  1400. //复选框4(自动)
  1401. CreateWindowEx(
  1402. 0,
  1403. "BUTTON",
  1404. "Auto Radio Button 4",
  1405. WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
  1406. x,
  1407. y += 28,
  1408. 150,
  1409. 24,
  1410. hWnd,
  1411. (HMENU)IDC_BTN_AUTORADIO4,
  1412. g_hInstance,
  1413. NULL);
  1414. //复选框(自动)(类似于下压按钮)
  1415. CreateWindowEx(
  1416. 0,
  1417. "BUTTON",
  1418. "Push Like",
  1419. WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | BS_PUSHLIKE,
  1420. x,
  1421. y += 28,
  1422. 150,
  1423. 32,
  1424. hWnd,
  1425. (HMENU)IDC_BTN_PUSHLIKE,
  1426. g_hInstance,
  1427. NULL);
  1428. return 0;
  1429. }
  1430. //消息处理
  1431. case WM_COMMAND:
  1432. //按钮消息
  1433. switch (LOWORD(wParam))
  1434. {
  1435. case IDC_BTN_PUSH:
  1436. case IDC_BTN_DEFPUSH:
  1437. switch (HIWORD(wParam))
  1438. {
  1439. case BN_CLICKED:
  1440. {
  1441. CHAR szText[256];
  1442. GetWindowText((HWND)lParam,szText,
  1443. sizeof(szText)/sizeof(szText[0])); //取按钮文字
  1444. MessageBox(hWnd,szText,"按钮",MB_OK);
  1445. return 0;
  1446. }
  1447. }
  1448. break;
  1449. case IDC_BTN_CHECK:
  1450. switch(HIWORD(wParam))
  1451. {
  1452. //复选框(勾)
  1453. case BN_CLICKED:
  1454. switch(SendMessage((HWND)lParam,BM_GETCHECK,0,0))
  1455. {//打勾勾
  1456. case BST_CHECKED:
  1457. SendMessage((HWND)lParam,BM_SETCHECK,BST_UNCHECKED,0);
  1458. break;
  1459. case BST_UNCHECKED:
  1460. SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);
  1461. break;
  1462. }
  1463. return 0;
  1464. }
  1465. break;
  1466. //3态复选框(勾)
  1467. case IDC_BTN_3STATE:
  1468. switch(HIWORD(wParam))
  1469. {
  1470. case BN_CLICKED:
  1471. switch(SendMessage((HWND)lParam,BM_GETCHECK,0,0))
  1472. {//打勾勾
  1473. case BST_CHECKED:
  1474. SendMessage((HWND)lParam,
  1475. BM_SETCHECK,BST_INDETERMINATE,0);//灰态
  1476. break;
  1477. case BST_INDETERMINATE:
  1478. SendMessage((HWND)lParam,
  1479. BM_SETCHECK,BST_UNCHECKED,0);//无勾
  1480. break;
  1481. case BST_UNCHECKED:
  1482. SendMessage((HWND)lParam,
  1483. BM_SETCHECK,BST_CHECKED,0);//有勾
  1484. break;
  1485. }
  1486. return 0;
  1487. }
  1488. break;
  1489. case IDC_BTN_RADIO1://单选框1
  1490. switch (HIWORD(wParam))
  1491. {
  1492. case BN_CLICKED:
  1493. if(SendMessage((HWND)lParam,BM_GETCHECK,0,
  1494. 0) == BST_UNCHECKED)//当前没选
  1495. {
  1496. SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);
  1497. SendMessage(GetDlgItem(hWnd,IDC_BTN_RADIO2),
  1498. BM_SETCHECK,BST_UNCHECKED,0);
  1499. }
  1500. return 0;
  1501. }
  1502. break;
  1503. case IDC_BTN_RADIO2://单选框2
  1504. switch (HIWORD(wParam))
  1505. {
  1506. case BN_CLICKED:
  1507. if(SendMessage((HWND)lParam,BM_GETCHECK,0,
  1508. 0) == BST_UNCHECKED)//当前没选
  1509. {
  1510. SendMessage((HWND)lParam,BM_SETCHECK,BST_CHECKED,0);
  1511. SendMessage(GetDlgItem(hWnd,IDC_BTN_RADIO1),
  1512. BM_SETCHECK,BST_UNCHECKED,0);
  1513. }
  1514. return 0;
  1515. }
  1516. break;
  1517. }
  1518. break;
  1519. case WM_DESTROY:
  1520. PostQuitMessage( 0 );//能够使getMessage返回0
  1521. break;
  1522. }
  1523. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  1524. }
  1525. //注册窗口类
  1526. void Register(LPSTR lpClassName,WNDPROC wndproc )
  1527. {
  1528. WNDCLASSEX wce = { 0 };
  1529. wce.cbSize = sizeof( wce ); //结构体的大小
  1530. wce.cbClsExtra = 0;//窗口类缓冲区
  1531. wce.cbWndExtra = 0;//窗口缓冲区
  1532. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  1533. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  1534. wce.hIcon = NULL;//窗口大图标句柄
  1535. wce.hIconSm = NULL;//窗口小图标句柄
  1536. wce.hInstance = g_hInstance;//当前模块句柄
  1537. wce.lpfnWndProc = wndproc;//窗口处理函数
  1538. wce.lpszClassName = lpClassName;//窗口类名称
  1539. wce.lpszMenuName = NULL;//菜单名
  1540. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  1541. RegisterClassEx( &wce );//注册,向系统写入
  1542. }
  1543. //创建主窗口
  1544. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  1545. {
  1546. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  1547. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  1548. NULL );
  1549. return hWnd;
  1550. }
  1551. //显示(绘制)窗口
  1552. void Display(HWND hWnd )
  1553. {
  1554. ShowWindow( hWnd,SW_SHOW );
  1555. UpdateWindow( hWnd);
  1556. }
  1557. //消息循环
  1558. void Message()
  1559. {
  1560. MSG uMsg = { 0 };
  1561. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  1562. {
  1563. TranslateMessage( &uMsg ); // 翻译,分析
  1564. DispatchMessage( &uMsg ); // 调度,派发
  1565. }
  1566. }
  1567. int APIENTRY WinMain(HINSTANCE hInstance,
  1568. HINSTANCE hPrevInstance,
  1569. LPSTR lpCmdLine,
  1570. int nCmdShow)
  1571. {
  1572. g_hInstance = hInstance;
  1573. Register("Main",WndProc);
  1574. HWND hWnd = CreateMain( "Main","Windows");
  1575. Display( hWnd);
  1576. Message();
  1577. return 0;
  1578. }
  1579. **//****编辑框**
  1580. \#include "stdafx.h"
  1581. \#include <stdlib.h>
  1582. \#define IDC_EDIT_SINGLEINE 4001
  1583. \#define IDC_EDIT_PASSWORD 4002
  1584. \#define IDC_EDIT_NUMBER 4003
  1585. \#define IDC_EDIT_UPPERCASE 4004
  1586. \#define IDC_EDIT_MULTILINE 4005
  1587. \#define IDC_EDIT_READONLY 4006
  1588. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  1589. //窗口处理函数
  1590. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  1591. {
  1592. switch ( nMsg )
  1593. {
  1594. case WM_CREATE:
  1595. {
  1596. int x = 4, y = 4;
  1597. //单行编辑框
  1598. CreateWindowEx(
  1599. WS_EX_CLIENTEDGE,
  1600. "EDIT",
  1601. "Hello, world !",
  1602. WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
  1603. x,
  1604. y,
  1605. 238,
  1606. 24,
  1607. hWnd,
  1608. (HMENU)IDC_EDIT_SINGLEINE,
  1609. g_hInstance,
  1610. NULL);
  1611. //单行编辑框(口令式(密码))
  1612. CreateWindowEx(
  1613. WS_EX_CLIENTEDGE,
  1614. "EDIT",
  1615. NULL,
  1616. WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_PASSWORD | ES_CENTER,
  1617. x,
  1618. y += 28,
  1619. 238,
  1620. 24,
  1621. hWnd,
  1622. (HMENU)IDC_EDIT_PASSWORD,
  1623. g_hInstance,
  1624. NULL);
  1625. //单行编辑框(只能输入数字)
  1626. CreateWindowEx(
  1627. WS_EX_CLIENTEDGE,
  1628. "EDIT",
  1629. NULL,
  1630. WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_NUMBER | ES_RIGHT,
  1631. x,
  1632. y += 28,
  1633. 238,
  1634. 24,
  1635. hWnd,
  1636. (HMENU)IDC_EDIT_NUMBER,
  1637. g_hInstance,
  1638. NULL);
  1639. //单行编辑框(只能输入大写)
  1640. CreateWindowEx(
  1641. WS_EX_CLIENTEDGE,
  1642. "EDIT",
  1643. NULL,
  1644. WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | ES_UPPERCASE,
  1645. x,
  1646. y += 28,
  1647. 238,
  1648. 24,
  1649. hWnd,
  1650. (HMENU)IDC_EDIT_UPPERCASE,
  1651. g_hInstance,
  1652. NULL);
  1653. //多行编辑框
  1654. CreateWindowEx(
  1655. WS_EX_CLIENTEDGE,
  1656. "EDIT",
  1657. NULL,
  1658. WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL,
  1659. x,
  1660. y += 28,
  1661. 238,
  1662. 183,
  1663. hWnd,
  1664. (HMENU)IDC_EDIT_MULTILINE,
  1665. g_hInstance,
  1666. NULL);
  1667. //多行编辑框(只读)
  1668. CreateWindowEx(
  1669. WS_EX_CLIENTEDGE,
  1670. "EDIT",
  1671. "你好啊\r\n我爱学习",
  1672. WS_CHILD | WS_VISIBLE | ES_MULTILINE |
  1673. WS_HSCROLL | WS_VSCROLL | ES_READONLY,
  1674. x,
  1675. y += 187,
  1676. 238,
  1677. 183,
  1678. hWnd,
  1679. (HMENU)IDC_EDIT_READONLY,
  1680. g_hInstance,
  1681. NULL);
  1682. return 0;
  1683. }
  1684. //鼠标左键消息
  1685. case WM_LBUTTONDOWN:
  1686. {
  1687. int nLen = GetWindowTextLength(GetDlgItem (hWnd,IDC_EDIT_MULTILINE));
  1688. if(nLen)
  1689. {
  1690. ++nLen;
  1691. LPSTR pszText = (LPSTR)malloc(nLen * sizeof(pszText[0]));
  1692. GetWindowText(GetDlgItem(hWnd,IDC_EDIT_MULTILINE),pszText,nLen);
  1693. MessageBox(hWnd,pszText,"密码",MB_OK);
  1694. free(pszText);
  1695. }
  1696. return 0;
  1697. }
  1698. case WM_DESTROY:
  1699. PostQuitMessage( 0 );//能够使getMessage返回0
  1700. break;
  1701. }
  1702. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  1703. }
  1704. //注册窗口类
  1705. void Register(LPSTR lpClassName,WNDPROC wndproc )
  1706. {
  1707. WNDCLASSEX wce = { 0 };
  1708. wce.cbSize = sizeof( wce ); //结构体的大小
  1709. wce.cbClsExtra = 0;//窗口类缓冲区
  1710. wce.cbWndExtra = 0;//窗口缓冲区
  1711. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  1712. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  1713. wce.hIcon = NULL;//窗口大图标句柄
  1714. wce.hIconSm = NULL;//窗口小图标句柄
  1715. wce.hInstance = g_hInstance;//当前模块句柄
  1716. wce.lpfnWndProc = wndproc;//窗口处理函数
  1717. wce.lpszClassName = lpClassName;//窗口类名称
  1718. wce.lpszMenuName = NULL;//菜单名
  1719. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  1720. RegisterClassEx( &wce );//注册,向系统写入
  1721. }
  1722. //创建主窗口
  1723. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  1724. {
  1725. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  1726. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  1727. NULL );
  1728. return hWnd;
  1729. }
  1730. //显示(绘制)窗口
  1731. void Display(HWND hWnd )
  1732. {
  1733. ShowWindow( hWnd,SW_SHOW );
  1734. UpdateWindow( hWnd);
  1735. }
  1736. //消息循环
  1737. void Message()
  1738. {
  1739. MSG uMsg = { 0 };
  1740. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  1741. {
  1742. TranslateMessage( &uMsg ); // 翻译,分析
  1743. DispatchMessage( &uMsg ); // 调度,派发
  1744. }
  1745. }
  1746. int APIENTRY WinMain(HINSTANCE hInstance,
  1747. HINSTANCE hPrevInstance,
  1748. LPSTR lpCmdLine,
  1749. int nCmdShow)
  1750. {
  1751. g_hInstance = hInstance;
  1752. Register("Main",WndProc);
  1753. HWND hWnd = CreateMain( "Main","Windows");
  1754. Display( hWnd);
  1755. Message();
  1756. return 0;
  1757. }
  1758. **//****滚动条**
  1759. \#include "stdafx.h"
  1760. \#define IDC_SB_R 4001
  1761. \#define IDC_SB_G 4002
  1762. \#define IDC_SB_B 4003
  1763. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  1764. //窗口处理函数
  1765. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  1766. {
  1767. switch ( nMsg )
  1768. {
  1769. case WM_CREATE:
  1770. {
  1771. int x = 4, y = 4;
  1772. //滚动条(垂直)
  1773. HWND hwndSB = CreateWindowEx (
  1774. 0,
  1775. "SCROLLBAR",
  1776. NULL,
  1777. WS_CHILD | WS_VISIBLE | SBS_VERT,
  1778. x,
  1779. y,
  1780. 50,
  1781. 300,
  1782. hWnd,
  1783. (HMENU)IDC_SB_R,
  1784. g_hInstance,
  1785. NULL);
  1786. SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围
  1787. SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置
  1788. //滚动条(水平)
  1789. hwndSB = CreateWindowEx (
  1790. 0,
  1791. "SCROLLBAR",
  1792. NULL,
  1793. WS_CHILD | WS_VISIBLE | SBS_HORZ,
  1794. x += 54,
  1795. y += 125,
  1796. 150,
  1797. 50,
  1798. hWnd,
  1799. (HMENU)IDC_SB_G,
  1800. g_hInstance,
  1801. NULL);
  1802. SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围
  1803. SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置
  1804. //滚动条(垂直)
  1805. hwndSB = CreateWindowEx (
  1806. 0,
  1807. "SCROLLBAR",
  1808. NULL,
  1809. WS_CHILD | WS_VISIBLE | SBS_VERT,
  1810. x += 154,
  1811. y -= 125,
  1812. 50,
  1813. 300,
  1814. hWnd,
  1815. (HMENU)IDC_SB_B,
  1816. g_hInstance,
  1817. NULL);
  1818. SendMessage(hwndSB,SBM_SETRANGE,0,255);//设置范围
  1819. SendMessage(hwndSB,SBM_SETPOS,0,FALSE);//设置位置
  1820. return 0;
  1821. }
  1822. //消息处理
  1823. //处理水平滚动条
  1824. case WM_HSCROLL:
  1825. {
  1826. int nPos = SendMessage((HWND)lParam,SBM_GETPOS,0,0);
  1827. switch(LOWORD(wParam))
  1828. {
  1829. case SB_LINELEFT:
  1830. --nPos;
  1831. break;
  1832. case SB_LINERIGHT:
  1833. ++nPos;
  1834. break;
  1835. case SB_PAGELEFT:
  1836. nPos -= 32;
  1837. break;
  1838. case SB_PAGERIGHT:
  1839. nPos += 32;
  1840. break;
  1841. // case SB_THUMBPOSITION: //松手时拿到消息
  1842. case SB_THUMBTRACK: // 滚动过程中拿到消息
  1843. nPos = HIWORD(wParam);
  1844. break;
  1845. }
  1846. SendMessage((HWND)lParam,SBM_SETPOS,nPos,TRUE);
  1847. InvalidateRect(hWnd,NULL,TRUE);//重绘
  1848. return 0;
  1849. }
  1850. //处理垂直滚动条
  1851. case WM_VSCROLL:
  1852. {
  1853. int nPos = SendMessage((HWND)lParam,SBM_GETPOS,0,0);
  1854. switch(LOWORD(wParam))
  1855. {
  1856. case SB_LINEUP:
  1857. --nPos;
  1858. break;
  1859. case SB_LINEDOWN:
  1860. ++nPos;
  1861. break;
  1862. case SB_PAGEUP:
  1863. nPos -= 32;
  1864. break;
  1865. case SB_PAGEDOWN:
  1866. nPos += 32;
  1867. break;
  1868. // case SB_THUMBPOSITION: //松手时拿到消息
  1869. case SB_THUMBTRACK: // 滚动过程中拿到消息
  1870. nPos = HIWORD(wParam);
  1871. break;
  1872. }
  1873. SendMessage((HWND)lParam,SBM_SETPOS,nPos,TRUE);
  1874. InvalidateRect(hWnd,NULL,TRUE);//重绘
  1875. return 0;
  1876. }
  1877. //刷背景消息
  1878. case WM_ERASEBKGND:
  1879. {
  1880. HBRUSH hbru = CreateSolidBrush(RGB(
  1881. SendMessage(GetDlgItem(hWnd,IDC_SB_R),SBM_GETPOS,0,0),
  1882. SendMessage(GetDlgItem(hWnd,IDC_SB_G),SBM_GETPOS,0,0),
  1883. SendMessage(GetDlgItem(hWnd,IDC_SB_B),SBM_GETPOS,0,0) ));
  1884. RECT rcClient;
  1885. GetClientRect(hWnd,&rcClient);
  1886. FillRect((HDC)wParam,&rcClient,hbru);
  1887. DeleteObject(hbru);
  1888. return 0;
  1889. }
  1890. case WM_DESTROY:
  1891. PostQuitMessage( 0 );//能够使getMessage返回0
  1892. break;
  1893. }
  1894. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  1895. }
  1896. //注册窗口类
  1897. void Register(LPSTR lpClassName,WNDPROC wndproc )
  1898. {
  1899. WNDCLASSEX wce = { 0 };
  1900. wce.cbSize = sizeof( wce ); //结构体的大小
  1901. wce.cbClsExtra = 0;//窗口类缓冲区
  1902. wce.cbWndExtra = 0;//窗口缓冲区
  1903. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  1904. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  1905. wce.hIcon = NULL;//窗口大图标句柄
  1906. wce.hIconSm = NULL;//窗口小图标句柄
  1907. wce.hInstance = g_hInstance;//当前模块句柄
  1908. wce.lpfnWndProc = wndproc;//窗口处理函数
  1909. wce.lpszClassName = lpClassName;//窗口类名称
  1910. wce.lpszMenuName = NULL;//菜单名
  1911. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  1912. RegisterClassEx( &wce );//注册,向系统写入
  1913. }
  1914. //创建主窗口
  1915. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  1916. {
  1917. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW |
  1918. WS_CLIPCHILDREN |WS_HSCROLL | WS_VSCROLL,CW_USEDEFAULT,
  1919. CW_USEDEFAULT,CW_USEDEFAULT,
  1920. CW_USEDEFAULT,NULL,NULL,g_hInstance,
  1921. NULL );
  1922. return hWnd;
  1923. }
  1924. //显示(绘制)窗口
  1925. void Display(HWND hWnd )
  1926. {
  1927. ShowWindow( hWnd,SW_SHOW );
  1928. UpdateWindow( hWnd);
  1929. }
  1930. //消息循环
  1931. void Message()
  1932. {
  1933. MSG uMsg = { 0 };
  1934. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  1935. {
  1936. TranslateMessage( &uMsg ); // 翻译,分析
  1937. DispatchMessage( &uMsg ); // 调度,派发
  1938. }
  1939. }
  1940. int APIENTRY WinMain(HINSTANCE hInstance,
  1941. HINSTANCE hPrevInstance,
  1942. LPSTR lpCmdLine,
  1943. int nCmdShow)
  1944. {
  1945. g_hInstance = hInstance;
  1946. Register("Main",WndProc);
  1947. HWND hWnd = CreateMain( "Main","Windows");
  1948. Display( hWnd);
  1949. Message();
  1950. return 0;
  1951. }
  1952. **//****组合框**
  1953. \#include "stdafx.h"
  1954. \#include "resource.h"
  1955. \#include <stdio.h>
  1956. \#include <stdlib.h>
  1957. \#define IDC_CMB_SIM 4001
  1958. \#define IDC_CMB_DD 4002
  1959. \#define IDC_CMB_DDL 4003
  1960. HINSTANCE g_hInstance = 0; //接收当前程序实例句柄
  1961. //窗口处理函数
  1962. LRESULT CALLBACK WndProc( HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam)
  1963. {
  1964. static HWND hwndSim = NULL,hwndDd = NULL,hwndDdl = NULL;
  1965. switch ( nMsg )
  1966. {
  1967. case WM_CREATE:
  1968. {
  1969. int x = 4,y = 4;
  1970. //组合框(简单的)
  1971. hwndSim = CreateWindowEx(
  1972. 0,
  1973. "COMBOBOX",
  1974. NULL,
  1975. WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  1976. CBS_AUTOHSCROLL | CBS_SIMPLE,
  1977. x,
  1978. y,
  1979. 200,
  1980. 250,
  1981. hWnd,
  1982. (HMENU)IDC_CMB_SIM,
  1983. g_hInstance,
  1984. NULL);
  1985. //组合框(下拉式)
  1986. hwndDd = CreateWindowEx(
  1987. 0,
  1988. "COMBOBOX",
  1989. NULL,
  1990. WS_CHILD | WS_VISIBLE | WS_VSCROLL |
  1991. CBS_AUTOHSCROLL | CBS_DROPDOWN,
  1992. x += 204,
  1993. y,
  1994. 200,
  1995. 250,
  1996. hWnd,
  1997. (HMENU)IDC_CMB_DD,
  1998. g_hInstance,
  1999. NULL);
  2000. //组合框(下拉列表式)
  2001. hwndDdl = CreateWindowEx(
  2002. 0,
  2003. "COMBOBOX",
  2004. NULL,
  2005. WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
  2006. x += 204,
  2007. y,
  2008. 200,
  2009. 250,
  2010. hWnd,
  2011. (HMENU)IDC_CMB_DDL,
  2012. g_hInstance,
  2013. NULL);
  2014. break;
  2015. }
  2016. //组合框消息
  2017. case WM_COMMAND:
  2018. switch (LOWORD(wParam))
  2019. {
  2020. //添加选项
  2021. case IDM_CMB_ADD:
  2022. {
  2023. for (int i = 0; i< 100; ++i)
  2024. {
  2025. CHAR szItem[256];
  2026. sprintf(szItem,"Item %d",i);
  2027. SendMessage(hwndSim,CB_ADDSTRING,0,(LPARAM)szItem);
  2028. SendMessage(hwndSim,CB_SETITEMDATA,i,i+1000);//附加数据
  2029. SendMessage(hwndDd,CB_ADDSTRING,0,(LPARAM)szItem);
  2030. SendMessage(hwndDdl,CB_ADDSTRING,0,(LPARAM)szItem);
  2031. }
  2032. SendMessage(hwndSim,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");
  2033. SendMessage(hwndDd,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");
  2034. SendMessage(hwndDdl,CB_INSERTSTRING,10,(LPARAM)"Item 9.5");
  2035. return 0;
  2036. }
  2037. //删除选项
  2038. case IDM_CMB_DEL:
  2039. SendMessage(hwndSim,CB_DELETESTRING,10,0);
  2040. SendMessage(hwndDd,CB_DELETESTRING,10,0);
  2041. SendMessage(hwndDdl,CB_DELETESTRING,10,0);
  2042. return 0;
  2043. //全删除
  2044. case IDM_CMB_RESET:
  2045. SendMessage(hwndSim,CB_RESETCONTENT,0,0);
  2046. SendMessage(hwndDd,CB_RESETCONTENT,0,0);
  2047. SendMessage(hwndDdl,CB_RESETCONTENT,0,0);
  2048. return 0;
  2049. //获取选项
  2050. case IDM_CMB_GET:
  2051. {
  2052. int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);
  2053. CHAR szMsg[256];
  2054. if(nSel == CB_ERR)
  2055. sprintf(szMsg,"没有选项!");
  2056. else
  2057. sprintf(szMsg,"当前选项:%d",nSel);
  2058. MessageBox(hWnd,szMsg,"WinCombo",MB_OK);
  2059. return 0;
  2060. }
  2061. //设置当前被选项
  2062. case IDM_CMB_SET:
  2063. {
  2064. SendMessage(hwndDd,CB_SETCURSEL,5,0);
  2065. return 0;
  2066. }
  2067. //模糊查找
  2068. case IDM_CMB_FIND:
  2069. {
  2070. int nFind = SendMessage(hwndDdl,CB_FINDSTRING,-1,(LPARAM)"Item 55");
  2071. CHAR szMsg[256];
  2072. if(nFind == CB_ERR)
  2073. sprintf(szMsg,"没找到!");
  2074. else
  2075. sprintf(szMsg,"找到了:%d",nFind);
  2076. MessageBox(hWnd,szMsg,"WinCombo",MB_OK);
  2077. return 0;
  2078. }
  2079. //精确查找
  2080. case IDM_CMB_EXACT:
  2081. {
  2082. int nFind = SendMessage(hwndDdl,CB_FINDSTRINGEXACT,-1,(LPARAM)"Item");
  2083. CHAR szMsg[256];
  2084. if(nFind == CB_ERR)
  2085. sprintf(szMsg,"没找到!");
  2086. else
  2087. sprintf(szMsg,"找到了:%d",nFind);
  2088. MessageBox(hWnd,szMsg,"WinCombo",MB_OK);
  2089. return 0;
  2090. }
  2091. //选择设置
  2092. case IDM_CMB_SELSTR:
  2093. {
  2094. SendMessage(hwndDdl,CB_SELECTSTRING,-1,(LPARAM)"itEm 55");
  2095. return 0;
  2096. }
  2097. //获取内容
  2098. case IDM_CMB_TEXT:
  2099. {
  2100. int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);
  2101. if(nSel != CB_ERR)
  2102. {
  2103. int nLen = SendMessage(hwndSim,CB_GETLBTEXTLEN,
  2104. nSel,0);
  2105. ++nLen;
  2106. LPSTR pszText = (LPSTR)malloc(nLen * sizeof(pszText[0]));
  2107. SendMessage(hwndSim,CB_GETLBTEXT,nSel,
  2108. (LPARAM)pszText);
  2109. MessageBox(hWnd,pszText,"WinCombo",MB_OK);
  2110. free(pszText);
  2111. }
  2112. return 0;
  2113. }
  2114. //显示文件/目录
  2115. case IDM_CMB_DIR:
  2116. {
  2117. SendMessage(hwndSim,CB_DIR,DDL_ARCHIVE,(LPARAM)"C:/Windows/*.*");//文件
  2118. SendMessage(hwndDd,CB_DIR,DDL_DIRECTORY | DDL_EXCLUSIVE/*排除它*/,
  2119. (LPARAM)"C:/Windows/*.*");//目录
  2120. SendMessage(hwndDdl,CB_DIR,DDL_HIDDEN | DDL_ARCHIVE | DDL_SYSTEM |
  2121. DDL_EXCLUSIVE/*排除它*/,(LPARAM)"C:/*.*");//隐藏文件
  2122. return 0;
  2123. }
  2124. //附加数据
  2125. case IDM_CMB_DATA:
  2126. {
  2127. int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);//获得所选项
  2128. if(nSel != CB_ERR)
  2129. {
  2130. int data = SendMessage(hwndSim,CB_GETITEMDATA,nSel,0);//拿选项
  2131. char szMsg[256];
  2132. sprintf(szMsg,"附加数据:%d",data);
  2133. MessageBox(hWnd,szMsg,"WinCombo",MB_OK);
  2134. }
  2135. return 0;
  2136. }
  2137. //通知消息
  2138. case IDC_CMB_SIM:
  2139. switch (HIWORD(wParam))
  2140. {
  2141. case CBN_SELCHANGE:
  2142. int nSel = SendMessage(hwndSim,CB_GETCURSEL,0,0);
  2143. if(nSel != CB_ERR)
  2144. {
  2145. SendMessage(hwndDd,CB_SETCURSEL,nSel,0);
  2146. SendMessage(hwndDdl,CB_SETCURSEL,nSel,0);
  2147. }
  2148. return 0;
  2149. }
  2150. }
  2151. break;
  2152. case WM_DESTROY:
  2153. PostQuitMessage( 0 );//能够使getMessage返回0
  2154. break;
  2155. }
  2156. return DefWindowProc( hWnd, nMsg, wParam,lParam );
  2157. }
  2158. //注册窗口类
  2159. void Register(LPSTR lpClassName,WNDPROC wndproc )
  2160. {
  2161. WNDCLASSEX wce = { 0 };
  2162. wce.cbSize = sizeof( wce ); //结构体的大小
  2163. wce.cbClsExtra = 0;//窗口类缓冲区
  2164. wce.cbWndExtra = 0;//窗口缓冲区
  2165. wce.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);//背景色
  2166. wce.hCursor = LoadCursor(NULL,IDC_ARROW);//光标
  2167. wce.hIcon = NULL;//窗口大图标句柄
  2168. wce.hIconSm = NULL;//窗口小图标句柄
  2169. wce.hInstance = g_hInstance;//当前模块句柄
  2170. wce.lpfnWndProc = wndproc;//窗口处理函数
  2171. wce.lpszClassName = lpClassName;//窗口类名称
  2172. wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU_MAIN);//菜单名
  2173. wce.style = CS_VREDRAW | CS_VREDRAW;//窗口风格
  2174. RegisterClassEx( &wce );//注册,向系统写入
  2175. }
  2176. //创建主窗口
  2177. HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName )
  2178. {
  2179. HWND hWnd = CreateWindowEx( 0, lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,
  2180. CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,
  2181. NULL );
  2182. return hWnd;
  2183. }
  2184. //显示(绘制)窗口
  2185. void Display(HWND hWnd )
  2186. {
  2187. ShowWindow( hWnd,SW_SHOW );
  2188. UpdateWindow( hWnd);
  2189. }
  2190. //消息循环
  2191. void Message()
  2192. {
  2193. MSG uMsg = { 0 };
  2194. while( GetMessage(&uMsg,NULL,0,0)) //获取消息
  2195. {
  2196. TranslateMessage( &uMsg ); // 翻译,分析
  2197. DispatchMessage( &uMsg ); // 调度,派发
  2198. }
  2199. }
  2200. int APIENTRY WinMain(HINSTANCE hInstance,
  2201. HINSTANCE hPrevInstance,
  2202. LPSTR lpCmdLine,
  2203. int nCmdShow)
  2204. {
  2205. g_hInstance = hInstance;
  2206. Register("Main",WndProc);
  2207. HWND hWnd = CreateMain( "Main","Windows");
  2208. Display( hWnd);
  2209. Message();
  2210. return 0;
  2211. }

打赏

微信支付 支付宝支付

License

本作品由Simonhttp://www.uusystem.com)创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 欢迎转载,但任何转载必须保留完整文章,在显要地方显示此声明以及原文链接。如您有任何疑问或者授权方面的协商,请邮件:postmaster@uusystem.com。