当前位置:   article > 正文

C++windows内核编程笔记day09_day10,对话框和窗体基本控件等的使用

hdc setbkmodel c++
//设置字体颜色
SetTextColor(hdc,RGB(255,0,0));


//窗体背景
//wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
//wce.hbrBackground=CreateSolidBrush(RGB(0,0,255));


//设置字体背景
SetBkColor(hdc,RGB(0,0,200));
//设置字体背景模式
SetBkMode(hdc,TRANSPARENT);//字体背景透明
//创建字体,成功返回字体,失败返回NULL
HFONT CreateFont(
  int nHeight,               // 高度
  int nWidth,                // 宽度
  int nEscapement,           // 倾斜角度
  int nOrientation,          // 旋转角度(三维效果)
  int fnWeight,              // 粗细
  DWORD fdwItalic,           // 斜体
  DWORD fdwUnderline,        // 下划线
  DWORD fdwStrikeOut,        // 删除线
  DWORD fdwCharSet,          // 字符集(GB2312_CHARSET)
  DWORD fdwOutputPrecision,  // 输出精度(0)
  DWORD fdwClipPrecision,    // 剪切精度(0)
  DWORD fdwQuality,          // 质量(0)
  DWORD fdwPitchAndFamily,   // 匹配字体
  LPCTSTR lpszFace           // 字体名称(在C:\Windows\Fonts 打开详细的字体再看第一行名称)
);




对话框窗体
//用户处理对话框指令的函数
//返回TRUE 系统不默认处理;返回FALSE系统处理;
INT_PTR CALLBACK DialogProc(  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
  );
  //创建对话框
 INT_PTR DialogBox(  HINSTANCE hInstance,  // handle to module
  LPCTSTR lpTemplate,   // dialog box template
  HWND hWndParent,      // handle to owner window
  DLGPROC lpDialogFunc  // dialog box procedure
  );
//模式对话框
result=(int)DialogBox(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DialogProc);
int CALLBACK DialogProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
{
UINT id=LOWORD(wParam);
switch(uMsg)
{
case WM_SYSCOMMAND:
if(wParam==SC_CLOSE)
{
   EndDialog(hwnd,1);
return true;
}
break;
case WM_INITDIALOG://对话框创建之后、显示之前if(wParam==SC_CLOSE)
{
return true;
}
break;


}
return false;
}
//非模式对话框
HWND dlg= CreateDialog(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG2),hwnd,DialogProc2);
ShowWindow(dlg,1);
int CALLBACK DialogProc2(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
{
UINT id=LOWORD(wParam);
switch(uMsg)
{
case WM_SYSCOMMAND:
if(wParam==SC_CLOSE)
{
DestroyWindow(hwnd);
return true;
}
break;
case WM_INITDIALOG://对话框创建之后、显示之前
{
return true;
}
break;
}
return false;
}


//程序和子控件消息交互
SendMessage


day72 pm 85
//创建文字静态框,HMENU參数传入控件ID
CreateWindowEx(0,"STATIC","hello",WS_CHILD|WS_VISIBLE,100,100,
200,40,hwnd,(HMENU)1001,g_hinstance,0);
//创建图标静态框,HMENU參数传入控件ID,增加 SS_ICON或SS_BITMAP 风格
//字符串 写 "#资源ID号",风格有 SS_NOTIFY 时,点击才干产生消息
CreateWindowEx(0,"STATIC","#104",WS_CHILD|WS_VISIBLE|SS_ICON,100,100,
200,40,hwnd,(HMENU)1002,g_hinstance,0);
//更改图标
HICON ico=LoadIcon(g_hinstance,MAKEINTRESOURCE(IDI_ICON1));
HWND hs=GetDlgItem(hwnd,1002);//通过窗体句柄和控件ID号获取控件句柄
SendMessage(hs,STM_SETICON,(WPARAM)ico,0);//发消息改动静态图标的图片


button:
普通下压式button:BS_PUSHBUTTON、BS_DEFPUSHBUTTON
分组框button:BS_GROUPBOX
复选框button:BS_CHECKBOX、BS_AUTOCHECKBOX、BS_3STATE、BS_AUTO3STATE
单选框button:BS_RADIOBUTTON、BS_AUTORADIOBUTTON


//手动维护复选框状态
if(HIWORD(wparam)==BN_CLICKED)
{
HWND cb=GetDlgItem(hwnd,2002);//获取复选框控件
UINT stat= SendMessage(cb,BM_GETCHECK,0,0);//获取当前状态
//设置状态
if(stat==BST_CHECKED) SendMessage(cb,BM_SETCHECK,BST_UNCHECKED,0);
else SendMessage(cb,BM_SETCHECK,BST_CHECKED,0);
}
//手动维护3种状态复选框的状态
//WM_COMMAND中,HIWORD(wparam):菜单-0,加速键-1,控件-通知码(BN_CLICKED/STN_CLICKED等)
//lparam:菜单、加速键-NULL,控件:控件句柄
if(HIWORD(wparam)==BN_CLICKED)
{
UINT id=LOWORD(wparam);
HWND cb=GetDlgItem(hwnd,id);
UINT stat= SendMessage(cb,BM_GETCHECK,0,0);//获取当前状态
//设置状态
if(stat==BST_CHECKED) SendMessage(cb,BM_SETCHECK,BST_INDETERMINATE,0);
else if(stat==BST_INDETERMINATE) SendMessage(cb,BM_SETCHECK,BST_UNCHECKED,0);
else SendMessage(cb,BM_SETCHECK,BST_CHECKED,0);
}


day72 pm over


day73 am
//创建单选框,两组(风格加 WS_GROUP,从当前開始到下一个此风格前一个结束为一组)
CreateWindowEx(0,"BUTTON","RADIO E",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,50,260,
100,30,hwnd,(HMENU)2006,g_hinstance,0);
CreateWindowEx(0,"BUTTON","RADIO F",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,50,300,
100,30,hwnd,(HMENU)2007,g_hinstance,0);
CreateWindowEx(0,"BUTTON","RADIO G",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,50,340,
100,30,hwnd,(HMENU)2008,g_hinstance,0);
CreateWindowEx(0,"BUTTON","RADIO H",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,50,380,
100,30,hwnd,(HMENU)2009,g_hinstance,0);
//多态button。变相的复选button
CreateWindowEx(0,"BUTTON","RADIO I",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX|BS_PUSHLIKE,50,420,
100,30,hwnd,(HMENU)2010,g_hinstance,0);


编辑框:
单行编辑框:仅仅能输入一行。
多行编辑框:ES_MULTILINE|WS_VSCROLL|ES_AUTOVSCROLL。可输入回车
password编辑框:ES_PASSWORD,仅仅能单行。不能用ES_MULTILINE
仅仅能输入数字:ES_NUMBER
windows控件全部风格:Window Styles,编辑框全部风格:edit styles 可查msdn。


//设置控件内容WM_SETTEXT
//获取控件内容WM_GETTEXT
//获取内容长度WM_GETTEXTLENGTH
SendMessage(
  (HWND) hWnd,// 控件句柄
  WM_SETTEXT/WM_GETTEXT/WM_GETTEXTLENGTH,// 消息
  (WPARAM) wParam,//获取时,写buff最大大小,其它传0
  (LPARAM) lParam// 字符串地址
);
HWND he=GetDlgItem(hwnd,1001);
HFONT font=CreateFont(30,0,0,0,900,0,0,0,
GB2312_CHARSET,0,0,0,0,"黑体");
SendMessage(he,WM_SETFONT,(WPARAM)font,1);//改动字体
WM_CTLCOLOREDIT   //改动edit背景颜色
//窗体处理函数中
case WM_CTLCOLOREDIT:
{
SetBkMode((HDC)wparam,TRANSPARENT);//文本背景设为透明
//SetBkColor((HDC)wparam,RGB(0,230,0));
return (LRESULT)CreateSolidBrush(RGB(0,230,0));
}
break;
//文字改变时通知  EN_CHANGE
//设置窗体标题    SetWindowText,GetWindowText


组合框 COMBOBOX
HWND com1= CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|CBS_SIMPLE,
50,50,200,200,hwnd,(HMENU)1001,g_hinstance,0);//简单组合框
HWND com2= CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN,
260,50,200,200,hwnd,(HMENU)1002,g_hinstance,0);//下拉式
HWND com3=CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
470,50,200,200,hwnd,(HMENU)1003,g_hinstance,0);//下拉列表式
HFONT font=CreateFont(30,0,0,0,900,0,0,0,GB2312_CHARSET,0,0,0,0,"黑体");
SendMessage(com1,WM_SETFONT,(WPARAM)font,0);//设置字体


CB_ADDSTRING  追加选项
CB_FINDSTRING 查找,返回index
CB_DELETESTRING 删除,依据index删除
CB_INSERTSTRING 插入
CB_RESETCONTENT 清除全部项
CB_SETCURSEL 设置选中索引
CB_SELECTSTRING 设置选中的字符串
CB_GETLBTEXT  获取选择的文字,CB_GETLBTEXTLEN 获取文字长度


day73 pm 136
//删除选项
HWND com2=GetDlgItem(hwnd,1002);
int index=0;//SendMessage(com2,CB_GETCURSEL,0,0);
char* s="item1";
char a[100]={0};
//模糊查找,返回第一个匹配的索引
index=SendMessage(com2,CB_FINDSTRING,(WPARAM)-1,(LPARAM)s);
//SendMessage(com2,CB_SELECTSTRING,(WPARAM)-1,(LPARAM)s);//模糊匹配,并设置被选择
if(index!=CB_ERR)
{
SendMessage(com2,CB_GETLBTEXT,(WPARAM)index,(LPARAM)a);//取模糊匹配的文字
if(strcmp(s,a)==0)//推断是否全然匹配
{
SendMessage(com2,CB_DELETESTRING,index,0);
SendMessage(com2,CB_SETCURSEL,(WPARAM)index,0);
}
}
SendMessage(com2,CB_SETITEMDATA,index,1000+i);//设置附加数据
int data=SendMessage(com2,CB_GETITEMDATA,index,0);//获取附加数据
char b[10]={0};
sprintf(b,"%d",data);


day73 pm over!
CBN_SELCHANGE  选择项改变时产生的消息
CBN_EDITCHANGE  输入改改时产生的消息:
LRESULT CALLBACK WindowProc(
  HWND hwnd,       // handle to window
  UINT uMsg,       // WM_COMMAND
  WPARAM wParam,   // combo box identifier, CBN_EDITCHANGE
  LPARAM lParam    // handle to combo box (HWND)
);
在WM_COMMAND事件中。选择项改变时,发生通知事件


case 1001://第1个下拉框ID
{
if(HIWORD(wparam)==CBN_SELCHANGE)
{
HWND com1=GetDlgItem(hwnd,1001);
HWND com2=GetDlgItem(hwnd,1002);
HWND com3=GetDlgItem(hwnd,1003);
int index=SendMessage(com1,CB_GETCURSEL,0,0);
if(index!=CB_ERR)
{
SendMessage(com2,CB_SETCURSEL,index,0);
SendMessage(com3,CB_SETCURSEL,index,0);
}
}
}
break;


列表框:LISTBOX
单列列表框-样式默认
多列列表框-样式加 LBS_MULTICOLUMN
操作选择与COMBOBOX一样,CB_改为LB_,如:加入项由CB_ADDSTRING改为 LB_ADDSTRING,当选择项改变后WM_COMMAND通知事件:LBN_SELCHANGE。



第一个程序:

资源ID,图片:


加了资源,必须记得包括 resource.h


主要cpp代码:

  1. // win32app.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include <stdio.h>
  6. HINSTANCE g_hinstance=0;//全局句柄
  7. HANDLE g_houtput=0;
  8. void OnCreate(HWND hwnd,LPARAM lparam)
  9. {
  10. // char txt[200]={0};
  11. // ScreenToClient(hwnd,&pt);
  12. // sprintf(txt,"%d,%d ; %d,%d,%d,%d\n",pt.x,pt.y,rc.top,rc.left,rc.right,rc.bottom);
  13. //
  14. // WriteConsole(g_houtput,txt,strlen(txt),NULL,NULL);
  15. /*
  16. CreateWindowEx(0,"STATIC","hello",WS_CHILD|WS_VISIBLE,100,100,
  17. 200,40,hwnd,(HMENU)1001,g_hinstance,0);
  18. CreateWindowEx(0,"STATIC","#104",WS_CHILD|WS_VISIBLE|SS_ICON|SS_NOTIFY,300,100,
  19. 0,0,hwnd,(HMENU)1002,g_hinstance,0);
  20. */
  21. ///*
  22. CreateWindowEx(0,"BUTTON","GroupBox",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,30,30,
  23. 300,400,hwnd,(HMENU)2000,g_hinstance,0);
  24. CreateWindowEx(0,"BUTTON","btn1",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,50,50,
  25. 40,20,hwnd,(HMENU)2001,g_hinstance,0);
  26. CreateWindowEx(0,"BUTTON","Check A",WS_CHILD|WS_VISIBLE|BS_CHECKBOX,50,100,
  27. 100,30,hwnd,(HMENU)2002,g_hinstance,0);
  28. CreateWindowEx(0,"BUTTON","Check B",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,50,140,
  29. 100,30,hwnd,(HMENU)2003,g_hinstance,0);
  30. CreateWindowEx(0,"BUTTON","3state C",WS_CHILD|WS_VISIBLE|BS_3STATE,50,180,
  31. 100,30,hwnd,(HMENU)2004,g_hinstance,0);
  32. CreateWindowEx(0,"BUTTON","3state D",WS_CHILD|WS_VISIBLE|BS_AUTO3STATE,50,220,
  33. 100,30,hwnd,(HMENU)2005,g_hinstance,0);//3种状态复选框
  34. CreateWindowEx(0,"BUTTON","RADIO E",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,50,260,
  35. 100,30,hwnd,(HMENU)2006,g_hinstance,0);
  36. CreateWindowEx(0,"BUTTON","RADIO F",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,50,300,
  37. 100,30,hwnd,(HMENU)2007,g_hinstance,0);
  38. CreateWindowEx(0,"BUTTON","RADIO G",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON|WS_GROUP,50,340,
  39. 100,30,hwnd,(HMENU)2008,g_hinstance,0);
  40. CreateWindowEx(0,"BUTTON","RADIO H",WS_CHILD|WS_VISIBLE|BS_AUTORADIOBUTTON,50,380,
  41. 100,30,hwnd,(HMENU)2009,g_hinstance,0);
  42. CreateWindowEx(0,"BUTTON","RADIO I",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX|BS_PUSHLIKE,50,420,
  43. 100,30,hwnd,(HMENU)2010,g_hinstance,0);//多态button,变相的复选button
  44. //*/
  45. }
  46. //对话框处理函数
  47. int CALLBACK DialogProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
  48. {
  49. switch(uMsg)
  50. {
  51. case WM_SYSCOMMAND:
  52. if(wParam==SC_CLOSE)
  53. {
  54. EndDialog(hwnd,1);
  55. return true;
  56. }
  57. break;
  58. case WM_INITDIALOG://对话框创建之后、显示之前if(wParam==SC_CLOSE)
  59. {
  60. return true;
  61. }
  62. break;
  63. }
  64. return false;
  65. }
  66. int CALLBACK DialogProc2(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
  67. {
  68. switch(uMsg)
  69. {
  70. case WM_SYSCOMMAND:
  71. if(wParam==SC_CLOSE)
  72. {
  73. DestroyWindow(hwnd);
  74. return true;
  75. }
  76. break;
  77. case WM_INITDIALOG://对话框创建之后、显示之前
  78. {
  79. return true;
  80. }
  81. break;
  82. }
  83. return false;
  84. }
  85. void oncommand(HWND hwnd,WPARAM wparam)
  86. {
  87. UINT id=LOWORD(wparam);
  88. int result=0;
  89. HWND dlg=0;
  90. switch(id)
  91. {
  92. case Dlg_Model:
  93. result=(int)DialogBox(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DialogProc);
  94. break;
  95. case Dlg_Normal:
  96. dlg= CreateDialog(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG2),hwnd,DialogProc2);
  97. ShowWindow(dlg,1);
  98. break;
  99. case ID_Blue:
  100. {
  101. HICON ico=LoadIcon(g_hinstance,MAKEINTRESOURCE(IDI_ICON2));
  102. SendMessage(GetDlgItem(hwnd,1002),STM_SETICON,(WPARAM)ico,0);
  103. break;
  104. }
  105. case ID_Black:
  106. {
  107. HICON ico=LoadIcon(g_hinstance,MAKEINTRESOURCE(IDI_ICON1));
  108. HWND hs=GetDlgItem(hwnd,1002);//通过窗体句柄和控件ID号获取控件句柄
  109. SendMessage(hs,STM_SETICON,(WPARAM)ico,0);//改动静态图标的图片
  110. break;
  111. }
  112. case 1002:
  113. {
  114. if(HIWORD(wparam)==STN_DBLCLK)
  115. {
  116. MessageBox(NULL,"双击图标","info",MB_OK);
  117. }
  118. }
  119. break;
  120. case 2001:
  121. {
  122. if(HIWORD(wparam)==BN_CLICKED)
  123. {
  124. MessageBox(NULL,"button点击","info",MB_OK);
  125. }
  126. }
  127. case 2002:
  128. {
  129. if(HIWORD(wparam)==BN_CLICKED)
  130. {
  131. HWND cb=GetDlgItem(hwnd,id);
  132. UINT stat= SendMessage(cb,BM_GETCHECK,0,0);//获取当前状态
  133. //设置状态
  134. if(stat==BST_CHECKED) SendMessage(cb,BM_SETCHECK,BST_UNCHECKED,0);
  135. else SendMessage(cb,BM_SETCHECK,BST_CHECKED,0);
  136. }
  137. }
  138. break;
  139. case 2004:
  140. {
  141. if(HIWORD(wparam)==BN_CLICKED)
  142. {
  143. HWND cb=GetDlgItem(hwnd,id);
  144. UINT stat= SendMessage(cb,BM_GETCHECK,0,0);//获取当前状态
  145. //设置状态
  146. if(stat==BST_CHECKED) SendMessage(cb,BM_SETCHECK,BST_INDETERMINATE,0);
  147. else if(stat==BST_INDETERMINATE) SendMessage(cb,BM_SETCHECK,BST_UNCHECKED,0);
  148. else SendMessage(cb,BM_SETCHECK,BST_CHECKED,0);
  149. }
  150. }
  151. break;
  152. }
  153. }
  154. //回调函数
  155. LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
  156. {
  157. switch(msg)
  158. {
  159. case WM_SYSCOMMAND:
  160. if(wparam==SC_CLOSE){
  161. int ret=MessageBox(NULL,"是否退出","info",MB_YESNO);
  162. if(ret==IDYES){
  163. //以下代码会自己主动关闭和销毁
  164. //PostQuitMessage(0);
  165. }
  166. else return 0;//不运行以下代码
  167. }
  168. break;
  169. //在创建窗体之后还未显示的时候
  170. case WM_CREATE:
  171. OnCreate(hwnd,lparam);
  172. break;
  173. case WM_DESTROY:
  174. PostQuitMessage(0);
  175. break;
  176. case WM_COMMAND:
  177. oncommand(hwnd,wparam);
  178. break;
  179. }
  180. return DefWindowProc(hwnd,msg,wparam,lparam);
  181. }
  182. //注冊窗体类
  183. BOOL Register(LPSTR lpClassName,WNDPROC wndproc)
  184. {
  185. WNDCLASSEX wce={0};
  186. wce.cbSize=sizeof(wce);
  187. wce.cbClsExtra=200;
  188. wce.cbWndExtra=200;
  189. wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  190. wce.hCursor=NULL;
  191. wce.hIcon=NULL;
  192. wce.hIconSm=NULL;
  193. wce.hInstance=g_hinstance;
  194. wce.lpfnWndProc=wndproc;
  195. wce.lpszClassName=lpClassName;
  196. wce.lpszMenuName=NULL;
  197. wce.style=CS_HREDRAW|CS_VREDRAW;
  198. ATOM atom= RegisterClassEx(&wce);
  199. if(atom==0){
  200. MessageBox(NULL,"注冊失败","info",MB_OK);
  201. return FALSE;
  202. }
  203. return TRUE;
  204. }
  205. //创建窗体
  206. HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
  207. {
  208. HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  209. CW_USEDEFAULT,CW_USEDEFAULT,NULL,LoadMenu(g_hinstance,MAKEINTRESOURCE(IDR_MENU1)),g_hinstance,"hello create");
  210. return hwnd;
  211. }
  212. //创建子窗体
  213. HWND CreateChild(HWND phwnd,LPSTR lpClassName,LPSTR lpWndName)
  214. {
  215. if(Register(lpClassName,DefWindowProc)==0)
  216. {
  217. MessageBox(phwnd,"创建子窗体失败","info",MB_OK);
  218. return NULL;
  219. }
  220. //子窗体风格,都要 WS_CHILD|WS_VISIBLE
  221. HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  222. 200,200,phwnd,NULL,g_hinstance,NULL);
  223. return hwnd;
  224. }
  225. //显示窗体
  226. void Display(HWND hwnd)
  227. {
  228. ShowWindow(hwnd,SW_SHOW);
  229. UpdateWindow(hwnd);
  230. }
  231. //处理消息
  232. void MSGdeal()
  233. {
  234. MSG msg={0};
  235. while(GetMessage(&msg,NULL,0,0)){
  236. TranslateMessage(&msg);//翻译消息
  237. DispatchMessage(&msg);//派发给 WinProc 处理消息
  238. }
  239. }
  240. void SetExtra(HWND hwnd)
  241. {
  242. SetClassLong(hwnd,0,301);
  243. SetWindowLong(hwnd,0,401);
  244. }
  245. void GetExtra(HWND hwnd)
  246. {
  247. long nc= GetClassLong(hwnd,0);
  248. long nw=GetWindowLong(hwnd,0);
  249. char txt[200]={0};
  250. sprintf(txt,"%d,%d",nc,nw);
  251. MessageBox(NULL,txt,"info",MB_OK);
  252. }
  253. int APIENTRY WinMain(HINSTANCE hInstance,
  254. HINSTANCE hPrevInstance,
  255. LPSTR lpCmdLine,
  256. int nCmdShow)
  257. {
  258. AllocConsole();//调试程序方法
  259. g_houtput=GetStdHandle(STD_OUTPUT_HANDLE);
  260. g_hinstance=hInstance;
  261. // TODO: Place code here.
  262. //SysReg();
  263. //AppReg();
  264. if(Register("main",WinProc)==0)
  265. {
  266. MessageBox(NULL,"注冊失败","提示",MB_OK);
  267. return 0;
  268. }
  269. HWND hwnd= CreateMain("main","pro1");
  270. //HWND hchild=CreateChild(hwnd,"child1","child1");//创建子窗体
  271. //HWND hchild2=CreateChild(hwnd,"child2","child2");
  272. Display(hwnd);
  273. //MoveWindow(hchild,300,200,200,200,TRUE);
  274. //MoveWindow(hchild2,500,200,200,200,TRUE);
  275. //SetExtra(hwnd);
  276. //GetExtra(hwnd);
  277. MSGdeal();
  278. return 0;
  279. }


第二个程序:

菜单资源:

主要cpp文件的代码:

  1. // win32app.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include "resource.h"
  6. HINSTANCE g_hinstance=0;//全局句柄
  7. HANDLE g_houtput=0;
  8. #include <IOSTREAM>
  9. void OnCreate(HWND hwnd,LPARAM lparam)
  10. {
  11. HWND com1= CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|CBS_SIMPLE,
  12. 50,50,200,200,hwnd,(HMENU)1001,g_hinstance,0);//简单组合框
  13. HWND com2= CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN,
  14. 260,50,200,200,hwnd,(HMENU)1002,g_hinstance,0);//下拉式
  15. HWND com3=CreateWindowEx(0,"COMBOBOX","",WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
  16. 470,50,200,200,hwnd,(HMENU)1003,g_hinstance,0);//下拉列表式
  17. HFONT font=CreateFont(30,0,0,0,900,0,0,0,GB2312_CHARSET,0,0,0,0,"黑体");
  18. SendMessage(com1,WM_SETFONT,(WPARAM)font,0);//设置字体
  19. }
  20. void OnAdd(HWND hwnd)
  21. {
  22. HWND com1=GetDlgItem(hwnd,1001);
  23. HWND com2=GetDlgItem(hwnd,1002);
  24. HWND com3=GetDlgItem(hwnd,1003);
  25. for(int i=0;i<30;i++)
  26. {
  27. char txt[100]={0};
  28. sprintf(txt,"item%d",i);
  29. SendMessage(com1,CB_ADDSTRING,0,(LPARAM)txt);
  30. SendMessage(com2,CB_ADDSTRING,0,(LPARAM)txt);
  31. SendMessage(com3,CB_ADDSTRING,0,(LPARAM)txt);
  32. SendMessage(com2,CB_SETITEMDATA,i,1000+i);//设置附加数据
  33. }
  34. }
  35. void oncommand(HWND hwnd,WPARAM wparam,LPARAM lparam)
  36. {
  37. UINT id=LOWORD(wparam);
  38. int result=0;
  39. HWND dlg=0;
  40. switch(id)
  41. {
  42. case ID_add:
  43. OnAdd(hwnd);
  44. break;
  45. case ID_delete:
  46. {
  47. HWND com2=GetDlgItem(hwnd,1002);
  48. int index=0;//SendMessage(com2,CB_GETCURSEL,0,0);
  49. char* s="item1";
  50. index=SendMessage(com2,CB_FINDSTRING,(WPARAM)-1,(LPARAM)s);//模糊查找,返回第一个匹配的索引
  51. //SendMessage(com2,CB_SELECTSTRING,(WPARAM)-1,(LPARAM)s);//模糊匹配,并设置被选择
  52. if(index!=CB_ERR)
  53. {
  54. int len=(int)SendMessage(com2,CB_GETLBTEXTLEN,(WPARAM)index,0);
  55. char *a=(char *) malloc(len+1);
  56. SendMessage(com2,CB_GETLBTEXT,(WPARAM)index,(LPARAM)a);//取模糊匹配的文字
  57. if(strcmp(s,a)==0)//推断是否全然匹配
  58. {
  59. int data=SendMessage(com2,CB_GETITEMDATA,index,0);
  60. char b[10]={0};
  61. sprintf(b,"%d",data);
  62. MessageBox(NULL,b,"info",MB_OK);
  63. SendMessage(com2,CB_DELETESTRING,index,0);
  64. SendMessage(com2,CB_SETCURSEL,(WPARAM)index,0);
  65. }
  66. free(a);
  67. }
  68. }
  69. break;
  70. case 1001:
  71. {
  72. if(HIWORD(wparam)==CBN_SELCHANGE)
  73. {
  74. HWND com1=GetDlgItem(hwnd,1001);
  75. HWND com2=GetDlgItem(hwnd,1002);
  76. HWND com3=GetDlgItem(hwnd,1003);
  77. int index=SendMessage(com1,CB_GETCURSEL,0,0);
  78. if(index!=CB_ERR)
  79. {
  80. SendMessage(com2,CB_SETCURSEL,index,0);
  81. SendMessage(com3,CB_SETCURSEL,index,0);
  82. }
  83. }
  84. }
  85. break;
  86. }
  87. }
  88. //回调函数
  89. LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
  90. {
  91. switch(msg)
  92. {
  93. case WM_SYSCOMMAND:
  94. if(wparam==SC_CLOSE){
  95. /*
  96. int ret=MessageBox(NULL,"是否退出","info",MB_YESNO);
  97. if(ret==IDYES){
  98. //以下代码会自己主动关闭和销毁
  99. //PostQuitMessage(0);
  100. }
  101. else return 0;//不运行以下代码
  102. */
  103. }
  104. break;
  105. //在创建窗体之后还未显示的时候
  106. case WM_CREATE:
  107. OnCreate(hwnd,lparam);
  108. break;
  109. case WM_DESTROY:
  110. PostQuitMessage(0);
  111. break;
  112. case WM_COMMAND:
  113. oncommand(hwnd,wparam,lparam);
  114. break;
  115. }
  116. return DefWindowProc(hwnd,msg,wparam,lparam);
  117. }
  118. //注冊窗体类
  119. BOOL Register(LPSTR lpClassName,WNDPROC wndproc)
  120. {
  121. WNDCLASSEX wce={0};
  122. wce.cbSize=sizeof(wce);
  123. wce.cbClsExtra=200;
  124. wce.cbWndExtra=200;
  125. wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
  126. wce.hCursor=NULL;
  127. wce.hIcon=NULL;
  128. wce.hIconSm=NULL;
  129. wce.hInstance=g_hinstance;
  130. wce.lpfnWndProc=wndproc;
  131. wce.lpszClassName=lpClassName;
  132. wce.lpszMenuName=NULL;
  133. wce.style=CS_HREDRAW|CS_VREDRAW;
  134. ATOM atom= RegisterClassEx(&wce);
  135. if(atom==0){
  136. MessageBox(NULL,"注冊失败","info",MB_OK);
  137. return FALSE;
  138. }
  139. return TRUE;
  140. }
  141. //创建窗体
  142. HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
  143. {
  144. HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  145. CW_USEDEFAULT,CW_USEDEFAULT,NULL,LoadMenu(g_hinstance,MAKEINTRESOURCE(IDR_MENU1)),g_hinstance,"hello create");
  146. return hwnd;
  147. }
  148. //创建子窗体
  149. HWND CreateChild(HWND phwnd,LPSTR lpClassName,LPSTR lpWndName)
  150. {
  151. if(Register(lpClassName,DefWindowProc)==0)
  152. {
  153. MessageBox(phwnd,"创建子窗体失败","info",MB_OK);
  154. return NULL;
  155. }
  156. //子窗体风格,都要 WS_CHILD|WS_VISIBLE
  157. HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
  158. 200,200,phwnd,NULL,g_hinstance,NULL);
  159. return hwnd;
  160. }
  161. //显示窗体
  162. void Display(HWND hwnd)
  163. {
  164. ShowWindow(hwnd,SW_SHOW);
  165. UpdateWindow(hwnd);
  166. }
  167. //处理消息
  168. void MSGdeal()
  169. {
  170. MSG msg={0};
  171. while(GetMessage(&msg,NULL,0,0)){
  172. TranslateMessage(&msg);//翻译消息
  173. DispatchMessage(&msg);//派发给 WinProc 处理消息
  174. }
  175. }
  176. void SetExtra(HWND hwnd)
  177. {
  178. SetClassLong(hwnd,0,301);
  179. SetWindowLong(hwnd,0,401);
  180. }
  181. void GetExtra(HWND hwnd)
  182. {
  183. long nc= GetClassLong(hwnd,0);
  184. long nw=GetWindowLong(hwnd,0);
  185. char txt[200]={0};
  186. sprintf(txt,"%d,%d",nc,nw);
  187. MessageBox(NULL,txt,"info",MB_OK);
  188. }
  189. int APIENTRY WinMain(HINSTANCE hInstance,
  190. HINSTANCE hPrevInstance,
  191. LPSTR lpCmdLine,
  192. int nCmdShow)
  193. {
  194. AllocConsole();//调试程序方法
  195. g_houtput=GetStdHandle(STD_OUTPUT_HANDLE);
  196. g_hinstance=hInstance;
  197. // TODO: Place code here.
  198. //SysReg();
  199. //AppReg();
  200. if(Register("main",WinProc)==0)
  201. {
  202. MessageBox(NULL,"注冊失败","提示",MB_OK);
  203. return 0;
  204. }
  205. HWND hwnd= CreateMain("main","pro1");
  206. //HWND hchild=CreateChild(hwnd,"child1","child1");//创建子窗体
  207. //HWND hchild2=CreateChild(hwnd,"child2","child2");
  208. Display(hwnd);
  209. //MoveWindow(hchild,300,200,200,200,TRUE);
  210. //MoveWindow(hchild2,500,200,200,200,TRUE);
  211. //SetExtra(hwnd);
  212. //GetExtra(hwnd);
  213. MSGdeal();
  214. return 0;
  215. }






声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号