win32 hello

#include <windows.h>
#include <WinUser.h>
#include <CommCtrl.h>
#include "controlid.h"
#include "globallists.h"
#include <cwchar>
#include <vector>

//----------------------------------------------------
//BS_PUSHBUTTON
//----------------------------------------------------
void CreatePushButton(const HWND &ParentHwnd)
{
    CreateWindow(
        L"Button",
        L"文本",
        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
        10,10,90,30,
        ParentHwnd,
        (HMENU)PUSHBUTTONID,
        (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
        NULL);
}

void HandlePushButtonMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
{
    switch(Message)
    {
    case WM_COMMAND:
        switch(LOWORD(WParam))
        {
            case PUSHBUTTONID:
                switch(HIWORD(WParam))
                {
                    case BN_CLICKED:
                        HRESULT messageboxResult=MessageBox(ParentHwnd,L"消息",L"标题",MB_ICONEXCLAMATION|MB_YESNO);
                        switch(messageboxResult)
                        {
                        case IDYES:
                            MessageBox(ParentHwnd,L"点击了\"是\"",L"标题",NULL);
                            break;
                        case IDNO:
                            MessageBox(ParentHwnd,L"点击了\"否\"",L"标题",NULL);
                            break;
                        }
                    break;
                }
        }
        break;
    }

}

 //----------------------------------------------------
 //BS_AUTO3STATE
 //----------------------------------------------------
 void CreateAuto3StateButton(const HWND &ParentHwnd)
 {
     CreateWindow(
         L"Button",
         L"文本",
         WS_VISIBLE | WS_CHILD | BS_AUTO3STATE,
         10,50,90,30,
         ParentHwnd,
         (HMENU)AUTO3STATEBUTTONID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );
     //默认设置为未选定
     SendMessage(GetDlgItem(ParentHwnd,AUTO3STATEBUTTONID),BM_SETCHECK,BST_INDETERMINATE,0);
 }

 void HandleAuto3StateButtonMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch (Message)
     {
     case WM_COMMAND:
         switch(LOWORD(WParam))
         {
         case AUTO3STATEBUTTONID:
             switch(HIWORD(WParam))
             {
             case BN_CLICKED:
                 UINT state=SendMessage(GetDlgItem(ParentHwnd,AUTO3STATEBUTTONID), BM_GETCHECK ,0,0);
                 switch(state)
                 {
                 case BST_CHECKED:
                     MessageBox(ParentHwnd,L"选择状态",L"标题",NULL);
                     break;
                 case BST_INDETERMINATE:
                     MessageBox(ParentHwnd,L"未定状态",L"标题",NULL);
                     break;
                 case BST_UNCHECKED:
                     MessageBox(ParentHwnd,L"未选择状态",L"标题",NULL);
                     break;
                 }
                 break;
             }
         }
         break;
     }
 }

 //----------------------------------------------------
 //BS_AUTOCHECKBOX
 //----------------------------------------------------
 void CreateAutoCheckBox(const HWND &ParentHwnd)
 {
     CreateWindow(
         L"Button",
         L"文本",
         WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
         10,90,90,30,
         ParentHwnd,
         (HMENU)AUTOCHECKBUTTONID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );
 }

 void HandleAutoCheckBoxMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch(Message)
     {
     case WM_COMMAND:
         switch(LOWORD(WParam))
         {
         case AUTOCHECKBUTTONID:
             switch(HIWORD(WParam))
             {
             case BN_CLICKED:
                 UINT state=SendMessage(GetDlgItem(ParentHwnd,AUTOCHECKBUTTONID), BM_GETCHECK ,0,0);
                 switch(state)
                 {
                 case BST_CHECKED:
                     MessageBox(ParentHwnd,L"选择状态",L"标题",NULL);
                     break;
                 case BST_UNCHECKED:
                     MessageBox(ParentHwnd,L"未选择状态",L"标题",NULL);
                     break;
                 }
                 break;
             }
         }
         break;
     }
 }
 //----------------------------------------------------
 //BS_GROUPBOX BS_AUTORADIOBUTTON
 //----------------------------------------------------

 void CreateRadioButtonGroup(const HWND &ParentHwnd)
 {
     CreateWindow(
         L"Button",
         L"文本",
         WS_VISIBLE | WS_CHILD | BS_GROUPBOX,
         10,130,80,90,
         ParentHwnd,
         (HMENU)GROUPBOXID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );

     CreateWindow(
         L"Button",
         L"文本1",
         WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON|WS_GROUP,
         15,150,60,20,
         ParentHwnd,
         (HMENU)AUTORADIOBUTTON1ID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );
     CreateWindow(
         L"Button",
         L"文本2",
         WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
         15,170,60,20,
         ParentHwnd,
         (HMENU)AUTORADIOBUTTON2ID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );
     CreateWindow(
         L"Button",
         L"文本3",
         WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
         15,190,60,20,
         ParentHwnd,
         (HMENU)AUTORADIOBUTTON3ID,
         (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
         NULL
         );

     //设置第二个为默认选中
     SendMessage(GetDlgItem(ParentHwnd,AUTORADIOBUTTON2ID),BM_SETCHECK,BST_CHECKED,0);
 }

 void HandleRadioButtonGroupMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     UINT buttons[3]={AUTORADIOBUTTON1ID,AUTORADIOBUTTON2ID,AUTORADIOBUTTON3ID};
     switch(Message)
     {
     case WM_COMMAND:
         switch(LOWORD(WParam))
         {
            case AUTORADIOBUTTON1ID:
            case AUTORADIOBUTTON2ID:
            case AUTORADIOBUTTON3ID:
                for (INT i=0;i<3;i++)
                {
                    if (BST_CHECKED==SendMessage(GetDlgItem(ParentHwnd,buttons[i]), BM_GETCHECK ,0,0))
                    {
                        wchar_t buf[30];
                        std::swprintf(buf,29,L"单选按钮%d",i+1);
                        MessageBox(ParentHwnd,buf,L"标题",NULL);
                    }
                }
                break;
         }
         break;
     }
 }

 //----------------------------------------------------
 //TRACKBAR_CLASS  STATIC
 //----------------------------------------------------
 void CreateTrackBar(const HWND &ParentHwnd)
 {
     //范围过大的时候可能出现问题
        HWND hwndDlg=ParentHwnd;    // handle of dialog box (parent window)
        UINT iMin=-100;                // minimum value in trackbar range
        UINT iMax=20000;            // maximum value in trackbar range 

        HWND hwndTrack = CreateWindow(
             TRACKBAR_CLASS,                    // class name
             L"Trackbar Control",                // title (caption)
             WS_CHILD |
             WS_VISIBLE | TBS_TOOLTIPS|
             TBS_AUTOTICKS,                    // style
             30, 230,                        // position
             200, 40,                       // size
             hwndDlg,                       // parent window
             (HMENU)TRACKBARID,             // control identifier
             (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),     // instance
             NULL                             // no WM_CREATE parameter
             ); 

         SendMessage(hwndTrack, TBM_SETRANGE,
             (WPARAM) TRUE,                   // redraw flag
             (LPARAM) MAKELONG(iMin, iMax));  // min. & max. positions

         SendMessage(hwndTrack, TBM_SETPAGESIZE,
             0, (LPARAM) 4);                  // new page size 

         SendMessage(hwndTrack, TBM_SETSEL,
             (WPARAM) FALSE,                  // redraw flag
             (LPARAM) MAKELONG(iMin, iMax)); 

         SendMessage(hwndTrack, TBM_SETPOS,
             (WPARAM) TRUE,                   // redraw flag
             (LPARAM) (long)((iMax-iMin)/2)); 

        //创建一个静态文本,显示当前值
         CreateWindow(
             L"STATIC",
             L"文本",
             WS_VISIBLE | WS_CHILD | SS_LEFT,
             30,280,150,20,
             ParentHwnd,
             (HMENU)TRACKBARSTATICID,
             (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE),
             NULL
             );

         int pos=SendMessage(hwndTrack, TBM_GETPOS,0,0);
         wchar_t buf[30];
         std::swprintf(buf,29,L"当前值为:%d",pos);
         SetWindowText(GetDlgItem(ParentHwnd,TRACKBARSTATICID),buf);

         SetFocus(hwndTrack);
 }

 void HandleTrackBarMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch(Message)
     {
        //本滑动条是横向的
     case WM_HSCROLL:
         //拖拽的同时,同步更新变量,更新频率会很高
        if ((HWND)LParam==GetDlgItem(ParentHwnd,TRACKBARID))
        {
            int pos=SendMessage((HWND)LParam, TBM_GETPOS,0,0);
            wchar_t buf[30];
            std::swprintf(buf,29,L"当前值为:%d",pos);
            SetWindowText(GetDlgItem(ParentHwnd,TRACKBARSTATICID),buf);
            SetFocus((HWND)LParam);
        }
     }
 }

 //----------------------------------------------------
 //WC_COMBOBOXEX
 //----------------------------------------------------
 void CreateComboBox(const HWND &ParentHwnd)
 {
     // Create the Combobox
     //
     // Uses the CreateWindow function to create a child window of
     // the application window. The WC_COMBOBOX window style specifies
     // that it is a combobox.

     int xpos = 10;            // Horizontal position of the window.
     int ypos = 320;            // Vertical position of the window.
     int nwidth = 200;          // Width of the window
     int nheight = 20;         // Height of the window
     HWND hwndParent = ParentHwnd; // Handle to the parent window

     HWND hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
         CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
         xpos, ypos,
         nwidth, nheight,
         hwndParent,
         (HMENU)COMBOBOXID,
         (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE),
         NULL);

     // load the combobox with item list.
     // Send a CB_ADDSTRING message to load each item

     TCHAR Planets[9][10] =
     {
         TEXT("Mercury"), TEXT("Venus"), TEXT("Terra"), TEXT("Mars"),
         TEXT("Jupiter"), TEXT("Saturn"), TEXT("Uranus"), TEXT("Neptune"),
         TEXT("Pluto??")
     };

     TCHAR A[16];
     int  k = 0;

     memset(&A, 0, sizeof(A));
     for (k = 0; k <= 8; k += 1)
     {
         wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]);

         // Add string to combobox.
         SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
     }

     // Send the CB_SETCURSEL message to display an initial item
     //  in the selection field
     SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0);

 }

 void HandleComboBoxMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch (Message)
     {
     case WM_COMMAND:
         //注意!当使用 SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0);
         //改变当前选定值时,不会产生CBN_SELCHANGE消息。
         if (HIWORD(WParam)== CBN_SELCHANGE && LOWORD(WParam)== COMBOBOXID)
         {
             // If the user makes a selection from the list:
             //   Send CB_GETCURSEL message to get the index of the selected list item.
             //   Send CB_GETLBTEXT message to get the item.
             //   Display the item in a messagebox.

             int ItemIndex = SendMessage((HWND)LParam, (UINT)CB_GETCURSEL,(WPARAM)0, (LPARAM)0);
             TCHAR  ListItem[256];
             (TCHAR)SendMessage((HWND)LParam, (UINT)CB_GETLBTEXT,(WPARAM)ItemIndex, (LPARAM)ListItem);
             MessageBox(ParentHwnd, (LPCWSTR)ListItem, TEXT("Item Selected"), MB_OK);
         }
     default:
         break;
     }
 }

 //----------------------------------------------------
 //WC_LISTBOX
 //----------------------------------------------------
 void CreateListBoxSingle(const HWND &ParentHwnd)
 {
     //创建
     HWND hwndTrack = CreateWindow(
         WC_LISTBOX,                    // class name
         L"List Box",                // title (caption)
         WS_CHILD |
         WS_VISIBLE | LBS_STANDARD | LBS_NOTIFY,    // style
         10, 350,                        // position
         200, 100,                       // size
         ParentHwnd,                       // parent window
         (HMENU)LISTBOXID,             // control identifier
         (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE),     // instance
         NULL                             // no WM_CREATE parameter
         );

     //初始化信息
     typedef struct
     {
         TCHAR achName[MAX_PATH];
         TCHAR achPosition[12];
         int nGamesPlayed;
         int nGoalsScored;
     } Player;

     Player Roster[] =
     {
         { TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
         { TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
         { TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
         { TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
         { TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
         { TEXT("Raposo, Rui"), TEXT("Back"), 24, 3 },
         { TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
         { TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
         { TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
         { TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
         { TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
     };

     // Add items to list.
     HWND hwndList = GetDlgItem(ParentHwnd, LISTBOXID);
     for (int i = 0; i < ARRAYSIZE(Roster); i++)
     {
         int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
             (LPARAM)Roster[i].achName);
         // Set the array index of the player as item data.
         // This enables us to retrieve the item from the array
         // even after the items are sorted by the list box.
         SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM)i);
     }
     SendMessage(hwndList, LB_SETCURSEL, 2, 0);

 }

 void HandleListBoxSingleMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch (Message)
     {
     case WM_COMMAND:

         if (HIWORD(WParam) == LBN_SELCHANGE && LOWORD(WParam) == LISTBOXID)
         {
             int ItemIndex = SendMessage((HWND)LParam, (UINT)LB_GETSEL, (WPARAM)0, (LPARAM)0);
             TCHAR  ListItem[256];
             (TCHAR)SendMessage((HWND)LParam, (UINT)LB_GETTEXT, (WPARAM)ItemIndex, (LPARAM)ListItem);
             MessageBox(ParentHwnd, (LPCWSTR)ListItem, TEXT("Item Selected"), MB_OK);
         }
     default:
         break;
     }
 }

 //----------------------------------------------------
 //Edit
 //----------------------------------------------------
 void CreateEdit(const HWND &ParentHwnd)
 {
     //创建
     HWND hwndedit = CreateWindow(
         L"EDIT",                    // class name
         L"edit",                // title (caption)
         WS_CHILD | WS_VISIBLE | WS_VSCROLL |
         ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,    // style
         10, 460,                        // position
         200,150,                       // size
         ParentHwnd,                       // parent window
         (HMENU)EDITID,             // control identifier
         (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE),     // instance
         NULL                             // no WM_CREATE parameter
         );

 }

 void HandleEditMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam)
 {
     switch (Message)
     {
     case WM_COMMAND:

         if (HIWORD(WParam) == EN_CHANGE && LOWORD(WParam) == EDITID)
         {
             int length =GetWindowTextLength((HWND)LParam);
             std::wstring buf;
             buf.resize(length);
             GetWindowText((HWND)LParam,(LPWSTR)buf.c_str(),length);
             MessageBox(ParentHwnd, buf.c_str(), TEXT("Item Selected"), MB_OK);
         }
     default:
         break;
     }
 }
时间: 2024-11-08 05:02:01

win32 hello的相关文章

带dos调试窗口的win32程序

#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"") 方法只要在cpp文件头部加入一行: #pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")//以console程序样式,并且制

win32 api

Microsoft在StrSafe.h定义了新的安全字符串函数 为了防止缓冲区溢出,在将一个可写缓冲区作为参数传递时,必须传递它的大小,大小可以有_countof获得, _countof获取字符数, sizeof获取字节数 for example: int a[10]; // _countof(a) == 10 _CrtSetReportMode(_CRT_ASSERT, 0); // in head of program 可以禁止C run time可能触发的debug assertion f

%1不是有效的 win32应用程序(64位转换32位)

phpmyadmin打开时候显示 HTTP500内部服务器错误,用火狐浏览器显示的是%1不是有效的 win32应用程序.谷歌一下,终于找出问题,原来是windows2003 64位的操作系统和32位IIS的问题..最终找到解决方法如下: 要运行 32 位版本的 ASP.NET 2.0,请按照以下步骤操作:1. 单击"开始",单击"运行",键入 cmd,然后单击"确定". 2. 键入以下命令启用 32 位模式: cscript %SYSTEMDRI

最近碰到了一个病毒木马:virus.win32.ramnit.B

由于 使用了 简单游 平台上的挂机工具: 番茄-自动人机对战免费版1217  ,使用了很久,头段时间家里电脑 360提示有病毒,本来我一直忽略的,但 我扫描了一下,大量的这个木马,于是 吧 简单游卸载了,全盘扫描,杀毒,修复了许多文件.许多文件被感染,文件大小都变了,还好 360 可以修复感染文件. 这一次,我是用公司电脑,再次 尝试使用 简单游 的挂机工具,没想到 依旧 是这个病毒 virus.win32.ramnit.B ,又是感染了大量的文件.360 居然没有提醒,还好 我清理电脑垃圾 顺

DS.DELMIA.VMAP.V5-6R2017.SP2.Win32

Tracrite.Software.Optimum.Control.Pro.v4.00.08.0102 Agisoft.PhotoScan.Professional.v1.3.2.4164 x64 Altair.HyperWorks.2017.1.Suite.Win64 DS.DELMIA.VMAP.V5-6R2017.SP2.Win32 exida.exSILentia.2014.v2.4.0.25 IMOLD.V13.SP4.2.for.SolidWorks2011-2017 maxmess

PowerShape 2011 R3 SP1 Update Only Win32 1CD

TraceParts v2.3-ISO 1CD VisCAM.RP.v5.2.8600 1CD  Beta-CAE ANSA.v13.1.2.Win64 1CD CEI.Ensight.Gold.v9.2.2b.Linux64.Debian 1CD CEI.Ensight.Gold.v9.2.2b.MacOSX 1CD InfinySlice.v1.0.8581 1CD Leica.LISCAD.v9.0.3 1CD  AFT Impulse v4.0 2011.04.21 1CD AgroKa

CYPE.V2015综合建筑设计和分析软件+CD-ADAPCO.SPEED.10.04.011.WIN32

CD-ADAPCO.SPEED.10.04.011.WIN32完整的电机解决方案   CD-adapco 是专注于计算流体动力学的全球最大非上市计算机辅助工程提供商.技术领先的模拟套装STAR-CCM+® 和 STAR-CD® 是该公司的主打产品.CD-adapco 的业务范围突破了计算流体动力学软件开发的范围,囊括了流体动力学.热传递和结构工程方面的各种计算机辅助工程设计服务.该公司一直秉承着"通过工程模拟软件和服务激发创新和降低成本"的使命. CD-ADAPCO.SPEED-为电机

win32用GDI+加载png图片作为背景图

#include <windows.h> #include <gdiplus.h> /* GDI+ startup token */ ULONG_PTR gdiplusStartupToken; /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); // UpdateLayeredWindow Defination typedef BOOL(*UP

纯win32实现PNG图片透明窗体

#include <windows.h> #include <gdiplus.h> /*  GDI+ startup token */ ULONG_PTR gdiplusStartupToken; /*  Declare Windows procedure  */ LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); // UpdateLayeredWindow Defination typedef BOOL(

Win32编程中如何处理控制台消息

这篇文章讨论如何处理所有的控制台消息. 第一步,首先要安装一个事件钩子,也就是说要建立一个回调函数.调用Win32 API,原型如下: BOOL SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, // 回调函数BOOL Add // 表示添加还是删除): 参数HandlerRoutine是一个指向函数的指针,原型如下: BOOL WINAPI HandlerRoutine(DWORD dwCtrlType // 控制事件类型): 所有的