精华内容
下载资源
问答
  • 自己做个"WINDOWS按钮突破大师" -------------------------------------------------------------------------------- --------------Enable You!.cpp----------------------- 入口函数 #include "m

    自己做个"WINDOWS按钮突破大师"

     

     

    --------------------------------------------------------------------------------

     

     

     

     

    --------------Enable You!.cpp-----------------------

     

    入口函数

     

    #include "main.h"

    #include "resource.h"

    int APIENTRY WinMain(HINSTANCE hInstance,

                         HINSTANCE hPrevInstance,

                         LPSTR     lpCmdLine,

                         int       nCmdShow)

    {

        DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, Main_Proc);

        return(0);

    }

     

    ------------------main.cpp----------------------------------------

     

    #include <windows.h>

    #include <windowsx.h>

    #include "main.h"

    #include "resource.h"

    #include "stdio.h"

    #include "stdlib.h"

    int i;

    typedef unsigned (__stdcall *LPTHREADFUNC)(void *);

    BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

    {

        switch(uMsg)

        {

            //BEGIN MESSAGE CRACK

            HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);

            HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);

            HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);

            //END MESSAGE CRACK

        }

        return FALSE;

    }

    //  Main_OnInitDialog

    BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)

    {

     // Set app icons

        HICON hIcon = LoadIcon((HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE) ,MAKEINTRESOURCE(IDI_ICONAPP));

        SendMessage(hwnd, WM_SETICON, TRUE,  (LPARAM)hIcon);

        SendMessage(hwnd, WM_SETICON, FALSE, (LPARAM)hIcon);

     

        int k=0;

        SetWindowText(GetDlgItem(hwnd,IDC_EDIT_TIME),"5");

       

        return TRUE;

    }

    //  Main_OnCommand

    void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)

    {

        char temp2[64],temp[64],Time[64];

        int k=0;

        HMENU hMenu,hSubMenu;

        HWND Main;

        switch(id)

        {

            case IDC_BUTTON_ENABLE:

                i=0;

                GetWindowText(GetDlgItem(hwnd,IDC_EDIT_TIME),Time,GetWindowTextLength(GetDlgItem(hwnd,IDC_EDIT_TIME))+1);

                sprintf(temp2,"程序将在%d秒后启动,请做好准备,迅速切换至要执行突破的窗口.(^_^)",atoi(Time));

                MessageBox(hwnd,temp2,"提示",MB_OK);

                Sleep(atoi(Time)*1000);

                Main=GetForegroundWindow();

       SendMessage(GetDlgItem(hwnd,IDC_EDIT_DEBUG),EM_REPLACESEL,1,(LPARAM)"Discover Symbols:.../n");

                EnumChildWindows(Main,EnumChildProc,(LPARAM)Main) ;

     

       //Enable Menu

                hMenu=GetMenu(Main);

       itoa(GetMenuItemCount(hMenu),temp2,10);

       wsprintf(temp,"Discover MenuItems :%d /n",temp2);

       SendMessage(GetDlgItem(hwnd,IDC_EDIT_DEBUG),EM_REPLACESEL,1,(LPARAM)temp);

                for (;k<GetMenuItemCount(hMenu);k++)

                {

        EnableMenuItem(hMenu,GetMenuItemID(hMenu,k),MF_ENABLED);

        hSubMenu=GetSubMenu(hMenu,0);

        EnableMenuItem(hSubMenu,GetMenuItemID(hSubMenu,0),MF_ENABLED);

                }

                SendMessage(GetDlgItem(hwnd,IDC_EDIT_DEBUG),EM_REPLACESEL,1,(LPARAM)"完成!");

            break;

      case IDC_BUTTON_HELP:

       MessageBox(hwnd,

        "Windows按钮突破者(Enable You!),彻底摧毁灰色控件!/n在Time:框中填入开始时间(秒)/n/t/ Made By 全金属外壳"

        ,"About",MB_OK);

            break;

      default:break;

        }

    }

    //  Main_OnClose

    void Main_OnClose(HWND hwnd)

    {

        EndDialog(hwnd, 0);

    }

    BOOL CALLBACK EnumChildProc( HWND hwndChild,LPARAM lParam_hmain)

    {

        EnableWindow(hwndChild,TRUE) ;

        return true;

    }

    //

     

     
    展开全文
  • delphi中为自定控件绘制windows按钮

    千次阅读 2007-05-19 17:13:00
    若要自定工具拦,但也要保留windows样式的按钮时下面给出一种实现procedure TmyButton.defaultdraw;var arect,R:Trect; wpos,hpos:integer; Details: TThemedElementDetails;begin hpos:=0; arect:=self....
     
    

    若要自定工具拦,但也要保留windows样式的按钮时

    下面给出一种实现

    procedure TmyButton.defaultdraw;
    var arect,R:Trect;
        wpos,hpos:integer;
        Details: TThemedElementDetails;
    begin
      hpos:=0;
      arect:=self.ClientRect;
      canvas.Font:=font;
      canvas.Brush.Color:=$CCFFFF;
      if assigned(fimagelist) then
      begin
        if (fimagelist.Count>0) or (fimage>=0) then
        begin
          wpos:=(width-imagelist.Width) div 2;
          if fhot and ( not (csDesigning in ComponentState) ) then
          begin
            R:=rect(wpos-8,0,wpos+fimagelist.width+8,fimagelist.Height+16);
            if ThemeServices.ThemesEnabled then
            begin
              if fdown then
              begin
                Details := ThemeServices.GetElementDetails(themes.ttbButtonPressed);
              end
              else
                Details := ThemeServices.GetElementDetails(themes.ttbButtonHot);
              ThemeServices.DrawElement(Canvas.Handle, Details, R, nil);
            end
            else
            begin
              if fdown then
              begin
                canvas.FillRect(r);
                DrawEdge(Canvas.Handle, R, BDR_SUNKENOUTER, BF_RECT);
              end
              else
                DrawEdge(Canvas.Handle, R, BDR_RAISEDINNER, BF_RECT);
            end;
          end;
          fimagelist.Draw(canvas,wpos,8,fimage);
          hpos:=hpos+fimagelist.Height+20;
        end;
        canvas.Brush.Style:=bsclear;
        wpos:=(width-canvas.textwidth(fcaption)) div 2;
        canvas.TextOut(wpos,hpos,fcaption);
        exit;
      end;
      wpos:=(width-canvas.textwidth(fcaption)) div 2;
      hpos:=4;
      canvas.TextOut(wpos,hpos,fcaption);
      if fhot and ( not (csDesigning in ComponentState) ) then
      begin
        if fdown then
          DrawEdge(canvas.Handle,arect,EDGE_ETCHED,BF_FLAT or BF_RECT)
        else
          DrawEdge(canvas.Handle,arect,EDGE_RAISED,BF_FLAT or BF_RECT);
      end;
    end;

     
    展开全文
  • Windows SDK 按钮

    2017-06-25 17:36:11
    按钮作为window的一种, 可以通过CreateWindow来进行创建 如下代码创建了一个最基本的push button, 按下后会触发WM_...#include <windows.h>#define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 /* 全局变量定义区 */

    按钮作为window的一种, 可以通过CreateWindow来进行创建
    如下代码创建了一个最基本的push button, 按下后会触发WM_COMMAND消息

    PushButton代码

    此代码显示了一个按钮, 按下后显示一个消息框

    
    #include <windows.h>
    
    #define WINDOW_WIDTH 800
    #define WINDOW_HEIGHT 600
    
    
    /* 全局变量定义区 */
    char* g_szApplicationName = "AppName";
    char* g_szWindowClassName = "windowClassName";
    
    /* 消息回调函数 */
    LRESULT CALLBACK WindowProc(HWND   hwnd,
        UINT   msg,
        WPARAM wParam,
        LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
    
        switch (msg)
        {
        case WM_CREATE:
            CreateWindow(TEXT("Button"), TEXT("PUSHBUTTON"), 
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
                30, 30, 200, 50,
                hwnd, (HMENU)1, 
                ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            return 0;
        case WM_COMMAND:    //按钮被按下后的响应
            MessageBox(hwnd, TEXT("button pressed"), TEXT("Info"), MB_OK);
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            DrawText(hdc, TEXT("This is a window program"), -1, &rect,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            //终了程序,发送WM_QUIT消息  
            PostQuitMessage(0);
            break;
        }
    
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR     szCmdLine,
        int       iCmdShow)
    {
        HWND hWnd;  //窗口句柄
        WNDCLASSEX winclass;    //窗口类对象
    
                                //窗口类对象的初始化
        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hInstance;
        winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = NULL;
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = g_szWindowClassName;
        winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        //注册窗口类
        if (!RegisterClassEx(&winclass))
        {
            MessageBox(NULL, "Registration Failed!", "Error", 0);
            return 0;
        }
    
        //创建窗口  
        hWnd = CreateWindowEx(NULL,                 // extended style
            g_szWindowClassName,  // window class name
            g_szApplicationName,  // window caption
            WS_OVERLAPPEDWINDOW,  // window style
            0,                    // initial x position
            0,                    // initial y position
            WINDOW_WIDTH,         // initial x size
            WINDOW_HEIGHT,        // initial y size
            NULL,                 // parent window handle
            NULL,                 // window menu handle
            hInstance,            // program instance handle
            NULL);                // creation parameters
    
                                  //容错处理
        if (!hWnd)
        {
            MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0);
            return 0;
        }
    
        //显示窗口
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    

    其他类型按钮展示:

    如下代码显示了常见按钮样式, 执行效果如下图
    这里写图片描述

    
    
    #include <windows.h>
    
    #define WINDOW_WIDTH 800
    #define WINDOW_HEIGHT 600
    
    struct
    {
        int iStyle;
        TCHAR *szText;
    } button[] = {  //按钮的样式
        BS_PUSHBUTTON,      TEXT("PUSHBUTTON"),
        BS_DEFPUSHBUTTON,   TEXT("BS_DEFPUSHBUTTON"),
        BS_CHECKBOX,        TEXT("BS_CHECKBOX"),
        BS_AUTOCHECKBOX,    TEXT("BS_AUTOCHECKBOX"),
        BS_RADIOBUTTON,     TEXT("BS_RADIOBUTTON"),
        BS_3STATE,          TEXT("BS_3STATE"),
        BS_AUTO3STATE,      TEXT("BS_AUTO3STATE"),
        BS_GROUPBOX,        TEXT("BS_GROUPBOX"),
        BS_USERBUTTON,      TEXT("BS_USERBUTTON"),
        BS_AUTORADIOBUTTON, TEXT("BS_AUTORADIOBUTTON"),
        BS_PUSHBOX,         TEXT("BS_PUSHBOX"),
        BS_OWNERDRAW,       TEXT("BS_OWNERDRAW"),
    };
    
    #define BUTTON_NUM (sizeof button / sizeof button[0])
    
    
    /* 全局变量定义区 */
    char* g_szApplicationName = "AppName";
    char* g_szWindowClassName = "windowClassName";
    
    /* 消息回调函数 */
    LRESULT CALLBACK WindowProc(HWND   hwnd,
        UINT   msg,
        WPARAM wParam,
        LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        static int cxChar, cyChar;
        int i;
    
        switch (msg)
        {
        case WM_CREATE:
            cxChar = LOWORD(GetDialogBaseUnits());
            cyChar = HIWORD(GetDialogBaseUnits());
    
            for (i = 0; i < BUTTON_NUM; ++i)
            {
                CreateWindow(TEXT("Button"), button[i].szText,
                    WS_CHILD | WS_VISIBLE | button[i].iStyle,
                    cxChar, cyChar * (1 + 2 * i),
                    20 * cxChar, 2 * cyChar, 
                    hwnd, (HMENU)i,
                    ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            }
    
            return 0;
        case WM_COMMAND:
            MessageBox(hwnd, TEXT("button pressed"), TEXT("Info"), MB_OK);
            return 0;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            GetClientRect(hwnd, &rect);
            DrawText(hdc, TEXT("This is a window program"), -1, &rect,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            EndPaint(hwnd, &ps);
            break;
        case WM_DESTROY:
            //终了程序,发送WM_QUIT消息  
            PostQuitMessage(0);
            break;
        }
    
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR     szCmdLine,
        int       iCmdShow)
    {
        HWND hWnd;  //窗口句柄
        WNDCLASSEX winclass;    //窗口类对象
    
                                //窗口类对象的初始化
        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hInstance;
        winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = NULL;
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = g_szWindowClassName;
        winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        //注册窗口类
        if (!RegisterClassEx(&winclass))
        {
            MessageBox(NULL, "Registration Failed!", "Error", 0);
            return 0;
        }
    
        //创建窗口  
        hWnd = CreateWindowEx(NULL,                 // extended style
            g_szWindowClassName,  // window class name
            g_szApplicationName,  // window caption
            WS_OVERLAPPEDWINDOW,  // window style
            0,                    // initial x position
            0,                    // initial y position
            WINDOW_WIDTH,         // initial x size
            WINDOW_HEIGHT,        // initial y size
            NULL,                 // parent window handle
            NULL,                 // window menu handle
            hInstance,            // program instance handle
            NULL);                // creation parameters
    
                                  //容错处理
        if (!hWnd)
        {
            MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0);
            return 0;
        }
    
        //显示窗口
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    

    处理按钮按下

    如下代码修改自fishc讲解的示例代码
    代码运行效果如下:
    这里写图片描述
    在菜单中选择后, 点击提交按钮会弹出对话框, 显示选择的内容
    点击[随便]按钮, 会随机选择一个, 并禁止在此选择

    #include <windows.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define WINDOW_WIDTH 800
    #define WINDOW_HEIGHT 600
    
    #define FOOD_NUM 4
    #define GROUP_BUTTON_ID (FOOD_NUM + 1)
    #define PUSH_BUTTON_COMMIT_ID (FOOD_NUM + 2)
    #define PUSH_BUTTON_RANDOM_ID (FOOD_NUM + 3)
    
    
    /* 全局变量定义区 */
    TCHAR* g_szApplicationName = "吃什么???";
    TCHAR* g_szWindowClassName = "windowClassName";
    
    /* 消息回调函数 */
    LRESULT CALLBACK WindowProc(HWND   hwnd,
        UINT   msg,
        WPARAM wParam,
        LPARAM lParam)
    {
        RECT rect;
        TCHAR* foods[FOOD_NUM] = { TEXT("恐龙肉"), TEXT("蓝鲸腿"), TEXT("章鱼舌"), TEXT("凤凰蛋") };
        static HWND hwndButton[FOOD_NUM] = { 0 };
        static int cxChar, cyChar;
        static int choosed = -1;
    
        switch (msg)
        {
        case WM_CREATE:
            cxChar = LOWORD(GetDialogBaseUnits());
            cyChar = HIWORD(GetDialogBaseUnits());
    
            //修改窗口尺寸
            GetWindowRect(hwnd, &rect);
            MoveWindow(hwnd, rect.left, rect.top, 27 * cxChar, 13 * cyChar, TRUE);
            for (int i = 0; i < FOOD_NUM; ++i)
            {
                hwndButton[i] = CreateWindow(TEXT("button"), foods[i], 
                    BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 
                    2 * cxChar, cyChar * (2 + 2 * i), 
                    10 * cxChar, 7 * cyChar / 4,
                    hwnd, (HMENU)i,
                    ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            }
            CreateWindow(TEXT("button"), "菜单:",
                BS_GROUPBOX | WS_CHILD | WS_VISIBLE,
                cxChar, cyChar / 2,
                12 * cxChar, 10 * cyChar,
                hwnd, (HMENU)GROUP_BUTTON_ID,
                ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            CreateWindow(TEXT("button"), "提交",
                BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
                14 * cxChar, cyChar,
                10 * cxChar, 7 * cyChar / 4,
                hwnd, (HMENU)PUSH_BUTTON_COMMIT_ID,
                ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            CreateWindow(TEXT("button"), "随便",
                BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
                14 * cxChar, 3 * cyChar,
                10 * cxChar, 7 * cyChar / 4,
                hwnd, (HMENU)PUSH_BUTTON_RANDOM_ID,
                ((LPCREATESTRUCT)lParam)->hInstance, NULL);
            return 0;
        case WM_COMMAND:
        {
            int id = LOWORD(wParam);
            if (id < FOOD_NUM)
            {
                choosed = id;
            }
            switch (id)
            {
            case PUSH_BUTTON_COMMIT_ID: //选择按钮
                if (choosed == -1)
                {
                    MessageBox(hwnd, TEXT("尚未选择!!!"), TEXT("警告"), MB_OK);
                }
                else
                {
                    MessageBox(hwnd, foods[choosed], TEXT("你选择的食物"), MB_OK);
                }
                break;
            case PUSH_BUTTON_RANDOM_ID: //随便按钮
                if (choosed != -1)
                {   //取消上一个按钮的选中状态(给子窗口发送消息来实现)
                    SendMessage(GetDlgItem(hwnd, choosed), BM_SETCHECK, BST_UNCHECKED, 0);
                }
                srand((unsigned int) time(NULL));
                choosed = rand() % FOOD_NUM;
                //设置单选按钮为选中状态(给子窗口发送消息来实现)
                SendMessage(GetDlgItem(hwnd, choosed), BM_SETCHECK ,BST_CHECKED, 0);
                //之后把食物选项和随便按钮禁用
                for (int i = 0; i < FOOD_NUM; ++i)
                {
                    EnableWindow(GetDlgItem(hwnd, i), FALSE);
                }
                EnableWindow(GetDlgItem(hwnd, PUSH_BUTTON_RANDOM_ID), FALSE);
                break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
            }
        }
            return 0;
    
        case WM_DESTROY:
            //终了程序,发送WM_QUIT消息  
            PostQuitMessage(0);
            break;
        }
    
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance,
        LPSTR     szCmdLine,
        int       iCmdShow)
    {
        HWND hWnd;  //窗口句柄
        WNDCLASSEX winclass;    //窗口类对象
    
                                //窗口类对象的初始化
        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_HREDRAW | CS_VREDRAW;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;
        winclass.cbWndExtra = 0;
        winclass.hInstance = hInstance;
        winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);   //窗口背景颜色设置为按钮的颜色
        winclass.lpszMenuName = NULL;
        winclass.lpszClassName = g_szWindowClassName;
        winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        //注册窗口类
        if (!RegisterClassEx(&winclass))
        {
            MessageBox(NULL, "Registration Failed!", "Error", 0);
            return 0;
        }
    
        //创建窗口  
        hWnd = CreateWindowEx(NULL,                 // extended style
            g_szWindowClassName,  // window class name
            g_szApplicationName,  // window caption
            WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,  // window style
            0,                    // initial x position
            0,                    // initial y position
            WINDOW_WIDTH,         // initial x size
            WINDOW_HEIGHT,        // initial y size
            NULL,                 // parent window handle
            NULL,                 // window menu handle
            hInstance,            // program instance handle
            NULL);                // creation parameters
    
                                  //容错处理
        if (!hWnd)
        {
            MessageBox(NULL, "CreateWindowEx Failed!", "Error!", 0);
            return 0;
        }
    
        //显示窗口
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return msg.wParam;
    }
    
    展开全文
  • 大家好,我正在学习windows8的应用开发,用XAML开发的 我想做个连连看的应用,用户点击一张图片,那么这张图片边框颜色发生改变。 我设定的界面是一个6x6的网格,每个网格一张图片,图片上设置一个透明按钮 点击按钮...
  • WindowsAPI自绘按钮

    千次阅读 2016-05-01 14:50:58
    自绘按钮有多种方法,比如创建一个BS_OWNERDRAW风格的按钮,然后就可以自绘了,但是这个好像不能用SetWindowRgn画各种形状。我介绍的这个方法很简单,但是代码量会稍多一点。就是把一个按钮当做一个单独的窗口来处理...

    自绘按钮有多种方法,比如创建一个BS_OWNERDRAW风格的按钮,然后就可以自绘了,但是这个好像不能用SetWindowRgn画各种形状。我介绍的这个方法很简单,但是代码量会稍多一点。就是把一个按钮当做一个单独的窗口来处理,但是这就必须要每个按钮都要注册一个类,所以代码会多一点。

    总体的思路是:注册主窗口类并创建主窗口–>注册子窗口类并创建子窗口(就是按钮)–>实现各自的窗口过程–>处理对应的消息.

    效果如图:


    代码详解及注释:

    #include <windows.h>
    #include <tchar.h>
    #include "resource.h"
    
    HINSTANCE hInst = NULL; //全局实例句柄
    LRESULT CALLBACK WindProc(HWND, UINT, WPARAM, LPARAM);//主窗口过程
    LRESULT CALLBACK BtnProc1(HWND, UINT, WPARAM, LPARAM); //按钮1窗口过程
    LRESULT CALLBACK BtnProc2(HWND, UINT, WPARAM, LPARAM);//按钮2窗口过程
    int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
    {
        hInst = hInstance;
        TCHAR szAppName[] = TEXT("MyApp");   //主窗口类名
        TCHAR szBtnName1[] = TEXT("MyBtn1"); //按钮窗口类名
        TCHAR szBtnName2[] = TEXT("MyBtn2");
        //主窗口类
        WNDCLASS wndclass = { 0 };
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hbrBackground = (HBRUSH)GetStockObject(COLOR_BACKGROUND);
        wndclass.hCursor = NULL;
        wndclass.hIcon = NULL;
        wndclass.hInstance = hInstance;
        wndclass.lpfnWndProc = WindProc;
        wndclass.lpszClassName = szAppName;
        wndclass.lpszMenuName = NULL;
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        if (!RegisterClass(&wndclass))
        {
            MessageBox(NULL, TEXT("RegisterClass wndclass failed"), TEXT("ERROR"), 0);
            return 0;
        }
        //主窗口
        HWND hwnd = CreateWindow(szAppName, TEXT("MyWindow"), WS_OVERLAPPEDWINDOW,
            100, 100, 300, 200, NULL, NULL, hInstance, 0);
    
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
    
        //自定义按钮类
        WNDCLASS wndbtn1 = { 0 };
        wndbtn1.cbClsExtra = 0;
        wndbtn1.cbWndExtra = 0;  //这个是位图背景,需要自己添加位图资源
        wndbtn1.hbrBackground = CreatePatternBrush(LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP)));
        wndbtn1.hCursor = NULL;
        wndbtn1.hIcon = NULL;
        wndbtn1.hInstance = hInstance;
        wndbtn1.lpfnWndProc = BtnProc1;
        wndbtn1.lpszClassName = szBtnName1;
        wndbtn1.lpszMenuName = NULL;
        wndbtn1.style = CS_HREDRAW | CS_VREDRAW;
    
        if (!RegisterClass(&wndbtn1))
        {
            MessageBox(NULL, TEXT("RegisterClass wndbtn failed"), TEXT("ERROR"), 0);
            return 0;
        }
        //自绘按钮
        HWND hButton1 = CreateWindow(szBtnName1, TEXT("MyButton1"),
            WS_CHILD | WS_VISIBLE,0, 0, 0, 0, hwnd, NULL, hInstance, 0);
    
        ShowWindow(hButton1, SW_SHOW);
        UpdateWindow(hButton1);
    
        WNDCLASS wndbtn2 = { 0 };
        wndbtn2.cbClsExtra = 0;
        wndbtn2.cbWndExtra = 0;
        wndbtn2.hbrBackground = CreateSolidBrush(RGB(255,0,0));
        wndbtn2.hCursor = NULL;
        wndbtn2.hIcon = NULL;
        wndbtn2.hInstance = hInstance;
        wndbtn2.lpfnWndProc = BtnProc2;
        wndbtn2.lpszClassName = szBtnName2;
        wndbtn2.lpszMenuName = NULL;
        wndbtn2.style = CS_HREDRAW | CS_VREDRAW;
    
        if (!RegisterClass(&wndbtn2))
        {
            MessageBox(NULL, TEXT("RegisterClass wndbtn failed"), TEXT("ERROR"), 0);
            return 0;
        }
        HWND hButton2 = CreateWindow(szBtnName2, TEXT("MyButton2"),
            WS_CHILD | WS_VISIBLE,0, 0, 0, 0, hwnd, NULL, hInstance, 0);
        ShowWindow(hButton2, SW_SHOW);
        UpdateWindow(hButton2);
    
        MSG msg = { 0 };
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    
    LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps = { 0 };
        HDC DC = NULL;
        switch (message)
        {
        case WM_CREATE:
    
            break;
        case WM_PAINT:
            DC = BeginPaint(hwnd, &ps);
    
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            break;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    
    }
    
    LRESULT CALLBACK BtnProc1(HWND hBtn1, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps = { 0 };
        HDC DC = NULL;
        switch (message)
        {
        case WM_CREATE:
            {
                MoveWindow(hBtn1, 10, 10, 100, 100, TRUE);
                //画椭圆区域,还可以画圆角,多边形等等,可以自行百度
                HRGN hRgn = CreateEllipticRgn(10, 10, 100, 100);
                SetWindowRgn(hBtn1, hRgn, TRUE);
            }
        break;
        case WM_LBUTTONDOWN:   //测试是否成功响应消息
                MessageBox(NULL, TEXT("MyButton1"), TEXT("提示"), 0);
            break;
        case WM_PAINT:
        {
            DC = BeginPaint(hBtn1, &ps);
            SetBkColor(DC, RGB(0, 255, 255)); //按钮背景颜色
            SetTextColor(DC, RGB(255, 0, 0)); //按钮上的文字颜色
            TextOut(DC, 20, 45, TEXT("MyButton1"), 9);//按钮上的文字
            EndPaint(hBtn1, &ps);
        }
        break;
        default:
            break;
        }
        return DefWindowProc(hBtn1, message, wParam, lParam);
    
    }
    
    LRESULT CALLBACK BtnProc2(HWND hBtn2, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps = { 0 };
        HDC DC = NULL;
        switch (message)
        {
        case WM_CREATE:
        {
            MoveWindow(hBtn2, 160, 10, 250, 100, TRUE);
            //画椭圆区域,还可以画圆角,多边形等等,可以自行百度
            HRGN hRgn = CreateRoundRectRgn(10, 10, 100, 100,20,20);
            SetWindowRgn(hBtn2, hRgn, TRUE);
        }
        break;
        case WM_LBUTTONDOWN:
            MessageBox(NULL, TEXT("MyButton2"), TEXT("提示"), 0);
            break;
        case WM_PAINT:
        {
            DC = BeginPaint(hBtn2, &ps);
            SetBkColor(DC, RGB(0, 255, 255)); //按钮背景颜色
            SetTextColor(DC, RGB(255, 0, 0)); //按钮上的文字颜色
            TextOut(DC, 20, 45, TEXT("MyButton2"), 9); //按钮上的文字
            EndPaint(hBtn2, &ps);
        }
        break;
        default:
            break;
        }
        return DefWindowProc(hBtn2, message, wParam, lParam);
    
    }
    
    展开全文
  • Windows用户体验团队承认他们在 Windows 8 发布后很快就意识到“需要将 Windows 8 中的左下角 Start Tip 改为 Windows Logo”,微软宣布在 Windows 8.1 中默认开启“开始按钮”,并且无法禁用;在 Metro 环境中,...
  • WindowsAPI之按钮

    千次阅读 2011-07-08 19:33:28
    WindowsAPI之按钮n 创建按钮函数:HWND CreateButton( const HWND hParent, //父窗口类 const HINSTANCE hInst,
  • Windows按钮、复选框、分组框控件 单选按钮(Radio Button)和复选框(Check Box)是常见的Windows控件,用于从给出的选项中选择一项或多项,如下图所示: 单选钮与复选框 单选按钮是互斥的,只能选择其中一项;...
  • 怎样实现 单击读取按钮btn 控件dgv 显示 文本文件.csv数据 ![图片说明](https://img-ask.csdn.net/upload/201507/14/1436851604_43163.png);![图片说明]...
  • Windows API怎样获取单选按钮组里的各个单选按钮的值?能否用VS2017可运行的完整代码举例说明?不用MFC。
  • Windows Phone 8 模拟按钮点击

    千次阅读 2013-07-23 14:23:32
    Windows Phone 8中,可以通过代码模拟按钮点击来触发一个按钮。 第一、对Button写个静态扩展类 public static class ButtonBaseExtensions { public static void PerformClick(this System.Windows.Controls...
  • vue中将按钮放在右边Recently Ubuntu joined Mac by moving the Minimize, Maximize, and Close Buttons to the left side of app windows. If this is a feature that you would also like to have in Windows, ...
  • Windows编程4:按钮

    2020-03-20 19:57:18
    控件 控件是什么?... 所以呢,大家可以发现按钮的创建十分简单,但是既然Windows是关于消息的,那么就一定需要按钮的响应消息,这个嘛,也不难,不过大家不要急于求成,我们先把按钮给显示出来吧。
  • 尽管Windows 8系统带来了很多的创新和优化,但是很多用户在体验了各种不习惯和鸡肋的开始屏幕之后都对这款系统给予了负面的评价,其中最为广大用户诟病的就是取消了开 始按钮,因此也诞生了Start 8等著名的第三方...
  • Android实现Windows 8磁贴(Tile)样式按钮

    千次阅读 2015-04-09 11:05:47
    《Android实现Windows 8磁贴(Tile)样式按钮》 效果图如下: 我在网上流传的代码基础上精简、整理出一个简单的类,我暂时把它命名为:Windows8TileImageView,即Windows 8磁贴(Tile)样式按钮Windows8...
  • Windows程序设计-按钮的创建

    千次阅读 2018-12-04 16:15:20
    Windows.h&gt; typedef WNDPROC WMPROC; typedef struct{ UINT msg; WMPROC wmProc; }MsgWithProc; #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define WINDOW_TITLE L"按钮的创建" ...
  • window10快捷ctrl+c和ctrl+v不能使用 重启windows快捷键就好了 ...还有一种情况复制按钮可能会用不了,一般都是向日葵或者第三方远程桌面控制的会占用复制粘贴按钮的端口,关闭第三方远程软件就好了 ...
  • .NET FRAMEWORK 提供了一个派生于Control的类System.Windows.Forms.ButtonBase.它实现了按钮控件的基本功能,可以从这个类中派生,创建定制的按钮控件。 System.Windows.Forms命名空间提供了3个派生于ButtonBase的...
  • 在上一篇文章中,我介绍了windows定义的四种按钮,但是这四种按钮都不能实现对按钮外观的完全控制,所以我下来要介绍一种可以对按钮外观进行完全控制的第五种按钮。 这种按钮在建立时需要包含BS_OWNERDRAW风格,...
  • 大体上看,Windows定义了4种类型的按钮,分别是Check Boxes,Group Boxes,Push Buttons,Radio Buttons。 每一个类型的按钮又可以细分为几种按钮。在此我来一一介绍一下。 因为是按钮入门的原因,所以我在这里讲...
  • 利用C#实现类似windows的开始按钮,即按一下弹出,在按一下收回,按空白区域弹出的界面也可收回。最好利用WPF的popup实现。我目前无法实现当界面已弹出后,再按一下让界面收回。
  • 在实际开发中,可能会碰到这样一个场景:使用Windows系统下的MessageBox给出提示信息时,按钮的显示内容是Windows已经设定好的,无法更改。比如,在中文系统下,IDOK对应的按钮内容为“确定”,IDCANCLE对应的按钮...
  • 单选按钮(Radio Button)和复选框(Check Box)是常见的Windows控件,用于从给出的选项中选择一项或多项,如下图所示: 单选按钮是互斥的,只能选择其中一项;而复选框没有限制,可以选择一项或多项。 单选...
  • Windows button控件(按钮控件)

    千次阅读 2016-06-20 06:38:59
    按钮既是标准控件,也是子窗口,窗口类名是button。 Button 控件样式以BS开头,常用的有: 样式 说明 BS_LEFT 文本居左。 BS_RIGHT 文本居右。 BS_CENTER 文本水平居中(默认为 BS...
  • QT去掉最大最小关闭按钮,保留标题栏,且保留Windows窗体移动到屏幕边缘自动排列功能,要怎么设置? 我的做法是this->setWindowFlags(Qt::Window | Qt::WindowTitleHint);这样去掉了最小最大关闭三个按钮,但是同时...
  • 按钮既是标准控件,也是子窗口,窗口类名是button。 Button 控件样式以BS...Windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE hInst; int WINAPI WinMain( HINSTANCE hInstanc...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 21,432
精华内容 8,572
关键字:

windows按钮