博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
win32自定义控件(虽然不美观,但对理解很有好处)
阅读量:5931 次
发布时间:2019-06-19

本文共 3795 字,大约阅读时间需要 12 分钟。

win32的自定义控件不是很美观,但是对控件有个比较底层的认识,值了。

#include 
#include "resource.h"#define CLSNAME "About3"#define WNDNAME "about3 demo"LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );LRESULT CALLBACK EllipPushWndProc(HWND,UINT,WPARAM,LPARAM);BOOL CALLBACK DialogProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){ static TCHAR clsname[] = TEXT(CLSNAME); WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WindowProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = clsname ; wndclass.lpszClassName = clsname ; if(!::RegisterClass(&wndclass)) { MessageBox ( NULL, TEXT ("This program requires Windows NT!"), clsname, MB_ICONERROR) ; return 0 ; } //注册自定义窗口类 wndclass.style = CS_HREDRAW|CS_VREDRAW; wndclass.lpfnWndProc = EllipPushWndProc;//here wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = NULL; wndclass.hCursor = ::LoadCursor(NULL,IDC_ARROW); wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);//here wndclass.lpszMenuName = NULL; wndclass.lpszClassName = TEXT("EllipPush");//here //资源id必须和类名一样(不分大小写) ::RegisterClass(&wndclass);//注册 HWND hwnd; hwnd = ::CreateWindow(clsname,TEXT(WNDNAME), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); ::ShowWindow(hwnd,SW_SHOW); ::UpdateWindow(hwnd); MSG msg; while(::GetMessage(&msg,NULL,0,0)){ ::TranslateMessage(&msg); ::DispatchMessage(&msg); } return msg.wParam;}LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){ static HINSTANCE hInstance; switch(uMsg) { case WM_CREATE: hInstance = (HINSTANCE)::GetWindowLong(hwnd,GWL_HINSTANCE); return 0; case WM_COMMAND: switch(LOWORD(wParam)) { case IDM_APP_ABOUT: DialogBox(hInstance,TEXT("AboutBox"),hwnd,DialogProc); return 0; } break; case WM_KEYDOWN: switch(wParam) { case VK_RETURN: ::SendMessage(hwnd,WM_COMMAND,IDM_APP_ABOUT,0); return 0; } break; case WM_DESTROY: ::PostQuitMessage(0); return 0; } return ::DefWindowProc(hwnd,uMsg,wParam,lParam);}BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ){ switch(uMsg) { case WM_INITDIALOG: return true; case WM_COMMAND: switch(LOWORD(wParam)) { case IDOK: case IDCANCEL: ::EndDialog(hwndDlg,0); return true; } break; } return FALSE;}//自定义控件过程LRESULT CALLBACK EllipPushWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ){ TCHAR szText[40]; HBRUSH hBrush; HDC hdc; PAINTSTRUCT ps; RECT rect; switch(uMsg) { case WM_PAINT: ::GetClientRect(hwndDlg,&rect); ::GetWindowText(hwndDlg,szText,sizeof(szText)); hdc = ::BeginPaint(hwndDlg,&ps); hBrush = ::CreateSolidBrush(::GetSysColor(COLOR_BTNFACE)); hBrush = (HBRUSH)::SelectObject(hdc,hBrush); ::SetBkColor(hdc,GetSysColor(COLOR_BTNFACE));//设置背景颜色 ::SetTextColor(hdc,GetSysColor(COLOR_WINDOWTEXT));//设置文本颜色 ::Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);//着色 ::DrawText(hdc,szText,-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);//又收获一个财富 ::DeleteObject(::SelectObject(hdc,hBrush)); return 0; case WM_KEYUP: if(wParam != VK_SPACE)//这里如果是空格键就fall through break; //如果点击了鼠标左键,就给主窗口发消息 case WM_LBUTTONDOWN: ::SendMessage(::GetParent(hwndDlg),WM_COMMAND, MAKEWORD(::GetWindowLong(hwndDlg,GWL_ID),0), (LPARAM)hwndDlg); return 0; } return ::DefWindowProc(hwndDlg,uMsg,wParam,lParam);}

转载地址:http://kfktx.baihongyu.com/

你可能感兴趣的文章
cobbler------跨网段装机
查看>>
我的友情链接
查看>>
图像基本处理算法的简单实现(二)
查看>>
jqGrid中prmNames,jsonReader,colModel的参数简介
查看>>
SugarCRM之在EditViews中调用js
查看>>
唯品会等被纳入MSCI指数,中概股迎来春天
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
MongoDB负载信息一目了然 阿里云HDM重磅发布MongoDB监控和诊断功
查看>>
VS2005相关----显示最近打开项目
查看>>
IPsec ××× (下)
查看>>
我的友情链接
查看>>
linux各个发行版的区别
查看>>
mysql客户端连接失败处理方法(can't connect to MySQL server on localhost'(10060))
查看>>
动态路由之OSPF高级应用
查看>>
vsftpd配置虚拟用户
查看>>
cesiumjs开发实践(二) Viewer控件
查看>>
学习NGUI前的准备NGUI的相关信息
查看>>
python内置数据结构
查看>>
Neutron - mtu值太大导致虚拟机无法上百度
查看>>