(GDI) AlphaBlend - c

This question is follow up Win32 (GDI) - Set Opacity of STATIC Control
My problem is that my alpha blending function is not working and I was wondering if someone help me out?
Here is the Setup WndProc:
case WM_PAINT:
OnChildPaint(BeginPaint(hWnd, &ps));
EndPaint(hWnd, &ps);
break;
My OnChildPaint function:
BOOL
OnChildPaint(HDC i_hDC)
{
HDC hMemDC;
HGDIOBJ hOldObj;
HBITMAP hBitmap;
BLENDFUNCTION blendFunc_s = {AC_SRC_OVER, 0, 128, AC_SRC_ALPHA};
hMemDC = CreateCompatibleDC(i_hDC);
hBitmap = CreateCompatibleBitmap(hMemDC, 100, 100);
/* hBrush = CreateSolidBrush(RGB(255, 0, 0)); */
hOldObj = SelectObject(hMemDC, hBitmap);
BitBlt(i_hDC, 10, 100, 100, 100, hMemDC, 0, 0, SRCCOPY);
AlphaBlend(i_hDC, 10, 100, 100, 100, hMemDC, 10, 100, 100, 100, blendFunc_s);
SelectObject(hMemDC, hOldObj);
DeleteObject(hMemDC);
return TRUE;
}
Have that in mind that I'm not working with actual Bitmap Image (There is no Image).
Thanks

Related

I allways get the Error that the Not initialised variable hdc get used

i´m trying to programm a traffic light, but i allways get the error 4700, idk what to do with HDC hdc;
#include <windows.h>
#include <stdio.h>
[...]
the traffic light should run 4 times and than stop working. so i put the colors which should be changed in a for loop
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HERE IS HDC hdc, i tried to google it but didnt get really an answer which would help me out to fix it
HDC hdc;
PAINTSTRUCT ps;
HBRUSH gruen, gelb, rot, weiss;
int i;
//Punkte
POINT PointsAmpel[4] = {50, 50, 250, 50, 250, 550, 50, 550};
Colors
gelb = CreateSolidBrush(RGB(255, 255, 0));
rot = CreateSolidBrush(RGB(255, 0, 0));
gruen = CreateSolidBrush(RGB(0, 100, 0));
weiss = CreateSolidBrush(RGB(0, 0, 0));
switch (message)
{
case WM_PAINT:
//Form
Polygon(hdc, PointsAmpel, 4);
for (i = 0; i < 4; i++) {
//rot ON
hdc = BeginPaint(hWnd, &ps);
Sleep(1000);
SelectObject(hdc, rot);
Ellipse(hdc, 100, 100, 200, 200);
InvalidateRect(hWnd, NULL, TRUE);
//+gelb ON
hdc = BeginPaint(hWnd, &ps);
Sleep(1000);
SelectObject(hdc, gelb);
Ellipse(hdc, 100, 250, 200, 350);
InvalidateRect(hWnd, NULL, TRUE);
//rot+gelb OFF grün ON
hdc = BeginPaint(hWnd, &ps);
Sleep(1000);
SelectObject(hdc, weiss);
Ellipse(hdc, 100, 250, 200, 350);
Ellipse(hdc, 100, 100, 200, 200);
SelectObject(hdc, gruen);
Ellipse(hdc, 100, 400, 200, 500);
InvalidateRect(hWnd, NULL, TRUE);
//grün OFF gelb ON
hdc = BeginPaint(hWnd, &ps);
Sleep(1000);
SelectObject(hdc, weiss);
Ellipse(hdc, 100, 400, 200, 500);
SelectObject(hdc, gelb);
Ellipse(hdc, 100, 250, 200, 350);
InvalidateRect(hWnd, NULL, TRUE);
//gelb OFF rot ON
hdc = BeginPaint(hWnd, &ps);
Sleep(1000);
SelectObject(hdc, weiss);
Ellipse(hdc, 100, 250, 200, 350);
SelectObject(hdc, rot);
Ellipse(hdc, 100, 100, 200, 200);
InvalidateRect(hWnd, NULL, TRUE);
EndPaint(hWnd, &ps);
}
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
DeleteObject(rot);
DeleteObject(gelb);
DeleteObject(gruen);
DeleteObject(weiss);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

Why am I getting a spurious white pixel when I render a texture in SDL?

I'm using SDL2 to draw some stuff on screen. Here is some code:
#include <SDL.h>
void main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Texture *texture;
{
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
Uint32 pixel_format = window_surface->format->format;
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, 48, 48, 8, pixel_format);
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
SDL_FreeSurface(window_surface);
}
SDL_Rect target_rectangle;
target_rectangle.x = 100;
target_rectangle.y = 100;
target_rectangle.w = 48;
target_rectangle.h = 48;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawLine(renderer, 0, 50, 50, 0);
SDL_RenderCopy(renderer, texture, 0, &target_rectangle);
SDL_RenderPresent(renderer);
SDL_Event event;
while(1)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
return;
break;
}
}
SDL_Delay(10);
}
}
The code creates some dummy blank texture texture. I get the same behavior with a texture loaded from a bitmap file. These lines are the important ones:
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDrawLine(renderer, 0, 50, 50, 0);
SDL_RenderCopy(renderer, texture, 0, &target_rectangle);
SDL_RenderPresent(renderer);
So draw a line somewhere in white pixels, and then copy the dummy texture to the screen, and display it all. When I do this, I get a white pixel at the bottom right of the texture.
As I said, I also get this when the texture comes from a bitmap using code like this:
SDL_Surface *bitmap_surface = SDL_LoadBMP("test.bmp");
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bitmap_surface);
SDL_FreeSurface(bitmap_surface);

Inserting text item into Listview without an associated icon

I have a simple list view which has an image list added to it. Displaying image items is fine, but when I just want one piece of text, no icon or image, one pure text item into my list view, the thing drops an image on me. I can't add any item without it having an icon/image next to it.
Here's the code
case WM_CREATE:{
HBITMAP hbmp_o, hbmp;
COLORREF crMask;
// listview view is LVS_ICON by default
hWndListView = CreateWindowEx(0, WC_LISTVIEW, TEXT("ListView1"), WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL, 0, 0, 400, 500, hwnd, NULL, GetModuleHandle(NULL), NULL);
hbmp_o = (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP3), IMAGE_BITMAP, 0, 0, 0);
BITMAP bm_o = { 0 };
GetObject(hbmp_o, sizeof(bm_o), &bm_o);
HDC dc_o = CreateCompatibleDC(NULL), dc = CreateCompatibleDC(NULL);
HBITMAP old_o = (HBITMAP)SelectObject(dc_o, hbmp_o);
unsigned char *Data;
BITMAPINFO BitmapInfo;
memset(&BitmapInfo.bmiHeader, 0, sizeof(BitmapInfo.bmiHeader));
BitmapInfo.bmiHeader.biWidth = lv_bmp_width;
BitmapInfo.bmiHeader.biHeight = lv_bmp_height;
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
hbmp = CreateDIBSection(dc_o, &BitmapInfo, DIB_RGB_COLORS, (void **)&Data, 0, 0);
HBITMAP old = (HBITMAP)SelectObject(dc, hbmp);
SetStretchBltMode(dc, HALFTONE);
StretchBlt(dc, 0, 0, lv_bmp_width, lv_bmp_height, dc_o, 0, 0, bm_o.bmWidth, bm_o.bmHeight, SRCCOPY);
SelectObject(dc_o, old_o);
hbmp = (HBITMAP)SelectObject(dc, old);
BITMAP bm = { 0 };
GetObject(hbmp, sizeof(bm), &bm);
hImageList_small = ImageList_Create(lv_bmp_width, lv_bmp_height, ILC_COLOR32 | ILC_MASK, 3, 0);
ImageList_Add(hImageList_small, hbmp, (HBITMAP)NULL);
ListView_SetImageList(hWndListView, hImageList_small, LVSIL_NORMAL);
LVITEM lv;
lv = { LVIF_IMAGE, itm_count, 0, 0, 0, NULL, 0, img_count, 0, 0, 0, 0, 0 };
ListView_InsertItem(hWndListView, &lv); img_count++; itm_count++;
hwndNextViewer = SetClipboardViewer(hwnd);
break;
}
/*********************************************************************************************/
case WM_DRAWCLIPBOARD:{
static UINT auPriorityList[] = {
CF_OWNERDISPLAY,
CF_TEXT,
CF_OEMTEXT,
CF_UNICODETEXT,
CF_ENHMETAFILE,
CF_BITMAP,
CF_DIB
};
if (!OpenClipboard(hwnd)) lasterror(L"Clipboard not opening");
UINT uformat = GetPriorityClipboardFormat(auPriorityList, 7);
HGLOBAL hnd;
LPWSTR wstr = NULL;
LPWSTR str = NULL;
switch (uformat)
{
case CF_OEMTEXT:
case CF_TEXT:
case CF_UNICODETEXT:{
if((hnd = GetClipboardData(CF_UNICODETEXT)) == NULL) lasterror(L"hbmp_o is NULL");
wstr = (LPWSTR)GlobalLock(hnd);
str = (LPWSTR)malloc(wcslen(wstr) * sizeof(WCHAR) + sizeof(WCHAR));
wcscpy(str, wstr);
GlobalUnlock(hnd);//GlobalFree(hnd); fuck global free. makes crash
CloseClipboard();
LVITEM lv;
lv = { LVIF_TEXT, itm_count++, 0, 0, 0, str, 0, 0, 0, 0, 0, 0, 0 };
int a = ListView_InsertItem(hWndListView, &lv); itm_count++;
free(str);
break;
}
//etc..
Aside from dropping the image list and custom drawing the bitmap into it, I have no solutions to this.

how to use plgblt

I need to rotate picture by pressing arrows.
rotate(dots, 90, 150, 120, size.cx, size.cy);
PlgBlt(dc, dots, my_dc, 0, 0, size.cx, size.cy, my_mask, 0, 0);
There is a function - rotate.
But I don't know where to put this part. When I put it into WM_TIMER - happens nothing. In WM_PAINT - too. What's wrong?
Where put this code and where call this?
Here is WM_PAINT and WM_TIMER
case WM_PAINT:
dc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
BitBlt(mdc, 0, 0, info.bmWidth, info.bmHeight, bdc, 0, 0, SRCCOPY);
MaskBlt(mdc, my_pos.x, my_pos.y, my_size.cx, my_size.cy, my_dc, 0, 0, my_mask, 0, 0, MAKEROP4(SRCCOPY, SRCPAINT));
BitBlt(dc, 0, 0, rect.right, rect.bottom, mdc, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
break;
case WM_TIMER:
if(LOWORD(wParam) == 777u) {
if(GetAsyncKeyState(VK_LEFT))
{
if (GreatFuncLeftRight(my_pos.x,my_pos.y))
{
my_pos.x -= 2;
}
else my_pos.x-=0;
}
if(GetAsyncKeyState(VK_RIGHT))
{
if (GreatFuncLeftRightReverse(my_pos.x,my_pos.y))
{
my_pos.x += 2;
}
else my_pos.x+=0;
}
if(GetAsyncKeyState(VK_UP))
{
if (GreatFuncUpDown(my_pos.x,my_pos.y))
{
my_pos.y -= 2;
}
else my_pos.y-=0;// вверх
}
if(GetAsyncKeyState(VK_DOWN))
{
if (GreatFuncUpDownReverse(my_pos.x,my_pos.y))
{
my_pos.y += 2;
}
else my_pos.y+=0;
}
InvalidateRect(hwnd, NULL, FALSE);
}
break;

ListView scrollbar flickering

I am coding my first program with GUI using only pure WinAPI. But I faced some strange problem.
There are 2 ListView elements on the form to output some values. When scrollbar appears on second ListView, all child objects on the form disappears. And scrollbar of that ListView is flickering. And when I push on that ListView all get back to normal. I don't know what to do.
But this problem applies only to the second ListView element, hListViewCh on the form. There is all ok with the first element.
Here is the code:
case WM_CREATE:
{
GetClientRect(hWnd, &Rect);
h_Chk1 = CreateWindow(TEXT("button"), TEXT("Graphic"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
Rect.right - 550,
300 + 10,
100,
20,
hWnd, (HMENU)0xCB01, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
h_Chk2 = CreateWindow(TEXT("button"), TEXT("Diagram"),
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
Rect.right - 550,
300 + 35,
100,
20,
hWnd, (HMENU)0xCB02, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
CheckDlgButton(hWnd, 0xCB01, BST_UNCHECKED);
CheckDlgButton(hWnd, 0xCB02, BST_UNCHECKED);
hListViewCh = CreateWindow(
WC_LISTVIEW,
_T("MyList"),
LVS_REPORT|WS_CHILD|WS_VISIBLE,
Rect.right - 265,
377,
250,
200,
hWnd, (HMENU)listViewCh, (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
//--
LVCOLUMN lvColumn = {0};
lvColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT;
lvColumn.fmt = LVCFMT_CENTER;
lvColumn.pszText = "№";
lvColumn.cx = 30;
ListView_InsertColumn(hListViewCh, 0, &lvColumn);
lvColumn.pszText = "Property";
lvColumn.cx = 70;
ListView_InsertColumn(hListViewCh, 1, &lvColumn);
lvColumn.pszText = "Value";
lvColumn.cx = 120;
ListView_InsertColumn(hListViewCh, 2, &lvColumn);
//--
hListView = CreateWindow(
WC_LISTVIEW,
_T("Set of variate values"),
LVS_REPORT|WS_CHILD|WS_VISIBLE,
Rect.right - 550,
Rect.top + 15,
535,
275,
hWnd,
(HMENU)listView,
(HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
NULL);
//--
lvColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT;
lvColumn.pszText = "No";
lvColumn.fmt = LVCFMT_CENTER;
lvColumn.cx = 43;
ListView_InsertColumn(hListView, 0, &lvColumn);
//--
lvColumn.pszText = "[ a[i-1] , a[i] )";
lvColumn.cx = 151;
ListView_InsertColumn(hListView, 1, &lvColumn);
//--
lvColumn.pszText = "xi";
lvColumn.cx = 85;
ListView_InsertColumn(hListView, 2, &lvColumn);
//--
lvColumn.pszText = "ni";
ListView_InsertColumn(hListView, 3, &lvColumn);
//--
lvColumn.pszText = "V";
ListView_InsertColumn(hListView, 4, &lvColumn);
//--
lvColumn.pszText = "EV";
ListView_InsertColumn(hListView, 5, &lvColumn);
break;
}
There is some redraw functions in WM_PAINT to make ListViews move with a window on maximizing.
//-- LISTVIEW POSITION
SetWindowPos(hListViewCh, NULL,\
Rect.right - 265,
312,
250,
200,
SWP_NOSIZE);
//-- LISTVIEW POSITION
SetWindowPos(hListView, NULL,\
Rect.right - 550,\
Rect.top + 15,\
535,\
340,\
SWP_NOSIZE|SWP_NOZORDER);
//--
Resizing child windows in a WM_PAINT is simply wrong and I suspect this to be a big part of your problems.
The code to position the child windows should be run in response to the WM_WINDOWPOSCHANGED message.

Resources