C++windows内核编程笔记day09_day10,对话框和窗体基本控件等的使用
//设置字体颜色
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;
}
C++windows内核编程笔记day09_day10,对话框和窗体基本控件等的使用的更多相关文章
- Windows程序设计学习笔记(四)自绘控件与贴图的实现
Windows系统提供大量的控件供我们使用,但是系统提供的控件样式都是统一的,不管什么东西看久了自然会厌烦,为了使界面更加美观,添加一些新的东西我们需要自己绘制控件. 控件在默认情况下并不进行自绘,如 ...
- c++windows内核编程笔记day12 硬盘逻辑分区管理、文件管理、内存管理
windows系统磁盘文件存储: 分区格式:NTFS / FAT32 GetSystemDirectory();//获取系统路径 GetWindowsDirectory();//获取windows路径 ...
- C++windows内核编程笔记day13 进程、线程与信号量
Windows进程 进程是一个容器,包括程序运行须要的代码.数据.资源等信息, windows进程的特点: 每一个进程都有自己的ID号 每一个进程都有自己的地址空间.进程之间无法訪问对方的地址空间. ...
- C++windows内核编程笔记day11 win32静态库和动态库的使用
windows库程序: 静态库: 源码被链接到调用的程序或动态库,被调用时,代码最少有1份,文件后缀.LIB 动态库: 函数被程序或其它动态库调用,被调用时,代码仅仅有1份,文件后缀.DLL 静态库( ...
- storysnail的Windows串口编程笔记
storysnail的Windows串口编程笔记 作者 He YiJun – storysnail<at>gmail.com 团队 ls 版权 转载请保留本声明! 本文档包含的原创代码根据 ...
- MFC编程入门之八(对话框:创建对话框类和添加控件变量)
创建好对话框资源后要做的就是生成对话框类了.生成对话框类主要包括新建对话框类.添加控件变量和控件的消息处理函数. 例程Addition是基于对话框的程序,所以程序自动创建了对话框模板IDD_ADDIT ...
- 【转】VS2010/MFC编程入门之八(对话框:创建对话框类和添加控件变量)
原文网址:http://www.jizhuomi.com/software/153.html 前两讲中鸡啄米为大家讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.鸡啄米再声明下, ...
- VS2010/MFC编程入门之八(对话框:创建对话框类和添加控件变量)
前两讲中鸡啄米为大家讲解了如何创建对话框资源.创建好对话框资源后要做的就是生成对话框类了.鸡啄米再声明下,生成对话框类主要包括新建对话框类.添加控件变量和控件的消息处理函数等. 因为鸡啄米给大家的例程 ...
- Windows窗口样式速查参考,Delphi窗口控件的风格都有它们来决定(附Delphi何时用到它们,并举例说明)good
/* 窗口样式参考列表(都是GetWindowLong的GWL_STYLE风格,都是TCreateParams.Sytle的一部分),详细列表如下:https://msdn.microsoft.com ...
随机推荐
- javascript 中遍历数组的简单方法
在Javascript中有自带方便遍历数组的方法(此方法非彼方法不要误会哦): 1 .利用for( index in array ){}; 2.利用 array.forEach( function(e ...
- Markdown规则
第一次写随想,写的不好还请包涵呀!!! 这两天在用markdown写一些文档,感觉还不错,整理一下其规则,对于新手会有帮助. 1. 文章的标题,一般写在首行,第二行添加"---------- ...
- 发布npm时遇到的两个小问题,解决方法
1.出现这个错误 no_perms Private mode enable, only admin can publish this module 错误输出内容 npm ERR! publish Fa ...
- [转]查询sqlserver 正在执行的sql语句的详细信息
包含用户名,所在数据库,执行的sql语句,执行开始时间,驱动程序,主机名称 SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME(sp. ...
- Android 使用GangSDK创建第三方家族公会系统经验分享
由于需要对之前的游戏加入一个家族系统,想到这块儿可能会有大量的工作需要自己做,就偷了个懒去网上搜罗了一波,有没有类似现成的系统?结果让我惊奇的发现,目前市面上居然真的有类似的服务,虽然是小公司开发的, ...
- Python 日志处理(一) 按Nginx log_format 分割日志记录
要求:不使用正则 根据nginx 默认的日志记录格式,分割日志记录. log_format main '$remote_addr - $remote_user [$time_local] " ...
- Mybatis3 快速入门
Mybatis3 快速入门 目前常见的持久层java框架有Hibernate,Mybatis,SpringData.笔者比较喜欢用SpringData.Hibernate 和 Mybatis 也经常用 ...
- geotrellis使用(三十五)Cesium加载geotrellis TMS瓦片
前言 做任何事情都不是想象中的那么简单.好久没有更新技术博客了,跟最近瞎忙有很大关系,虽说是瞎忙也抽空研究了些技术. 主要是前端渲染,像原生的WebGL和Cesium.WebGL写了几篇博客,自我感觉 ...
- 实现JavaScript forEach
function forEach(list, callback){ ; n <list.length; n++){ callback.call(list[n], n); } } ,,,,,,,] ...
- JSP6(JSP 指令与JSP 动作元素)
一.JSP指令用来设置整个JSP页面相关的属性 指令可以有很多个属性,它们以键值对的形式存在,并用逗号隔开. JSP中的三种指令标签: Page指令 Page指令为容器提供当前页面的使用说明.一个JS ...