本文是截图:Windows编程基础 第十章 组合框 列表框 滚动条 自绘制控件
源文件pdf下载地址是:http://yunpan.cn/cdeWCMEFbXBIW 访问密码 d5d2
//本程序用来演示ComboBox
#include "windows14.h"
#include "stdafx.h"
#include "resource.h"
#include "stdio.h"
#include "windows.h"
HINSTANCE g_hInst=NULL;
VOID OnAdd(HWND hWnd);
VOID OnDel(HWND hWnd);
VOID OnReset(HWND hWnd);
VOID OnSelect(HWND hWnd);
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
CreateWindowEx(0,TEXT("COMBOBOX"),TEXT("SIMPLE"),WS_CHILD|WS_VISIBLE|CBS_SIMPLE,50,50,120,300,hWnd,(HMENU)1001,g_hInst,NULL);
CreateWindowEx(0,TEXT("COMBOBOX"),TEXT("DROPDOWN"),WS_CHILD|WS_VISIBLE|CBS_DROPDOWN|CS_VREDRAW,200,50,120,300,hWnd,(HMENU)1002,g_hInst,NULL);//这种风格可以输入文字
CreateWindowEx(0,TEXT("COMBOBOX"),TEXT("DROPDOWNLIST"),WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST|CS_VREDRAW,350,50,120,300,hWnd,(HMENU)1003,g_hInst,NULL);
}
VOID OnAdd(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
TCHAR szText[]=TEXT("ComBoBox 0");
int nLen=_tcslen(szText);
for (int nIndex=0;nIndex<10;nIndex++)
{
LRESULT nItem=SendMessage(hSimple,CB_ADDSTRING,0,(LPARAM)szText);//给ComboBox中添加项。
SendMessage(hSimple,CB_SETITEMDATA,nIndex,100+nIndex);
SendMessage(hDropDown,CB_ADDSTRING,0,(LPARAM)szText);
SendMessage(hDropDownList,CB_ADDSTRING,0,(LPARAM)szText);
szText[9]++;
}
}
VOID OnDel(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
SendMessage(hSimple,CB_DELETESTRING,5,0);//删除第六项
SendMessage(hDropDown,CB_DELETESTRING,5,0);//删除第六项
SendMessage(hDropDownList,CB_DELETESTRING,5,0);//删除第六项
}
VOID OnReset(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
SendMessage(hSimple,CB_RESETCONTENT,0,0);
SendMessage(hDropDown,CB_RESETCONTENT,0,0);
SendMessage(hDropDownList,CB_RESETCONTENT,0,0);
}
VOID OnSelect(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
LRESULT nSel=SendMessage(hSimple,CB_GETCURSEL,0,0);
LRESULT nValue=SendMessage(hSimple,CB_GETITEMDATA,nSel,0);
SendMessage(hDropDown,CB_SETCURSEL,nSel,0);
TCHAR szText[10]=TEXT("");
_stprintf(szText,TEXT("%d"),nValue);
MessageBox(NULL,szText,TEXT("获取到的附加数据:"),MB_OK);
TCHAR szSelect[]=TEXT("ComBoBox 5");
SendMessage(hDropDownList,CB_SELECTSTRING,-1,(LPARAM)szSelect);
}
VOID OnFind(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
TCHAR szSelect[]=TEXT("ComBoBox 5");
LRESULT nFind1=SendMessage(hSimple,CB_FINDSTRING,-1,(LPARAM)szSelect);
SendMessage(hDropDown,CB_SETCURSEL,nFind1,0);
SendMessage(hDropDownList,CB_SETCURSEL,nFind1,0);
}
VOID OnPath(HWND hWnd)
{
HWND hSimple=GetDlgItem(hWnd,1001);
HWND hDropDown=GetDlgItem(hWnd,1002);
HWND hDropDownList=GetDlgItem(hWnd,1003);
TCHAR szSelect[MAX_PATH]=TEXT("C:\\*.*");
SendMessage(hSimple,CB_DIR,DDL_ARCHIVE|DDL_DIRECTORY|DDL_HIDDEN,(LPARAM)szSelect);
SendMessage(hDropDown,CB_DIR,DDL_ARCHIVE|DDL_DIRECTORY|DDL_HIDDEN,(LPARAM)szSelect);
SendMessage(hDropDownList,CB_DIR,DDL_ARCHIVE|DDL_DIRECTORY|DDL_HIDDEN,(LPARAM)szSelect);
}
VOID OnCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int nNotifyCode=HIWORD(wParam);
int nCtrlIDandMenuEvent=LOWORD(wParam);
switch(nCtrlIDandMenuEvent)
{
case ID_ADD:
OnAdd(hWnd);
break;
case ID_DEL:
OnDel(hWnd);
break;
case ID_RESET:
OnReset(hWnd);
break;
case ID_SEL:
OnSelect(hWnd);
break;
case ID_FIND:
OnFind(hWnd);
break;
case ID_PATH:
OnPath(hWnd);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND://用来处理菜单消息
OnCommand(hWnd,uMsg,wParam,lParam);//
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
wce.cbWndExtra=0;
wce.hbrBackground=HBRUSH(COLOR_WINDOW+1);//GetStockBrush(WHITE_BRUSH);
wce.hCursor=NULL;
wce.hIcon=NULL;
wce.hIconSm=NULL;
wce.hInstance=g_hInst;
wce.lpfnWndProc=WndProc;
wce.lpszClassName=pszClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message(HWND hWnd)
{
HACCEL hAccel=LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS14));
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateAccelerator(hWnd,hAccel,&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
//加载字符串资源
TCHAR szText[100]=TEXT("");
LoadString(g_hInst,IDS_STRINGMain,szText,100);
//加载菜单资源
HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS14));
HWND hWndNew=CreateWindowEx(0,pszClassName,szText,WS_OVERLAPPEDWINDOW|WS_THICKFRAME,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
return hWndNew;
}
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message(hWnd);
return TRUE;
}
// windows15.cpp : ListBox使用演示程序
//
#include "stdafx.h"
#include "windows15.h"
HINSTANCE g_hInst=NULL;
HWND hSingle=NULL;
HWND hMult=NULL;
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
hSingle=CreateWindowEx(0,TEXT("LISTBOX"),TEXT("SIGLE"),WS_CHILD|WS_VISIBLE|LBS_NOTIFY,50,50,200,400,hWnd,(HMENU)1001,g_hInst,NULL);
hMult=CreateWindowEx(0,TEXT("LISTBOX"),TEXT("Mult"),WS_CHILD|WS_VISIBLE|LBS_NOTIFY|LBS_MULTIPLESEL,300,50,200,400,hWnd,(HMENU)1002,g_hInst,NULL);
//这个时候ListBox已经创建出来了,但是你在窗口上什么也看不见,你点击一下,能看到两个方框。我们需要向里面添加内容。
}
VOID OnAdd(HWND hWnd)
{
TCHAR szText[]=TEXT("ListBox 0");
for (int nIndex=0;nIndex<10;nIndex++)
{
LRESULT nItem=SendMessage(hSingle,LB_ADDSTRING,0,(LPARAM)szText);
SendMessage(hMult,LB_ADDSTRING,0,(LPARAM)szText);
szText[8]++;
}
}
VOID OnSel(HWND hWnd)
{
SendMessage(hSingle,LB_SETCURSEL,2,0);
for(int nIndex=0;nIndex<10;nIndex++)
{
SendMessage(hMult,LB_SETSEL,TRUE,nIndex);
}
LRESULT nCount=SendMessage(hMult,LB_GETSELCOUNT,0,0);
if (nCount==10)
{
MessageBox(hMult,TEXT("被选择的数量是10!"),TEXT("选择数量:"),MB_OK);
}
INT nSel[20]={0};
SendMessage(hMult,LB_GETSELITEMS,20,(LPARAM)nSel);
}
VOID OnCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int nNotifyCode=HIWORD(wParam);
int nCtrlIDandMenuEvent=LOWORD(wParam);
switch(nCtrlIDandMenuEvent)
{
case ID_ADD:
OnAdd(hWnd);
break;
case ID_SEL:
OnSel(hWnd);
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND://用来处理菜单消息
OnCommand(hWnd,uMsg,wParam,lParam);
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
wce.cbWndExtra=0;
wce.hbrBackground=HBRUSH(COLOR_WINDOW+5);//GetStockBrush(WHITE_BRUSH);
wce.hCursor=NULL;
wce.hIcon=NULL;
wce.hIconSm=NULL;
wce.hInstance=g_hInst;
wce.lpfnWndProc=WndProc;
wce.lpszClassName=pszClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message(HWND hWnd)
{
HACCEL hAccel=LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS15));
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateAccelerator(hWnd,hAccel,&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
//加载字符串资源
TCHAR szText[100]=TEXT("");
LoadString(g_hInst,IDS_STRINGMain,szText,100);
//加载菜单资源
HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS15));
HWND hWndNew=CreateWindowEx(0,pszClassName,szText,WS_OVERLAPPEDWINDOW|WS_THICKFRAME,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
return hWndNew;
}
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message(hWnd);
return TRUE;
}
// 演示滚动条的使用
#include "stdafx.h"
#include "windows16.h"
HINSTANCE g_hInst=NULL;
HWND g_hHorz=NULL;
HWND g_hVert=NULL;
VOID OnCommand(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int nNotifyCode=HIWORD(wParam);
int nCtrlIDandMenuEvent=LOWORD(wParam);
switch(nCtrlIDandMenuEvent)
{
}
}
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
g_hHorz=CreateWindowEx(0,TEXT("SCROLLBAR"),TEXT("HORZ"),WS_VISIBLE|WS_CHILD|SBS_HORZ,0,300,300,30,hWnd,(HMENU)1001,g_hInst,NULL);//创建水平滚动条
g_hVert=CreateWindowEx(0,TEXT("SCROLLBAR"),TEXT("VERT"),WS_CHILD|WS_VISIBLE|SBS_VERT,300,0,30,300,hWnd,(HMENU)1002,g_hInst,NULL);//创建垂直滚动条
SendMessage(g_hHorz,SBM_SETRANGE,0,300);
SendMessage(g_hVert,SBM_SETRANGE,0,300);
}
VOID OnHSCROLL(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int nPosition=HIWORD(wParam);
//只有在SB_THUMBPOSITION和SB_THUMBTRACK时候这个才好使,所以其他的消息需要自己计算位置。
int nScroolCode=LOWORD(wParam);
switch(nScroolCode)
{
case SB_LINELEFT:
nPosition=SendMessage(g_hHorz,SBM_GETPOS,0,0);
nPosition=nPosition-5;
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
case SB_LINERIGHT:
nPosition=SendMessage(g_hHorz,SBM_GETPOS,0,0);
nPosition=nPosition+5;
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
case SB_PAGELEFT:
nPosition=SendMessage(g_hHorz,SBM_GETPOS,0,0);
nPosition=nPosition-20;
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
case SB_PAGERIGHT:
nPosition=SendMessage(g_hHorz,SBM_GETPOS,0,0);
nPosition=nPosition+20;
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
case SB_THUMBTRACK:
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
case SB_THUMBPOSITION:
SendMessage(g_hHorz,SBM_SETPOS,nPosition,TRUE);
break;
}
InvalidateRect(hWnd,NULL,TRUE);
}
VOID OnVSCROLL(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
int nPosition=HIWORD(wParam);
//只有在SB_THUMBPOSITION和SB_THUMBTRACK时候这个才好使,所以其他的消息需要自己计算位置。
int nScroolCode=LOWORD(wParam);
switch(nScroolCode)
{
case SB_LINEUP:
nPosition=SendMessage(g_hVert,SBM_GETPOS,0,0);
nPosition=nPosition-5;
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
case SB_LINEDOWN:
nPosition=SendMessage(g_hVert,SBM_GETPOS,0,0);
nPosition=nPosition+5;
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
case SB_PAGEUP:
nPosition=SendMessage(g_hVert,SBM_GETPOS,0,0);
nPosition=nPosition-20;
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
case SB_PAGEDOWN:
nPosition=SendMessage(g_hVert,SBM_GETPOS,0,0);
nPosition=nPosition+20;
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
case SB_THUMBTRACK:
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
case SB_THUMBPOSITION:
SendMessage(g_hVert,SBM_SETPOS,nPosition,TRUE);
break;
}
InvalidateRect(hWnd,NULL,TRUE);
}
VOID OnPaint(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps={0};
HDC hDC=BeginPaint(hWnd,&ps);
TCHAR szText[]=TEXT("Hello ScrollBar!");
int nVert=SendMessage(g_hVert,SBM_GETPOS,0,0);
int nHorn=SendMessage(g_hHorz,SBM_GETPOS,0,0);
TextOut(hDC,-nHorn,-nVert,szText,_tcslen(szText));
EndPaint(hWnd,&ps);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND://用来处理菜单消息
OnCommand(hWnd,uMsg,wParam,lParam);
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
case WM_HSCROLL:
OnHSCROLL(hWnd,uMsg,wParam,lParam);
break;
case WM_VSCROLL:
OnVSCROLL(hWnd,uMsg,wParam,lParam);
break;
case WM_PAINT:
OnPaint(hWnd,uMsg,wParam,lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
wce.cbWndExtra=0;
wce.hbrBackground=HBRUSH(COLOR_WINDOW+1);//GetStockBrush(WHITE_BRUSH);
wce.hCursor=NULL;
wce.hIcon=NULL;
wce.hIconSm=NULL;
wce.hInstance=g_hInst;
wce.lpfnWndProc=WndProc;
wce.lpszClassName=pszClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message(HWND hWnd)
{
HACCEL hAccel=LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS16));
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateAccelerator(hWnd,hAccel,&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
//加载字符串资源
TCHAR szText[100]=TEXT("");
LoadString(g_hInst,IDS_STRINGMain,szText,100);
//加载菜单资源
HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS16));
HWND hWndNew=CreateWindowEx(0,pszClassName,szText,WS_OVERLAPPEDWINDOW|WS_THICKFRAME|WS_VSCROLL|WS_HSCROLL|WS_CLIPCHILDREN,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
return hWndNew;
}
/*
WS_VSCROLL|WS_HSCROLL注意这两个滚动条和我们的自己创建的滚动条使用的是同一个消息处理函数,所以我们想区分他们就需要使用窗口句柄
WS_CLIPCHILDREN//剪切子窗口风格,防止滚动条形成的窗口,发生闪烁的现象
*/
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message(hWnd);
return TRUE;
}
// 本程序用来演示自绘制控件的使用
//
#include "stdafx.h"
#include "windows17.h"
HINSTANCE g_hInst=NULL;
HWND g_hHorz=NULL;
HWND g_hVert=NULL;
VOID OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
CreateWindowEx(0,TEXT("BUTTON"),TEXT("MyBUTTON"),WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,50,50,200,50,hWnd,(HMENU)1001,g_hInst,NULL);
}
/*
typedef struct tagDRAWITEMSTRUCT {
UINT CtlType;
UINT CtlID;
UINT itemID;
UINT itemAction;
UINT itemState; //控件的状态
HWND hwndItem; //控件的句柄
HDC hDC; //控件的绘图区域的HDC
RECT rcItem; //控件的矩形区域
DWORD itemData;
} DRAWITEMSTRUCT;
*/
VOID OnDrawItem(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
LPDRAWITEMSTRUCT pDis=(LPDRAWITEMSTRUCT)lParam;
if (pDis->CtlType==ODT_BUTTON)//如果控件的类型是Owner-drawn button则执行下面的语句
{
if (pDis->itemState&ODS_SELECTED)//如果按钮的状态是被选中的状态
{
//设置一种按钮的颜色,被选中状态下
HBRUSH hBrush=CreateSolidBrush(RGB(200,200,255));
HBRUSH hOldBrush=(HBRUSH)SelectObject(pDis->hDC,hBrush);
RoundRect(pDis->hDC,pDis->rcItem.left,pDis->rcItem.top,pDis->rcItem.right,pDis->rcItem.bottom,20,20);
SelectObject(pDis->hDC,hOldBrush);
DeleteObject(hOldBrush);
}
else
{
//设置一种按钮的颜色,没有被选中的状态下
HBRUSH hBrush=CreateSolidBrush(RGB(100,100,255));
HBRUSH hOldBrush=(HBRUSH)SelectObject(pDis->hDC,hBrush);
RoundRect(pDis->hDC,pDis->rcItem.left,pDis->rcItem.top,pDis->rcItem.right,pDis->rcItem.bottom,20,20);
SelectObject(pDis->hDC,hOldBrush);
DeleteObject(hOldBrush);
}
TCHAR szName[260]={0};
GetWindowText(pDis->hwndItem,szName,260);//取得按钮上面原来的文字
int nOldMode=SetBkMode(pDis->hDC,TRANSPARENT);//设置文字背景模式
DrawText(pDis->hDC,szName,_tcslen(szName),&pDis->rcItem,DT_CENTER|DT_SINGLELINE|DT_VCENTER);//绘制位置
SetBkMode(pDis->hDC,nOldMode);//还原原来的背景模式
}
}
VOID OnMeasureItem(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
LPMEASUREITEMSTRUCT pMis=(LPMEASUREITEMSTRUCT)lParam;
pMis->itemHeight=400;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
case WM_MEASUREITEM:
OnMeasureItem(hWnd,uMsg,wParam,lParam);
break;
case WM_DRAWITEM:
OnDrawItem(hWnd,uMsg,wParam,lParam);
break;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
BOOL RegisterWnd(TCHAR * pszClassName )
{
WNDCLASSEX wce={0};
wce.cbClsExtra=0;
wce.cbSize=sizeof(wce);
wce.cbWndExtra=0;
wce.hbrBackground=HBRUSH(COLOR_WINDOW+1);//GetStockBrush(WHITE_BRUSH);
wce.hCursor=NULL;
wce.hIcon=NULL;
wce.hIconSm=NULL;
wce.hInstance=g_hInst;
wce.lpfnWndProc=WndProc;
wce.lpszClassName=pszClassName;
wce.lpszMenuName=NULL;
wce.style=CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
ATOM result=RegisterClassEx(&wce);
if (result==0)
{
MessageBox(NULL,TEXT("窗口类注册失败!"),TEXT("窗口类注册失败!"),MB_OK);
return FALSE;
}
return TRUE;
}
VOID DisplayWindow(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
VOID Message(HWND hWnd)
{
HACCEL hAccel=LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS17));
MSG msg={0};
while(GetMessage(&msg,NULL,0,0))
{
TranslateAccelerator(hWnd,hAccel,&msg);
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
HWND CreateMyWindow(TCHAR * pszClassName)
{
//加载字符串资源
TCHAR szText[100]=TEXT("");
LoadString(g_hInst,IDS_STRINGMain,szText,100);
//加载菜单资源
HMENU hMenu=LoadMenu(g_hInst,MAKEINTRESOURCE(IDC_WINDOWS17));
HWND hWndNew=CreateWindowEx(0,pszClassName,szText,WS_OVERLAPPEDWINDOW|WS_THICKFRAME|WS_VSCROLL|WS_HSCROLL|WS_CLIPCHILDREN,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
return hWndNew;
}
/*
WS_VSCROLL|WS_HSCROLL注意这两个滚动条和我们的自己创建的滚动条使用的是同一个消息处理函数,所以我们想区分他们就需要使用窗口句柄
WS_CLIPCHILDREN//剪切子窗口风格,防止滚动条形成的窗口,发生闪烁的现象
*/
INT WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd )
{
RegisterWnd(TEXT("MyWnd"));
HWND hWnd=CreateMyWindow(TEXT("MyWnd"));
DisplayWindow(hWnd);
Message(hWnd);
return TRUE;
}