@likilix
Лицемер

В чем проблема отрисовки WM_DRAWITEM?

Пытаюсь сделать свою кнопку. Проблема заключается в курсоре, который меняет вид(постоянно крутится) на форме, пока не наведешь его на мою кнопу, и потом он тоже странно работает

case WM_SETCURSOR:
	   if( (HWND)wParam == ChBox[0] && (GetFocus() != ChBox[0]) ) SetFocus( ChBox[0] );
	return FALSE;


case WM_DRAWITEM:
		{
			LPDRAWITEMSTRUCT Draw = (LPDRAWITEMSTRUCT)lParam;

			hDCMem = CreateCompatibleDC( Draw->hDC );

			if( Draw->CtlType == ODT_BUTTON )
			{
				int indexButton = 0;

					switch( Draw->itemAction )
					{

						case ODA_FOCUS:

							if( Draw->itemState & ODS_FOCUS )
							{
								SetWindowText( hWnd, "ODA_FOCUS:ODS_FOCUS" );
							}
							else
							{
								SetWindowText( hWnd, "ODA_FOCUS" );
							}

							
						break;	 

	

						case ODA_SELECT: 
							if( Draw->itemState & ODS_SELECTED )
							{
								SetWindowText( hWnd, "ODA_SELECT:ODS_SELECTED" );
							}
							else
							{
								SetWindowText( hWnd, "ODA_SELECT" );
							}
						break;

						default: SetWindowText( hWnd, "Def" );
					}


				GetObject( hBitmap[ indexButton ], sizeof(BITMAP), (VOID*)&Bitmap );

			    SelectObject( hDCMem, hBitmap[ indexButton ] ); 
				BitBlt( Draw->hDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, hDCMem, 0, 0, SRCCOPY );

			}

			DeleteDC( hDCMem );
			return TRUE;
		}
  • Вопрос задан
  • 758 просмотров
Пригласить эксперта
Ответы на вопрос 2
return FALSE;
это обязательно FALSE возвращать?
Ответ написан
@likilix Автор вопроса
Лицемер
#include <Windows.h>
#include <WindowsX.h>

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[20] = {0};					// The title bar text
TCHAR szWindowClass[20] = "fdfdssd";			// the main window class name

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE g_hInstance;

int APIENTRY WinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{


	g_hInstance = hInstance;

 	// TODO: Place code here.
	MSG msg;


	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}


	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, IDI_APPLICATION);
	wcex.hCursor		= LoadCursor(hInstance, IDC_ARROW);
	wcex.hbrBackground	= GetStockBrush(0);
	wcex.lpszMenuName	= NULL;//MAKEINTRESOURCE(IDC_DRAW);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(hInstance, IDI_APPLICATION);

	return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

HWND ChBox[3];




LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

	static HDC     hDCMem;
	static HBITMAP hBitmap[3];
	static BITMAP   Bitmap;

	PAINTSTRUCT ps;
	HDC hdc;

	static LRESULT res;

	switch (message)
	{
	case WM_SETCURSOR:
		
	if( (HWND)wParam == ChBox[0] && (GetFocus() != ChBox[0]) ) SetFocus( ChBox[0] );
	//if( (HWND)wParam == ChBox[0] ) SetFocus( ChBox[0] );
	break;
	
	case WM_DRAWITEM:
		{
			LPDRAWITEMSTRUCT Draw = (LPDRAWITEMSTRUCT)lParam;

			hDCMem = CreateCompatibleDC( Draw->hDC );

			if( Draw->CtlType == ODT_BUTTON )
			{
				int indexButton = 0;

					switch( Draw->itemAction )
					{

						case ODA_FOCUS:

							if( Draw->itemState & ODS_FOCUS )
							{
								SetWindowText( hWnd, "ODA_FOCUS:ODS_FOCUS" );
							}
							else
							{
								SetWindowText( hWnd, "ODA_FOCUS" );
							}

							
						break;	 

	

						case ODA_SELECT: 
							if( Draw->itemState & ODS_SELECTED )
							{
								SetWindowText( hWnd, "ODA_SELECT:ODS_SELECTED" );
							}
							else
							{
								SetWindowText( hWnd, "ODA_SELECT" );
							}
						break;

						default: SetWindowText( hWnd, "Def" );
					}


				GetObject( hBitmap[ indexButton ], sizeof(BITMAP), (VOID*)&Bitmap );

			    SelectObject( hDCMem, hBitmap[ indexButton ] ); 
				BitBlt( Draw->hDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, hDCMem, 0, 0, SRCCOPY );

			}

			DeleteDC( hDCMem );
			
		}
	break;

	case WM_CREATE:

		hBitmap[0] = (HBITMAP)LoadImage( NULL, "C:\\checbox\\box_1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
		hBitmap[1] = (HBITMAP)LoadImage( NULL, "C:\\checbox\\box_2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
		hBitmap[2] = (HBITMAP)LoadImage( NULL, "C:\\checbox\\box_3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );

		if( !hBitmap[0] || !hBitmap[1] || !hBitmap[2] ) 
			MessageBox( hWnd, "Error load bitmap from file", NULL, MB_OK | MB_ICONERROR );
			

		ChBox[0] = CreateWindowEx( 0, "Button", "BS_AUTOCHECKBOX", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW /* BS_AUTOCHECKBOX */, 10, 10, 230, 25, hWnd, (HMENU)10, g_hInstance, NULL );
		ChBox[1] = CreateWindowEx( 0, "Button", "BS_AUTO3STATE", WS_CHILD | WS_VISIBLE | BS_AUTO3STATE, 255, 10, 210, 25, hWnd, (HMENU)11, g_hInstance, NULL );
		ChBox[3] = CreateWindowEx( 0, "Button", "BS_CHECKBOX" ,WS_CHILD | WS_VISIBLE | BS_CHECKBOX, 10, 65, 200, 25, hWnd, (HMENU)12, g_hInstance, NULL );


	break;

	case WM_COMMAND:

	switch( LOWORD( wParam ) )
	{
		case 10:

			res = SendMessage( ChBox[0], BM_GETCHECK, 0, 0 );

			if( res == BST_CHECKED )
			{
				SetWindowText( ChBox[0], "Состояние: включен" );
			}

			if( res == BST_UNCHECKED )
			{
				SetWindowText( ChBox[0], "Состояние: выключен" );
			}

		break;

		case 11:

			res = SendMessage( ChBox[1], BM_GETCHECK, 0, 0 );

			if( res == BST_CHECKED )
			{
				SetWindowText( ChBox[1], "Состояние: включен" );
			}
			
			if( res == BST_INDETERMINATE )
			{
				SetWindowText( ChBox[1], "Состояние: неопределено" );
			}

			if( res == BST_UNCHECKED )
			{
				SetWindowText( ChBox[1], "Состояние: выключен" );
			}
		
		break;

		case 12:
			
			res = SendMessage( ChBox[2], BM_GETCHECK, 0, 0 );

			if( res == BST_CHECKED) 
			{
				CheckDlgButton( hWnd, 12, 0 );
			}

			if( res == BST_UNCHECKED) 
			{
				CheckDlgButton( hWnd, 12, 1 );
			}
			
		break;
	}

	break;


	case WM_PAINT:
		hdc = BeginPaint( hWnd, &ps);

		EndPaint( hWnd, &ps);
	break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы