// Win32Test.cpp : 定义控制台应用程序的入口点。
//
#include <Windows.h>
#include "resource1.h"
#include <commctrl.h>
#include <stdio.h>
#pragma comment(lib,"comctl32.lib")
LRESULT WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DlgProcModel (HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgProcModeless (HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam);
HWND CreateListView(HINSTANCE hInstance, HWND hwndParent);
WNDCLASS wndclass;
HINSTANCE g_hInstance = NULL;
HWND hDlgModeless = NULL;
DWORD CALLBACK pfnFunThread(LPVOID)
{
//线程中窗口空间,回调函数在此线程中调用
HWND hWindow = CreateWindow("MyWindow","WindowName",WS_OVERLAPPEDWINDOW,700,500,300,200,NULL,NULL,g_hInstance,NULL);
ShowWindow(hWindow,SHOW_OPENWINDOW);
UpdateWindow(hWindow);
HWND hWindow2 = CreateWindow("button","线程按钮3333",WS_CHILD|WS_VISIBLE,
0,0,100,40,hWindow,HMENU(3333),g_hInstance,NULL);
MSG msg={0};
HWND hWnd=NULL;
while(GetMessage(&msg,NULL,0,0))
{
//TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
unsigned int ver = _WIN32_IE ;
InitCommonControls();
g_hInstance = hInstance;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 16;
wndclass.cbWndExtra = 16;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName="MyWindow";
if(!(RegisterClass(&wndclass)))
{
MessageBox(NULL,"RegisterClass error",NULL,MB_ICONERROR);
}
//CreateWindowA(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam)
HWND hWindow = CreateWindow("MyWindow","WindowName",WS_OVERLAPPEDWINDOW,100,100,700,500,NULL,
LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)),hInstance,NULL);
ShowWindow(hWindow,SHOW_OPENWINDOW);
UpdateWindow(hWindow);
SetClassLong(hWindow,1,1111);
//子窗口控件
HWND hWindow2 = CreateWindow("button","我是按钮1111",WS_CHILD|WS_VISIBLE,
0,0,100,40,hWindow,HMENU(1111),hInstance,NULL);
HWND hWindowOwnerDraw = CreateWindow("button","我是按钮4444-OWNERDRAW",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
120,0,100,40,hWindow,HMENU(4444),hInstance,NULL);
// ShowWindow(hWindow2,SHOW_OPENWINDOW);
// UpdateWindow(hWindow2);
//子窗口窗口
HWND hWindow3 = CreateWindow("MyWindow","我是子窗口",WS_OVERLAPPEDWINDOW|WS_CHILD|WS_VISIBLE,
100,100,200,100,hWindow,LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1)),hInstance,NULL);
//子窗口窗口 的 子控件
HWND hWindow4 = CreateWindow("button","我是按钮2222",WS_CHILD|WS_VISIBLE,
0,0,100,40,hWindow3,HMENU(2222),hInstance,NULL);
SendMessage(hWindow,100,0,0);
PostMessage(hWindow,100,0,0);
HANDLE hThread = CreateThread(NULL,0,pfnFunThread,NULL,0,NULL);
CloseHandle(hThread);
CreateListView(hInstance,hWindow);
MSG msg={0};
HWND hWnd=NULL;
while(GetMessage(&msg,NULL,0,0))
{
//TranslateMessage(&msg);
//此线程所有回调函数 包括,父窗口,子窗口控件(如,子窗口是 按钮),子窗口窗口(子窗口依旧是window),
//非模态对话框的回调函数,都是由此调用;子窗口控件,只能向父窗口发送WM_COMMAND消息,而子窗口窗口,可以,各种窗口消息
//模态对话框消息不进入消息队列
//IsDialogMessage 发送消息到 非模态对话框 回调函数,但是,不调用IsDialogMessage,DispatchMessage也可以把
//消息 发送到 非模态对话框,奇怪哉
if(hDlgModeless==0||!IsDialogMessage(hDlgModeless,&msg))
DispatchMessage(&msg);
}
return msg.wParam;
}
//回调函数只获取 此线程当中 窗口(包括父窗口和子窗口)消息
LRESULT WINAPI WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
LPCREATESTRUCT lpCreateStruct = LPCREATESTRUCT (lParam);
HDC hDC = GetDC(hwnd);
TextOut(hDC,20,20,"WM_CREATE",9);
ReleaseDC(hwnd,hDC);
return 0;
}
case WM_INITDIALOG://只有对话框才会收到?
{
int i = 0;
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps = {0};
RECT rect;
GetWindowRect(hwnd,&rect);
HDC hDC =BeginPaint(hwnd,&ps);
SelectObject(hDC,(HBRUSH)GetStockObject(GRAY_BRUSH));
for (int i = 0;i<1000;i++)
{
Rectangle(hDC,ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right,ps.rcPaint.bottom);
}
for (int i = 0;i<100;i++)
{
for (int j = 0;j<100;j++)
{
LineTo(hDC,rect.right,rect.bottom+i);
MoveToEx(hDC,0,0,NULL);
}
}
DWORD dwClsExtra = GetClassLong(hwnd,1);
DWORD dwWndExtra = GetClassLong(hwnd,GCL_CBWNDEXTRA);
EndPaint(hwnd,&ps);
return 0 ;
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_40001:
MessageBox(hwnd,"点击了菜单",NULL,MB_OK);
SetWindowLong(NULL,GCL_HCURSOR,(LONG)LoadCursor(g_hInstance,IDC_WAIT));
return 0;
case ID_40002:
//DialogBoxParamA(hInstance, lpTemplate, hWndParent, lpDialogFunc, 0L)
//#define MAKEINTRESOURCEA(i) ((LPSTR)((ULONG_PTR)((WORD)(i))))
//模态对话框
if(DialogBox(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG1) ,NULL,DlgProcModel))
MessageBox(hwnd,"点击OK",NULL,MB_OK);
else
MessageBox(hwnd,"点击Cancel",NULL,MB_OK);
return 0;
case ID_40003:
{
//非模态对话框
hDlgModeless = CreateDialog(g_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgProcModeless);
//这个button没有显示创建,随着此 非模态dialog的创建就创建好了。
HWND hDlgLessButton = GetDlgItem(hDlgModeless,ID_BUTTON);
return 0;
}
case ID_40004://从内存模板创建非模态对话框
{
DLGTEMPLATE myTemplate = {0};
myTemplate.style = WS_VISIBLE;
myTemplate.x = 600;myTemplate.y = 600;myTemplate.cx = 600;myTemplate.cy = 600;
CreateDialogIndirect(g_hInstance,&myTemplate,NULL,DlgProcModel);
return 0;
}
case 1111:
MessageBox(hwnd,"点击了按钮1111",NULL,MB_OK);
return 0;
case 4444:
MessageBox(hwnd,"点击了按钮4444",NULL,MB_OK);
return 0;
case 2222:
MessageBox(hwnd,"点击了按钮2222",NULL,MB_OK);
return 0;
case 3333:
MessageBox(hwnd,"点击了按钮3333",NULL,MB_OK);
return 0;
}
break;
case WM_NOTIFY:
switch(wParam)
{
case 1234:
{
UINT hdn_first = HDN_FIRST;
LPNMHDR pnmh = (LPNMHDR) lParam;
switch (pnmh->code)
{
case HDN_ITEMKEYDOWN:
MessageBox(NULL,"List 控件 击中了Item",NULL,MB_OK);
return 0;
case HDN_ITEMCHANGING:
MessageBox(NULL,"List 控件 Item Changing",NULL,MB_OK);
return 0;
case HDN_ITEMCLICK:
MessageBox(NULL,"List 控件 Item 单击",NULL,MB_OK);
return 0;
case HDN_ITEMDBLCLICK:
MessageBox(NULL,"List 控件 Item 双击",NULL,MB_OK);
return 0;
case HDN_BEGINTRACK:
MessageBox(NULL,"List 控件 BeginTrack",NULL,MB_OK);
return 0;
// case NM_CLICK:
// MessageBox(NULL,"List 控件 NM_CLICK",NULL,MB_OK);
// return 0;
default:
// char msg[128] = {0};
// sprintf(msg,"List 控件 idFrom:%d code:%0x",pnmh->idFrom,pnmh->code);
// MessageBox(NULL,msg,NULL,MB_OK);
return 0;
}
}
}
break;
case WM_DRAWITEM:
switch(wParam)
{
case 4444:
DRAWITEMSTRUCT drawItemStruct = *(DRAWITEMSTRUCT*)&lParam;
//MessageBox(hwnd,"按钮44444WM_DRAWITEM",NULL,MB_OK);
return 0;
}
case WM_CTLCOLORDLG:
return 0;
case WM_DESTROY:
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
//模态对话框回调函数 //由DialogBox调用
BOOL CALLBACK DlgProcModel (HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG://只有对话框才会收到?
{
int i = 0;
return 0;
}
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
//第二个参数是 对话框返回值
EndDialog (hDlg, TRUE) ;
return TRUE ;
case IDCANCEL:
EndDialog (hDlg, FALSE) ;
return FALSE ;
}
}
return FALSE;
}
//非模态对话框回调函数
BOOL CALLBACK DlgProcModeless(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
DestroyWindow(hDlg);
hDlgModeless = NULL;
return TRUE ;
case IDCANCEL:
DestroyWindow(hDlg);
hDlgModeless = NULL;
return TRUE ;
}
}
return FALSE;
}
HWND CreateListView(HINSTANCE hInstance, HWND hwndParent)
{
DWORD dwStyle;
HWND hwndListView;
HIMAGELIST himlSmall;
HIMAGELIST himlLarge;
BOOL bSuccess = TRUE;
dwStyle = WS_TABSTOP |
WS_CHILD |
WS_BORDER |
WS_VISIBLE |
LVS_AUTOARRANGE |
LVS_REPORT |
!LVS_OWNERDATA;
hwndListView = CreateWindowEx( WS_EX_CLIENTEDGE , // ex style
WC_LISTVIEW, // class name - defined in commctrl.h
TEXT(""), // dummy text
dwStyle, // style
250, // x position
200, // y position
300, // width
180, // height
hwndParent, // parent
(HMENU)1234, // ID
hInstance, // instance
NULL); // no extra data
if(!hwndListView)
return NULL;
DWORD dwStyleEx = ListView_GetExtendedListViewStyle(hwndListView);
dwStyleEx |= LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT
|LVS_EX_ONECLICKACTIVATE|LVS_EX_AUTOCHECKSELECT;
SendMessage(hwndListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM) dwStyleEx);
//初始化表格
TCHAR rgtsz[4][10] = {("名称"),("进程ID"),("线程数"),("父进程ID")};
LV_COLUMN lvcolumn;
RECT rect;
GetWindowRect(hwndListView,&rect);
for(int i=0;i<4;i++)
{
//列属性声明:对齐方式|子项的起始索引值|列标题使用文本|允许定义列的宽度|允许排序
lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH | LVCF_ORDER;
lvcolumn.fmt = LVCFMT_LEFT;//文本左对齐
lvcolumn.pszText = rgtsz[i];//列的名字
lvcolumn.iSubItem = i;//列的索引
lvcolumn.iOrder = i;//列顺序从0开始
if(i==0)
lvcolumn.cx = (rect.right-rect.left)*1.5/5.0;
else
lvcolumn.cx = (rect.right-rect.left)*1.0/5.0;
SendMessage(hwndListView,LVM_INSERTCOLUMN,i,(LPARAM)&lvcolumn);
}
int iRet = 0;
//由于是添加在末尾一行,我们需要计算出列表中已经有了几行
int nIndex =ListView_GetItemCount(hwndListView);//获取表格中有多少行
//这里我们要先在列表中创建一行,然后在修改其中的参数
LV_ITEM lvitemAdd = {0};//添加一行
lvitemAdd.mask = LVIF_TEXT;
lvitemAdd.iItem = nIndex ;//添加到最后一列
lvitemAdd.iSubItem = 0;//添加的列
lvitemAdd.pszText="A";
lvitemAdd.cchTextMax = 20;
iRet = ListView_InsertItem(hwndListView,&lvitemAdd);
//修改子项中的内容
lvitemAdd.iSubItem = 1;
lvitemAdd.pszText="1111";
iRet = ListView_SetItem(hwndListView,&lvitemAdd);
lvitemAdd.iSubItem = 2;
lvitemAdd.pszText="2222";
iRet = ListView_SetItem(hwndListView,&lvitemAdd);
lvitemAdd.iSubItem = 3;
lvitemAdd.pszText="3333";
iRet = ListView_SetItem(hwndListView,&lvitemAdd);
return hwndListView;
}