天下

记录修行的印记

WM_COMMAND(BN_CLICKED)实现

#include "stdafx.h"
// TestControl.c 
//转自http://www.cnblogs.com/memset/archive/2013/05/16/3081082.html ,稍加修改

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // 某些 CString 构造函数将是显式的
#include <atlbase.h>
#include <atlstr.h>

#include <WindowsX.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(#000000);
            hbrRed = CreateSolidBrush(#ff0000);

            return 0;
        }
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc;
            hdc = BeginPaint(hWnd,&ps);
            SelectObject(hdc,hbrBlack);
            Rectangle(hdc,0,0,100,50);

            {
                CString text;
                ::GetWindowText(hWnd,text.GetBuffer(100),100);
                text.ReleaseBuffer();
                TextOutW(hdc,10,10,text,text.GetLength());
            }

            EndPaint(hWnd,&ps);
            return 0;

        }
    case WM_LBUTTONDOWN:
        {
            HDC hdc;
            hdc = GetDC(hWnd);
            SelectObject(hdc,hbrRed);
            Rectangle(hdc,0,0,100,50);
            SetTextColor(hdc,#ff0000);
            {
                CString text;
                ::GetWindowText(hWnd,text.GetBuffer(100),100);
                text.ReleaseBuffer();
                TextOutW(hdc,10,10,text,text.GetLength());
            }
            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,#000000);
            {
                CString text;
                ::GetWindowText(hWnd,text.GetBuffer(100),100);
                text.ReleaseBuffer();
                TextOutW(hdc,10,10,text,text.GetLength());
            }
            ReleaseDC(hWnd,hdc);
        
            BOOL bRet =  (GetCapture()==hWnd);
            ReleaseCapture();

            HWND parent = ::GetParent(hWnd);
            HMENU hMenu = ::GetMenu(hWnd);

            //POINT pt = MAKEPOINTS(lParam);
            POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
            RECT rect;
            ::GetClientRect(hWnd,&rect);
            bRet = bRet && ::PtInRect(&rect,pt);
            if (bRet)
                ::SendMessage(parent,WM_COMMAND,(WPARAM)hMenu,NULL);
            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_COMMAND:
        {
            int wmId    = LOWORD(wParam);
            CString msg;
            msg.Format(L"hWmd:0x%08x,wmId:%d",hWnd,wmId);
            MessageBox(hWnd,msg,L"提示",MB_OK|MB_ICONINFORMATION);
        }
        break;
    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;
}

posted on 2016-01-20 11:23 天下 阅读(295) 评论(0)  编辑 收藏 引用 所属分类: C/C++Win32


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2016年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(4)

随笔分类(376)

随笔档案(325)

链接

最新随笔

搜索

最新评论