您的位置:首页 > 其它

简单win32程序创建控件

2011-10-13 18:23 537 查看
Win32 SDK利用ListView控件。

为了使用ListView控件,我们需要初始化公共控件库,我们需要在程序刚刚启动时调用

InitCommonControls() 函数,如果发生链接错误,说明我们没有链接拥有该函数的库文件。它们对应的

头和库 DLL分别为 #include <commctrl.h> comctl32.lib comctl32.dll

为了使用这个控件 我们就需要知道它的窗口类,利用Spy++等文件可以找到指定进程窗口的窗口类,

而一个ListView控件也是一个子窗口,所以我们可以得到它的类名为syslistview32,其他的控件,

我们只需要按照同样的道理来得到类名即可。

有了类名还不够,我们还需要知道每种控件的风格,比如listView控件有以下的风格LVS_REPORT | LVS_SHOWSELALWAYS, 它表示要产生报表和总是显示。为了得到控件的风格,我们可以通过MSDN中MFC中的ListView风格来作参考。有了窗口类和风格,我们利用CreateWindow就可以创建并得到

这个控件的句柄了。有了句柄,我们就可以随便控制了,具体要怎么看你自己的了。

此外为了向ListView内插入项和列,我们需要两个结构体。

LVITEM和LVCOLUMN

它们的定义分别为

typedef struct _LVITEM { 
    UINT mask; 
    int iItem; 
    int iSubItem; 
    UINT state; 
    UINT stateMask; 
    LPTSTR pszText; 
    int cchTextMax; 
    int iImage; 
    LPARAM lParam;
#if (_WIN32_IE >= 0x0300)
    int iIndent;
#endif
#if (_WIN32_IE >= 0x560)
    int iGroupId;
    UINT cColumns; // tile view columns
    PUINT puColumns;
#endif

typedef struct _LVCOLUMN { 
    UINT mask; 
    int fmt; 
    int cx; 
    LPTSTR pszText; 
    int cchTextMax; 
    int iSubItem; 
#if (_WIN32_IE >= 0x0300)
    int iImage;
    int iOrder;
#endif
} LVCOLUMN, *LPLVCOLUMN; 

} LVITEM, *LPLVITEM;

有了这两个结构体,我们就可以利用SendMessage来给ListView控件发送消息来为它添加项和列。

我们分别通过下面两个消息来添加项和列。

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

LVM_INSERTITEM表示添加项

LVM_INSERTCOLUMN表示添加列。

为了更好的查找关于ListView的消息,我们只需要在网上或MSDN 里查找 LVM_XXXXXX 就可以找到

相关的消息了。最好自己整理出一份关于ListView的全部消息。

WindowFrame &wndFrame = WindowFrame::Instance();



GetWindowRect(hWnd, &rect);

cx = rect.right-rect.left;

cy = rect.bottom-rect.top;

switch(msg)

{

case WM_CREATE:

{

rowIndex = 7;

hButton = CreateWindow("syslistview32", "",

WS_VISIBLE|WS_CHILD|WS_BORDER|

LVS_REPORT | LVS_SHOWSELALWAYS,

10, 20,

cx-30,

cy-100,

hWnd, NULL, wndFrame.getInstance(), NULL);

// 添加数据

LV_ITEM item; // 项

LV_COLUMN colmn; // 列

ZeroMemory(&item, sizeof(LV_ITEM));

ZeroMemory(&colmn, sizeof(LV_COLUMN));



colmn.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // 风格

colmn.cx = 0x28;

colmn.pszText = "进程名"; // 文字

colmn.cx = 0x42; // 后面列

SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

colmn.pszText = "内存使用";

SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

colmn.pszText = "ID";

SendMessage(hButton, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

// 添加一些行项

item.mask = LVIF_TEXT; // 文字

item.cchTextMax = MAX_PATH; // 文字长度

item.iItem = 0;

item.iSubItem = 0;

item.pszText = "中国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "日本";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "德国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "俄国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "美国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "英国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "法国";

SendMessage(hButton, LVM_INSERTITEM, 0, (LPARAM)&item);

}

break;

























以下是源代码



#include <windows.h>

#include <stdio.h>

#include "stdlib.h"

#include "brd_Ihead.h"

#include <commctrl.h>

typedef struct _LVITEM

{

UINT mask;

int iItem;

int iSubItem;

UINT state;

UINT stateMask;

LPTSTR pszText;

int cchTextMax;

int iImage;

LPARAM lParam;

#if (_WIN32_IE >= 0x0300)

int iIndent;

#endif

#if (_WIN32_IE >= 0x560)

int iGroupId;

UINT cColumns; // tile view columns

PUINT puColumns;

#endif

typedef struct _LVCOLUMN

{

UINT mask;

int fmt;

int cx;

LPTSTR pszText;

int cchTextMax;

int iSubItem;

#if (_WIN32_IE >= 0x0300)

int iImage;

int iOrder;

#endif

} LVCOLUMN, *LPLVCOLUMN;

} LVITEM, *LPLVITEM;



// 全局主对象

CMainObj g_MainObj;

// Win32消息入口

LRESULT CALLBACK WinSunProc(HWND hwnd/*窗口句柄*/,UINT uMsg/*消息id*/,WPARAM wParam/*附加信息*/, LPARAM lParam/*附加信息*/);

// Win32程序入口

int WINAPI WinMain(HINSTANCE hInstance/*应用程序实例*/,HINSTANCE hPrevInstance,LPSTR lpCmdLine/*命令行参数*/,int nCmdShow/*显示模式*/)

{

// 初始化全局窗口数据

g_MainObj.Init();

//定义窗口类

WNDCLASS mainWint;

mainWint.style = g_MainObj.m_szWNDCLASS.style;

mainWint.lpfnWndProc = WinSunProc;

mainWint.cbClsExtra = g_MainObj.m_szWNDCLASS.cbClsExtra;

mainWint.cbWndExtra = g_MainObj.m_szWNDCLASS.cbWndExtra;

mainWint.hbrBackground = g_MainObj.m_szWNDCLASS.hbrBackground;

mainWint.hCursor = g_MainObj.m_szWNDCLASS.hCursor;

mainWint.hIcon = g_MainObj.m_szWNDCLASS.hIcon;

mainWint.hInstance = hInstance;

mainWint.lpszClassName = g_MainObj.m_szWNDCLASS.lpszClassName;

mainWint.lpszMenuName = g_MainObj.m_szWNDCLASS.lpszMenuName;

//注册窗口类

if (!RegisterClass(&mainWint))

{

MessageBox (NULL, TEXT ("This program requires Windows NT!"),MAIN_WIND_CLASSNAME, MB_ICONERROR) ;

return PROC_SUCCESS;

}

// 创建主窗口

HWND hWnd_t;

hWnd_t = CreateWindow(MAIN_WIND_CLASSNAME,MAIN_WIND_NAME,MAIN_WIND_STYLE,\

MAIN_WIND_X,MAIN_WIND_Y,MAIN_WIND_WIDTH,MAIN_WIND_HIGHT,NULL,NULL,hInstance,NULL);

// 显示更新主窗口

ShowWindow(hWnd_t,SW_SHOWNORMAL);

UpdateWindow(hWnd_t);

g_MainObj.m_hMainWnd = &hWnd_t;

/*

BOOL GetMessage(

LPMSG lpMsg, //消息结构指针

HWND hWnd, //从哪个窗口获得消息 NULL接受任何窗口的消息

UINT wMsgFilterMin, //消息最小值 通常为0

UINT wMsgFilterMax //消息最大值 2个都是0 表示接受所有消息

);

*/

MSG msg;

//消息获取 循环函数

while(GetMessage(&msg,NULL,0,0))

{

//解析消息

TranslateMessage(&msg);

//派发消息

DispatchMessage(&msg);

}

return PROC_SUCCESS;

}

// 消息分派

LRESULT CALLBACK WinSunProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

if(g_MainObj.MainFunctionInterface(hwnd,uMsg,wParam,lParam))

{

return DefWindowProc(hwnd,uMsg,wParam,lParam);

}

return PROC_SUCCESS;

}

CIBase::CIBase(void) : m_hMainWnd(NULL),m_pMsgEx(new CMsgEx)

{

}

CIBase::~CIBase(void)

{

delete m_pMsgEx;

}



CMainObj::CMainObj(void)

{

}

CMainObj::~CMainObj(void)

{

}

int CMainObj::Init(void)

{

/*

* 窗口样式初始化

*

*/



//类型窗口的样式 风格 水平重画和垂直重画

m_szWNDCLASS.style = MAIN_CLASS_STYLE ;

//窗口过程函数(回调函数)函数指针

//m_szWNDCLASS.lpfnWndProc = WinSunProc;

// 一般为0 附加内存空间

m_szWNDCLASS.cbClsExtra = MAIN_CLASS_CBCLSEXTRA;

// 一般为0 窗口附加内存

m_szWNDCLASS.cbWndExtra = MAIN_CLASS_CBWNDEXTRA;

//背景画刷句柄

m_szWNDCLASS.hbrBackground = MAIN_CLASS_BACKGROUND;

//光标句柄

m_szWNDCLASS.hCursor = MAIN_CLASS_CURSOR;

//窗口类的图标句柄

m_szWNDCLASS.hIcon = MAIN_CLASS_ICON;

//包含窗口过程的实例句柄

//m_szWNDCLASS.hInstance = hInstance;

//窗口类名

m_szWNDCLASS.lpszClassName = MAIN_CLASS_NAME;

//菜单资源名

m_szWNDCLASS.lpszMenuName = MAIN_CLASS_MENU ;

return PROC_SUCCESS;

}

int CMainObj::MainFunctionInterface(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

m_pMsgEx->hwnd = hwnd;

m_pMsgEx->uMsg = uMsg;

m_pMsgEx->lParam = lParam;

m_pMsgEx->wParam = wParam;

if(OnMsg(m_pMsgEx))

{

return PROC_FAILED;

}

return PROC_SUCCESS;

}

int CMainObj::OnMsg(CMsgEx *pMsgEx)

{

static HWND hwndList, hwndText, hwndList_s;

int iIndex, iLength, cxChar, cyChar ;

switch(pMsgEx->uMsg)

{

//case WM_COMMAND:

// switch(wParam) {

// case :

// ;

// break;

//

// }

case WM_KEYDOWN:

switch(pMsgEx->wParam)

{

case VK_F1:

cxChar = LOWORD (GetDialogBaseUnits ()) ;

cyChar = HIWORD (GetDialogBaseUnits ()) ;

hwndList = CreateWindow ("listbox", NULL,

WS_CHILD | WS_VISIBLE | LBS_STANDARD,

cxChar, cyChar * 9,

cxChar * 16 + GetSystemMetrics (SM_CXVSCROLL),

cyChar * 5,

pMsgEx->hwnd, (HMENU) ID_LIST,

(HINSTANCE) GetWindowLong (pMsgEx->hwnd, GWL_HINSTANCE),

NULL);

hwndText = CreateWindow ("edit", "goods!",

WS_CHILD | WS_VISIBLE | SS_LEFT,

0, cyChar,

500, cyChar*8,

pMsgEx->hwnd, (HMENU) ID_TEXT,

(HINSTANCE) GetWindowLong (pMsgEx->hwnd, GWL_HINSTANCE),

NULL) ;



ShowWindow(hwndList,SW_SHOWNORMAL);

ShowWindow(hwndText,SW_SHOWNORMAL);

MessageBox(pMsgEx->hwnd,"F1 pressed","F1 pressed",MB_OK);

break;

case VK_F2:

hwndList_s = CreateWindow("syslistview32", "",

WS_VISIBLE|WS_CHILD|WS_BORDER|

LVS_REPORT | LVS_SHOWSELALWAYS,

10, 20,

300,

300,

pMsgEx->hwnd, NULL, (HINSTANCE) GetWindowLong (pMsgEx->hwnd, GWL_HINSTANCE), NULL);

// 添加数据

LV_ITEM item; // 项

LV_COLUMN colmn; // 列

ZeroMemory(&item, sizeof(LV_ITEM));

ZeroMemory(&colmn, sizeof(LV_COLUMN));

colmn.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // 风格

colmn.cx = 0x28;

colmn.pszText = "进程名"; // 文字

colmn.cx = 0x42; // 后面列

SendMessage(hwndList_s, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

colmn.pszText = "内存使用";

SendMessage(hwndList_s, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

colmn.pszText = "ID";

SendMessage(hwndList_s, LVM_INSERTCOLUMN, 0, (LPARAM)&colmn);

// 添加一些行项

item.mask = LVIF_TEXT; // 文字

item.cchTextMax = MAX_PATH; // 文字长度

item.iItem = 0;

item.iSubItem = 0;

item.pszText = "中国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "日本";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "德国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "俄国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "美国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "英国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

item.pszText = "法国";

SendMessage(hwndList_s, LVM_INSERTITEM, 0, (LPARAM)&item);

MessageBox(pMsgEx->hwnd,"F2 pressed","F2 pressed",MB_OK);

break;

//在这里可以添加更多按键的处理过程

case VK_ADD:

MessageBox(pMsgEx->hwnd,"F2 pressed","F2 pressed",MB_OK);

break;

}

break;

case WM_CHAR: //接受按键消息

SetTimer(pMsgEx->hwnd,1,1000,0);

char szChar[20];

sprintf(szChar,"char is %c",pMsgEx->wParam);

MessageBox(pMsgEx->hwnd,szChar,"窗口标题",MB_YESNO);

break;

case WM_TIMER:

//pp= (long*) malloc( sizeof(100));

// MessageBox(hwnd,"鼠标右键","鼠标右键",MB_OK);

//pp=fopen("C:/Documents and Settings/Administrator/桌面/zhy.txt","at+");

case WM_RBUTTONDOWN://接受鼠标右健消息

//MessageBox(hwnd,"鼠标右键","鼠标右键",MB_OK);

//break;

case WM_LBUTTONDOWN: //得到当鼠标左键按下时的鼠标位置

/*x = LOWORD(lParam);

y = HIWORD(lParam);

char info[20];

sprintf(info,"%d,%d",x,y);

MessageBox(hwnd,info,"鼠标左键",MB_OK);*/

case WM_PAINT://接受绘制消息

HDC hDC;

PAINTSTRUCT ps;

hDC=BeginPaint(pMsgEx->hwnd,&ps);

TextOut(hDC,100,100,"手好痛阿。。 !",strlen("手好通阿。。 !"));

//LineTo(hDC,100,100);

EndPaint(pMsgEx->hwnd,&ps);

break;

case WM_CLOSE://接受关闭消息

if(IDYES==MessageBox(pMsgEx->hwnd,"是否真的结束?","窗口标题",MB_YESNO))

{

DestroyWindow(pMsgEx->hwnd);

}

break;

case WM_DESTROY://接受销毁窗口消息

PostQuitMessage(0);

break;

default:

return PROC_FAILED;

}

return PROC_SUCCESS;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: