简单的自绘制按钮 和下拉列表的消息事件等 14.4.24

// WndConst.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "iostream"
HINSTANCE g_hInst   = NULL;
HANDLE g_hStdOut=NULL;
HWND hSD=NULL;
void onCreate(HWND hWnd,
						  UINT nMsg,
						  WPARAM wParam,
						  LPARAM lParam)
{
	//char szText[50]="hello listbox";
	//SBS_HORZ SBS_VERT也是滚动条 对应的消息处理的WM_HSCROLL WM_VSCROLL 
	//SBM_SETRANGE 设置滚动条的滚动范围 lparam 最小值 wparam最大  SBM_GETRANGE 获取滚动条的值对于的事件SB_LINEUP SB_LINEDOWN SB_PAGELEFT SB_PAGERIGHT
	//鼠标拉动滚动条 使其停留在鼠标UP的位置 用SB_THUMBPOSITION  SendMessage(hMenus,SBM_SETPOS,pos,TRUE);
	//hSD=CreateWindow("LISTBOX","LISTBX",WS_CHILD|WS_VISIBLE|SBS_HORZ|SBS_VERT,100,100,100,150,hWnd,(HMENU)10001,g_hInst,NULL);
	//SendMessage(hMenus,SBM_SETRANGE,0,300);
	//SendMessage(hMenus,LB_INSERTSTRING,0,(LPARAM)szText);

	CreateWindow("BUTTON","BUTTON",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,50,100,100,100,hWnd,(HMENU)1001,g_hInst,NULL);

}
void onDrawitem(HWND hWnd,
						  UINT nMsg,
						  WPARAM wParam,
						  LPARAM lParam)
{
	LPDRAWITEMSTRUCT pDis=(LPDRAWITEMSTRUCT)lParam;
	if(ODT_BUTTON==pDis->CtlType)
	{
		HBRUSH hOldBrush=NULL;
		HBRUSH hBrush=NULL;
		//设置文字背景色为透明
		int Sold= SetBkMode(pDis->hDC,TRANSPARENT);
		if(pDis->itemState & ODS_SELECTED){
		//绘制一个圆角的按钮
		hBrush=CreateSolidBrush(RGB(87,139,87));
		hOldBrush=(HBRUSH)SelectObject(pDis->hDC,hBrush);
		RoundRect(pDis->hDC,pDis->rcItem.left,pDis->rcItem.top,
			pDis->rcItem.right,pDis->rcItem.bottom,15,15);
		}else{
			hBrush=CreateSolidBrush(RGB(165,100,1));
			hOldBrush=(HBRUSH)SelectObject(pDis->hDC,hBrush);
			RoundRect(pDis->hDC,pDis->rcItem.left,pDis->rcItem.top,
			pDis->rcItem.right,pDis->rcItem.bottom,15,15);
		}
		char szName[260]={0};
		//获取句柄的文字
		GetWindowText(pDis->hwndItem,szName,260);

		//绘制
		DrawText(pDis->hDC,szName,strlen(szName),
			&pDis->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
		SelectObject(pDis->hDC,hOldBrush);
		DeleteObject(hBrush);
		//用完后恢复
		SetBkMode(pDis->hDC,Sold);
	}
}
void onPaint(HWND hWnd,
						  UINT nMsg,
						  WPARAM wParam,
						  LPARAM lParam)
{
	PAINTSTRUCT ps={0};
	HDC hDC=BeginPaint(hWnd,&ps);
	SendMessage(hWnd,SBM_SETRANGE,300,true);
	int s;
	int vP=SendMessage(hWnd,SBM_GETRANGE,s,0);
	int vV=SendMessage(hWnd,SBM_GETRANGE,0,0);
	EndPaint(hWnd,&ps);
}
void onMeasureItem(HWND hWnd,
						  UINT nMsg,
						  WPARAM wParam,
						  LPARAM lParam)
{
	LPMEASUREITEMSTRUCT pMis=(LPMEASUREITEMSTRUCT)lParam;
	pMis->itemHeight=200;

}
LRESULT CALLBACK WndProc( HWND hWnd,
						  UINT nMsg,
						  WPARAM wParam,
						  LPARAM lParam )
{
	switch( nMsg )
	{
	case WM_DESTROY:
		PostQuitMessage( 0 );
		return 0;
	case WM_CREATE:
		onCreate(hWnd,nMsg,wParam,lParam);
		break;
	case WM_PAINT:
		onPaint(hWnd,nMsg,wParam,lParam);
		break;
	case WM_DRAWITEM:
		onDrawitem(hWnd,nMsg,wParam,lParam);
		break;
	case WM_MEASUREITEM:
		onMeasureItem(hWnd,nMsg,wParam,lParam);
	}
	return DefWindowProc( hWnd, nMsg,
		wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize        = sizeof( wce );
	wce.cbClsExtra    = 0;
	wce.cbWndExtra    = 0;
	wce.hbrBackground = HBRUSH(COLOR_SCROLLBAR+1);
	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_VREDRAW|CS_HREDRAW;

	ATOM nAtom = RegisterClassEx( &wce );
	if( 0 == nAtom )
	{
		return FALSE;
	}
	return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
	
	HWND hWnd = CreateWindowEx( 0,
		pszClassName, "MyWnd", 
		WS_TILEDWINDOW|WS_CLIPCHILDREN|SBM_SETRANGE|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, NULL, NULL, 
		g_hInst, 0 );
	return hWnd;
}


void DisplayWnd( HWND hWnd )
{
	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );
}

void Message( )
{
	MSG msg = {0};
	while ( GetMessage( &msg, NULL, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}
void NewConsole( )
{
	AllocConsole( );
	g_hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	NewConsole( );
	g_hInst = hInstance;
	RegisterWnd( "MYWND" );
	HWND hWnd = CreateWnd( "MYWND" );
	DisplayWnd( hWnd );
	Message( );
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值