标签:des style blog http color 使用 os 文件
//设置字体颜色
第一个程序:
资源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);//多态按钮,变相的复选按钮 //*/ } //对话框处理函数 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 <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,对话框和窗口基本控件等的使用,布布扣,bubuko.com
C++windows内核编程笔记day09_day10,对话框和窗口基本控件等的使用
标签:des style blog http color 使用 os 文件
原文地址:http://blog.csdn.net/pukuimin1226/article/details/38176007