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);//發消息修改靜態圖標的圖片
按鈕:
普通下壓式按鈕:BS_PUSHBUTTON、BS_DEFPUSHBUTTON
分組框按鈕:BS_GROUPBOX
復選框按鈕:BS_CHECKBOX、BS_AUTOCHECKBOX、BS_3STATE、BS_AUTO3STATE
單選框按鈕: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);
//多態按鈕,變相的復選按鈕
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,可輸入回車
密碼編輯框: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<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPtb30qpjcHC0+sLro7o8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">// win32app.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include
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);//多態按鈕,變相的復選按鈕
//*/
}
//對話框處理函數
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,"按鈕點擊","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
#include "resource.h"
HINSTANCE g_hinstance=0;//全局句柄
HANDLE g_houtput=0;
#include
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;
}