自已动手写控件 --- 我第一次学习写自定义控件
一直都是用系统的控件, 还从不知道一个控件该怎么去写~~~
网上的教程太少了, 完全不知道怎么开头~~
也一直用 RegisterClassEx + CreateWindowEx 创建一个主程序窗口, 却从不知道控件也是通过这种方式来写的.
看来我们无时无刻不在自己写控件啊~~~
第1次写控件, 介绍一下我写的这个简单功能控件的创建过程:
1. 为控件写一个窗口类,指定默认窗口函数,并注册窗口类
2. 调用 CreateWindowEx 创建该子控件即可, 和创建主窗口完全一样
重点:
指定的窗口过程必须要为自己的子控件做好相应的消息处理:比如:鼠标消息, WM_PAINT等~~~
汗, 第1次写这种东西, 没有什么经验, 也没有什么好说的, 就给和我一样不会写控件的一个示范吧, 贴代码, 不多说~~~
实现的功能是:当点击控件时, 控件的背景颜色会发生变化~~~
没什么特别的意义, 只是想告诉不会写控件的大家, 该怎样开始写一个控件~~~
// TestControl.c #include <windows.h> LRESULT CALLBACK ControlWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); void RegisterControlClass(HINSTANCE hInstance) { WNDCLASSEXW wce={0}; wce.cbSize = sizeof(wce); wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wce.hInstance = hInstance; wce.lpfnWndProc = ControlWindowProc; wce.lpszClassName = L"ControlClass"; wce.style = CS_HREDRAW|CS_VREDRAW; RegisterClassExW(&wce); } LRESULT CALLBACK ControlWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { static HBRUSH hbrBlack,hbrRed; switch(uMsg) { case WM_CREATE: { hbrBlack = CreateSolidBrush(RGB(0,0,0)); hbrRed = CreateSolidBrush(RGB(255,0,0)); return 0; } case WM_PAINT: { PAINTSTRUCT ps; HDC hdc; hdc = BeginPaint(hWnd,&ps); SelectObject(hdc,hbrBlack); Rectangle(hdc,0,0,100,50); EndPaint(hWnd,&ps); return 0; } case WM_LBUTTONDOWN: { HDC hdc; hdc = GetDC(hWnd); SelectObject(hdc,hbrRed); Rectangle(hdc,0,0,100,50); SetTextColor(hdc,RGB(255,0,0)); TextOutW(hdc,10,10,L"测试控件",4); ReleaseDC(hWnd,hdc); SetCapture(hWnd); return 0; } case WM_LBUTTONUP: { HDC hdc; hdc = GetDC(hWnd); SelectObject(hdc,hbrBlack); Rectangle(hdc,0,0,100,50); SetTextColor(hdc,RGB(0,0,0)); TextOutW(hdc,10,10,L"测试控件",4); ReleaseDC(hWnd,hdc); ReleaseCapture(); return 0; } case WM_DESTROY: DeleteObject(hbrBlack); DeleteObject(hbrRed); return 0; } return DefWindowProcW(hWnd,uMsg,wParam,lParam); } LRESULT CALLBACK MainWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { switch(uMsg) { case WM_CREATE: { HINSTANCE hInstance = GetModuleHandleW(NULL); CreateWindowExW(0,L"Button",L"按键",WS_CHILD|WS_VISIBLE,0,0,100,50,hWnd,(HMENU)101,hInstance,NULL); CreateWindowExW(0,L"ControlClass",L"哈哈",WS_CHILD|WS_VISIBLE,200,200,100,50,hWnd,(HMENU)100,hInstance,NULL); CreateWindowExW(0,L"ControlClass",L"呵呵",WS_CHILD|WS_VISIBLE,400,400,100,50,hWnd,(HMENU)102,hInstance,NULL); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; default: break; } return DefWindowProcW(hWnd,uMsg,wParam,lParam); } int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nShowCmd) { HWND hWnd; WNDCLASSEXW wce = {0}; MSG msg; RegisterControlClass(hInstance); wce.cbSize = sizeof(wce); wce.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wce.hCursor = LoadCursor(NULL,IDC_ARROW); wce.hIcon = LoadIcon(NULL,IDI_APPLICATION); wce.hInstance = hInstance; wce.lpfnWndProc = MainWindowProc; wce.lpszClassName = L"MyWindowClass"; wce.style = CS_HREDRAW|CS_VREDRAW; RegisterClassExW(&wce); hWnd = CreateWindowExW(0,L"MyWindowClass",L"测试",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL); UpdateWindow(hWnd); ShowWindow(hWnd,nShowCmd); while(GetMessageW(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessageW(&msg); } return 0; }
有图有真相, 字体有点丑, 不过将就, 系统默认字体都这样:
女孩不哭 @ 2013-05-16 02:47:56 @ http://www.cnblogs.com/nbsofer