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代码:
- // win32app.cpp : Defines the entry point for the application.
- //
-
- #include "stdafx.h"
- #include "resource.h"
- #include <stdio.h>
- HINSTANCE g_hinstance=0;//全局句柄
- HANDLE g_houtput=0;
- void OnCreate(HWND hwnd,LPARAM lparam)
- {
- // char txt[200]={0};
- // ScreenToClient(hwnd,&pt);
- // sprintf(txt,"%d,%d ; %d,%d,%d,%d\n",pt.x,pt.y,rc.top,rc.left,rc.right,rc.bottom);
- //
- // WriteConsole(g_houtput,txt,strlen(txt),NULL,NULL);
- /*
- CreateWindowEx(0,"STATIC","hello",WS_CHILD|WS_VISIBLE,100,100,
- 200,40,hwnd,(HMENU)1001,g_hinstance,0);
- CreateWindowEx(0,"STATIC","#104",WS_CHILD|WS_VISIBLE|SS_ICON|SS_NOTIFY,300,100,
- 0,0,hwnd,(HMENU)1002,g_hinstance,0);
- */
- ///*
- CreateWindowEx(0,"BUTTON","GroupBox",WS_CHILD|WS_VISIBLE|BS_GROUPBOX,30,30,
- 300,400,hwnd,(HMENU)2000,g_hinstance,0);
- CreateWindowEx(0,"BUTTON","btn1",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,50,50,
- 40,20,hwnd,(HMENU)2001,g_hinstance,0);
-
- CreateWindowEx(0,"BUTTON","Check A",WS_CHILD|WS_VISIBLE|BS_CHECKBOX,50,100,
- 100,30,hwnd,(HMENU)2002,g_hinstance,0);
- CreateWindowEx(0,"BUTTON","Check B",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,50,140,
- 100,30,hwnd,(HMENU)2003,g_hinstance,0);
- CreateWindowEx(0,"BUTTON","3state C",WS_CHILD|WS_VISIBLE|BS_3STATE,50,180,
- 100,30,hwnd,(HMENU)2004,g_hinstance,0);
- CreateWindowEx(0,"BUTTON","3state D",WS_CHILD|WS_VISIBLE|BS_AUTO3STATE,50,220,
- 100,30,hwnd,(HMENU)2005,g_hinstance,0);//3种状态复选框
- 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);
- CreateWindowEx(0,"BUTTON","RADIO I",WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX|BS_PUSHLIKE,50,420,
- 100,30,hwnd,(HMENU)2010,g_hinstance,0);//多态button,变相的复选button
- //*/
-
- }
- //对话框处理函数
- int CALLBACK DialogProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
- {
- 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;
- }
- int CALLBACK DialogProc2(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_SYSCOMMAND:
- if(wParam==SC_CLOSE)
- {
- DestroyWindow(hwnd);
- return true;
- }
- break;
- case WM_INITDIALOG://对话框创建之后、显示之前
- {
-
- return true;
- }
- break;
-
- }
- return false;
- }
- void oncommand(HWND hwnd,WPARAM wparam)
- {
- UINT id=LOWORD(wparam);
- int result=0;
- HWND dlg=0;
- switch(id)
- {
- case Dlg_Model:
- result=(int)DialogBox(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DialogProc);
- break;
- case Dlg_Normal:
- dlg= CreateDialog(g_hinstance,MAKEINTRESOURCE(IDD_DIALOG2),hwnd,DialogProc2);
- ShowWindow(dlg,1);
- break;
- case ID_Blue:
- {
- HICON ico=LoadIcon(g_hinstance,MAKEINTRESOURCE(IDI_ICON2));
- SendMessage(GetDlgItem(hwnd,1002),STM_SETICON,(WPARAM)ico,0);
- break;
- }
-
- case ID_Black:
- {
- HICON ico=LoadIcon(g_hinstance,MAKEINTRESOURCE(IDI_ICON1));
- HWND hs=GetDlgItem(hwnd,1002);//通过窗体句柄和控件ID号获取控件句柄
- SendMessage(hs,STM_SETICON,(WPARAM)ico,0);//改动静态图标的图片
- break;
- }
- case 1002:
- {
-
- if(HIWORD(wparam)==STN_DBLCLK)
- {
- MessageBox(NULL,"双击图标","info",MB_OK);
- }
- }
- break;
- case 2001:
-
- {
- if(HIWORD(wparam)==BN_CLICKED)
- {
- MessageBox(NULL,"button点击","info",MB_OK);
- }
-
- }
- case 2002:
- {
- if(HIWORD(wparam)==BN_CLICKED)
- {
- HWND cb=GetDlgItem(hwnd,id);
- 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);
- }
- }
- break;
- case 2004:
- {
- if(HIWORD(wparam)==BN_CLICKED)
- {
- 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);
- }
- }
- break;
- }
- }
- //回调函数
- LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch(msg)
- {
- case WM_SYSCOMMAND:
- if(wparam==SC_CLOSE){
- int ret=MessageBox(NULL,"是否退出","info",MB_YESNO);
- if(ret==IDYES){
- //以下代码会自己主动关闭和销毁
- //PostQuitMessage(0);
- }
- else return 0;//不运行以下代码
- }
- break;
- //在创建窗体之后还未显示的时候
- case WM_CREATE:
- OnCreate(hwnd,lparam);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_COMMAND:
- oncommand(hwnd,wparam);
- break;
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }
- //注冊窗体类
- BOOL Register(LPSTR lpClassName,WNDPROC wndproc)
- {
- WNDCLASSEX wce={0};
- wce.cbSize=sizeof(wce);
- wce.cbClsExtra=200;
- wce.cbWndExtra=200;
- wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
- wce.hCursor=NULL;
- wce.hIcon=NULL;
- wce.hIconSm=NULL;
- wce.hInstance=g_hinstance;
- wce.lpfnWndProc=wndproc;
- wce.lpszClassName=lpClassName;
- wce.lpszMenuName=NULL;
- wce.style=CS_HREDRAW|CS_VREDRAW;
- ATOM atom= RegisterClassEx(&wce);
- if(atom==0){
- MessageBox(NULL,"注冊失败","info",MB_OK);
- return FALSE;
- }
- return TRUE;
- }
- //创建窗体
- HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
- {
- HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
- CW_USEDEFAULT,CW_USEDEFAULT,NULL,LoadMenu(g_hinstance,MAKEINTRESOURCE(IDR_MENU1)),g_hinstance,"hello create");
- return hwnd;
- }
- //创建子窗体
- HWND CreateChild(HWND phwnd,LPSTR lpClassName,LPSTR lpWndName)
- {
- if(Register(lpClassName,DefWindowProc)==0)
- {
- MessageBox(phwnd,"创建子窗体失败","info",MB_OK);
- return NULL;
- }
- //子窗体风格,都要 WS_CHILD|WS_VISIBLE
- HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
- 200,200,phwnd,NULL,g_hinstance,NULL);
- return hwnd;
- }
- //显示窗体
- void Display(HWND hwnd)
- {
- ShowWindow(hwnd,SW_SHOW);
- UpdateWindow(hwnd);
- }
- //处理消息
- void MSGdeal()
- {
- MSG msg={0};
- while(GetMessage(&msg,NULL,0,0)){
- TranslateMessage(&msg);//翻译消息
- DispatchMessage(&msg);//派发给 WinProc 处理消息
- }
- }
- void SetExtra(HWND hwnd)
- {
- SetClassLong(hwnd,0,301);
- SetWindowLong(hwnd,0,401);
- }
- void GetExtra(HWND hwnd)
- {
- long nc= GetClassLong(hwnd,0);
- long nw=GetWindowLong(hwnd,0);
- char txt[200]={0};
- sprintf(txt,"%d,%d",nc,nw);
- MessageBox(NULL,txt,"info",MB_OK);
- }
-
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- AllocConsole();//调试程序方法
- g_houtput=GetStdHandle(STD_OUTPUT_HANDLE);
- g_hinstance=hInstance;
- // TODO: Place code here.
- //SysReg();
- //AppReg();
- if(Register("main",WinProc)==0)
- {
- MessageBox(NULL,"注冊失败","提示",MB_OK);
- return 0;
- }
- HWND hwnd= CreateMain("main","pro1");
- //HWND hchild=CreateChild(hwnd,"child1","child1");//创建子窗体
- //HWND hchild2=CreateChild(hwnd,"child2","child2");
-
- Display(hwnd);
- //MoveWindow(hchild,300,200,200,200,TRUE);
- //MoveWindow(hchild2,500,200,200,200,TRUE);
- //SetExtra(hwnd);
- //GetExtra(hwnd);
- MSGdeal();
- return 0;
- }
-
-
第二个程序:
菜单资源:
主要cpp文件的代码:
- // win32app.cpp : Defines the entry point for the application.
- //
-
- #include "stdafx.h"
- #include <stdio.h>
- #include "resource.h"
- HINSTANCE g_hinstance=0;//全局句柄
- HANDLE g_houtput=0;
- #include <IOSTREAM>
- void OnCreate(HWND hwnd,LPARAM lparam)
- {
-
- 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);//设置字体
- }
- void OnAdd(HWND hwnd)
- {
- HWND com1=GetDlgItem(hwnd,1001);
- HWND com2=GetDlgItem(hwnd,1002);
- HWND com3=GetDlgItem(hwnd,1003);
- for(int i=0;i<30;i++)
- {
- char txt[100]={0};
- sprintf(txt,"item%d",i);
- SendMessage(com1,CB_ADDSTRING,0,(LPARAM)txt);
- SendMessage(com2,CB_ADDSTRING,0,(LPARAM)txt);
- SendMessage(com3,CB_ADDSTRING,0,(LPARAM)txt);
- SendMessage(com2,CB_SETITEMDATA,i,1000+i);//设置附加数据
- }
- }
- void oncommand(HWND hwnd,WPARAM wparam,LPARAM lparam)
- {
- UINT id=LOWORD(wparam);
- int result=0;
- HWND dlg=0;
- switch(id)
- {
- case ID_add:
- OnAdd(hwnd);
- break;
- case ID_delete:
- {
- HWND com2=GetDlgItem(hwnd,1002);
- int index=0;//SendMessage(com2,CB_GETCURSEL,0,0);
- char* s="item1";
-
- index=SendMessage(com2,CB_FINDSTRING,(WPARAM)-1,(LPARAM)s);//模糊查找,返回第一个匹配的索引
- //SendMessage(com2,CB_SELECTSTRING,(WPARAM)-1,(LPARAM)s);//模糊匹配,并设置被选择
- if(index!=CB_ERR)
- {
-
- int len=(int)SendMessage(com2,CB_GETLBTEXTLEN,(WPARAM)index,0);
- char *a=(char *) malloc(len+1);
- SendMessage(com2,CB_GETLBTEXT,(WPARAM)index,(LPARAM)a);//取模糊匹配的文字
- if(strcmp(s,a)==0)//推断是否全然匹配
- {
- int data=SendMessage(com2,CB_GETITEMDATA,index,0);
- char b[10]={0};
- sprintf(b,"%d",data);
- MessageBox(NULL,b,"info",MB_OK);
- SendMessage(com2,CB_DELETESTRING,index,0);
- SendMessage(com2,CB_SETCURSEL,(WPARAM)index,0);
- }
- free(a);
- }
- }
- break;
- case 1001:
- {
- 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;
- }
- }
- //回调函数
- LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
- {
- switch(msg)
- {
- case WM_SYSCOMMAND:
- if(wparam==SC_CLOSE){
- /*
- int ret=MessageBox(NULL,"是否退出","info",MB_YESNO);
- if(ret==IDYES){
- //以下代码会自己主动关闭和销毁
- //PostQuitMessage(0);
- }
- else return 0;//不运行以下代码
- */
- }
- break;
- //在创建窗体之后还未显示的时候
- case WM_CREATE:
- OnCreate(hwnd,lparam);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_COMMAND:
- oncommand(hwnd,wparam,lparam);
- break;
-
- }
- return DefWindowProc(hwnd,msg,wparam,lparam);
- }
- //注冊窗体类
- BOOL Register(LPSTR lpClassName,WNDPROC wndproc)
- {
- WNDCLASSEX wce={0};
- wce.cbSize=sizeof(wce);
- wce.cbClsExtra=200;
- wce.cbWndExtra=200;
- wce.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
- wce.hCursor=NULL;
- wce.hIcon=NULL;
- wce.hIconSm=NULL;
- wce.hInstance=g_hinstance;
- wce.lpfnWndProc=wndproc;
- wce.lpszClassName=lpClassName;
- wce.lpszMenuName=NULL;
- wce.style=CS_HREDRAW|CS_VREDRAW;
- ATOM atom= RegisterClassEx(&wce);
- if(atom==0){
- MessageBox(NULL,"注冊失败","info",MB_OK);
- return FALSE;
- }
- return TRUE;
- }
- //创建窗体
- HWND CreateMain(LPSTR lpClassName,LPSTR lpWndName)
- {
- HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
- CW_USEDEFAULT,CW_USEDEFAULT,NULL,LoadMenu(g_hinstance,MAKEINTRESOURCE(IDR_MENU1)),g_hinstance,"hello create");
- return hwnd;
- }
- //创建子窗体
- HWND CreateChild(HWND phwnd,LPSTR lpClassName,LPSTR lpWndName)
- {
- if(Register(lpClassName,DefWindowProc)==0)
- {
- MessageBox(phwnd,"创建子窗体失败","info",MB_OK);
- return NULL;
- }
- //子窗体风格,都要 WS_CHILD|WS_VISIBLE
- HWND hwnd=CreateWindowEx(0,lpClassName,lpWndName,WS_CHILD|WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
- 200,200,phwnd,NULL,g_hinstance,NULL);
- return hwnd;
- }
- //显示窗体
- void Display(HWND hwnd)
- {
- ShowWindow(hwnd,SW_SHOW);
- UpdateWindow(hwnd);
- }
- //处理消息
- void MSGdeal()
- {
- MSG msg={0};
- while(GetMessage(&msg,NULL,0,0)){
- TranslateMessage(&msg);//翻译消息
- DispatchMessage(&msg);//派发给 WinProc 处理消息
- }
- }
- void SetExtra(HWND hwnd)
- {
- SetClassLong(hwnd,0,301);
- SetWindowLong(hwnd,0,401);
- }
- void GetExtra(HWND hwnd)
- {
- long nc= GetClassLong(hwnd,0);
- long nw=GetWindowLong(hwnd,0);
- char txt[200]={0};
- sprintf(txt,"%d,%d",nc,nw);
- MessageBox(NULL,txt,"info",MB_OK);
- }
-
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- AllocConsole();//调试程序方法
- g_houtput=GetStdHandle(STD_OUTPUT_HANDLE);
- g_hinstance=hInstance;
- // TODO: Place code here.
- //SysReg();
- //AppReg();
- if(Register("main",WinProc)==0)
- {
- MessageBox(NULL,"注冊失败","提示",MB_OK);
- return 0;
- }
- HWND hwnd= CreateMain("main","pro1");
- //HWND hchild=CreateChild(hwnd,"child1","child1");//创建子窗体
- //HWND hchild2=CreateChild(hwnd,"child2","child2");
-
- Display(hwnd);
- //MoveWindow(hchild,300,200,200,200,TRUE);
- //MoveWindow(hchild2,500,200,200,200,TRUE);
- //SetExtra(hwnd);
- //GetExtra(hwnd);
- MSGdeal();
- return 0;
- }
-
-