Related
From my understanding, BeginPaint and EndPaint helps paint the screen and validates the rectangle that it paints, this also prevents WM_PAINT messages from being spammed since there will be no more invalid regions in the window.
With my understanding, I thought that theoretically speaking, if I were to use BitBlt() to paint the window in WM_CREATE, then use ValidateRect to Validate the entire client region, the code would work. However, I tried it and it doesn't, suggesting that there could be something possibly wrong with my understanding, or code. Could someone please explain to me where I went wrong?
Here is the code that I used, IDB_BITMAP1 can be replaced with any appropriate Bitmap. Do note the comment in WM_PAINT to reproduce the issue. Thanks for reading!
#include <windows.h>
#include "resource.h"
HBITMAP hBitMap = NULL;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
BITMAP infoBM;
hBitMap = LoadBitmap(GetModuleHandle(NULL), IDB_BITMAP1);
if (hBitMap == NULL)
{
MessageBoxA(NULL, "COULD NOT LOAD CAT BITMAP", "ERROR", MB_OK);
}
HDC winDC = GetDC(hwnd);
HDC hMem = CreateCompatibleDC(winDC);
GetObject(hBitMap, sizeof(infoBM), &infoBM);
HBITMAP hMemOld = SelectObject(hMem, hBitMap);
BitBlt(winDC, 0, 0, infoBM.bmWidth, infoBM.bmHeight, hMem, 0, 0, SRCCOPY);
SelectObject(hMem, hMemOld);
DeleteDC(hMem);
ReleaseDC(hwnd,winDC);
break;
}
case WM_PAINT: //<<<--------- I replaced the entire segment here with ValidateRect(hwnd,NULL);break; And the whole window doesnt show anymore, why is that so?
{
BITMAP infoBM;
PAINTSTRUCT ps;
HDC winDC = BeginPaint(hwnd, &ps);
GetObject(hBitMap, sizeof(infoBM), &infoBM);
HDC hMem = CreateCompatibleDC(winDC);
HBITMAP hOldMem = SelectObject(hMem, hBitMap);
BitBlt(winDC, 0, 0, infoBM.bmWidth, infoBM.bmHeight, hMem, 0, 0, SRCCOPY);
SelectObject(hMem, hOldMem);
DeleteObject(hMem);
EndPaint(hwnd, &ps);
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:return DefWindowProcA(hwnd, msg, wParam, lParam);
}
return 0;
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX),0,WindowProc,0,0,hInstance,NULL,LoadCursor(NULL,IDC_ARROW),(HBRUSH)(COLOR_WINDOW+1),NULL,"WindowClass",LoadIcon(NULL,IDI_APPLICATION)};
if (!RegisterClassEx(&wc))
{
MessageBoxA(NULL, "WINDOW REGISTRATION FAILED", "ERROR", MB_OK);
}
HWND hwnd = CreateWindowEx(WS_EX_LAYERED, "WindowClass", "Title", WS_POPUP, 200, 200, 300, 300, NULL, NULL, hInstance, NULL);
if (!hwnd)
{
MessageBoxA(NULL, "WINDOW CREATION FAILED", "ERROR", MB_OK);
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
if (!SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 255, LWA_ALPHA))
{
MessageBoxA(NULL, "WINDOW LAYERED ATTRIBUTES FAILED", "ERROR", MB_OK);
}
MSG msg;
while (GetMessage(&msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Typically,
You use the GetDC function to carry out drawing that must occur
instantly rather than when a WM_PAINT message is
processing.
And if you'd like, you can ValidateRect the entire client area instead of real painting when a WM_PAINT message is processing.
The following code is adapted from the thread.
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int i = 0;
switch (message)
{
case WM_PAINT:
{
if (i++ < 100)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect;
GetClientRect(hWnd, &rect);
COLORREF clrbak = SetBkColor(hdc, RGB(255, 0, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
int x = (rect.right - rect.left) / 8;
int y = (rect.bottom - rect.top) / 8;
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 255, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 0, 255));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.left += x; rect.right -= x;
rect.top += y; rect.bottom -= y;
SetBkColor(hdc, RGB(0, 0, 0));
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
SetBkColor(hdc, clrbak);
EndPaint(hWnd, &ps);
}
else
{
ValidateRect(hWnd,NULL);
}
}
break;
case WM_ERASEBKGND:
return 1;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
I'm trying to use GL_R8UI to pass unsigned, unnormalized data to a shader, but have found that on at least one GPU it doesn't work.
ie: this works:
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 32, 32, 0, GL_RED, GL_UNSIGNED_BYTE, pData);
but this doesn't:
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, 32, 32, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, pData);
I'm also changing the shader to use sampler2D vs usampler2D and updating the math in the shader appropriately, but finding it works on AMD/Radeon cards but fails on Intel and NVidia cards - the usampler2D always seems to return zero.
I've put together a minimal sample program here. The #define NORMALIZED switches between the two approaches.
So... my main question is: is this simply a driver problem or is there something else wrong in my code that's causing this?
To ask this question another way... what else needs to be changed to switch from GL_R8 normalized data to GL_R8UI unnormalized data, besides:
changing the call to glTextImage2D
changing from sampler2D to usampler2D in the shader
changing the shader to work with uint's instead of floats
changing the math in the shader to deal with normalized vs unnormalized data.
I've now logged an bug report with Intel: https://software.intel.com/en-us/forums/graphics-driver-bug-reporting/topic/748843
Since it was asked for here's the full example program source:
// ShaderTest.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ShaderTest.h"
#include <stdlib.h>
#include <stdint.h>
#include <GL/gl3w.h>
#pragma comment(lib, "opengl32.lib")
#define NORMALIZED
const char* pszVertexShader = R"***(
#version 150
uniform mat4 transform;
attribute vec3 position;
varying vec2 vPosition;
void main()
{
gl_Position = transform * vec4(position,1.);
vPosition = position.xy;
}
)***";
#ifdef NORMALIZED
const char* pszFragmentShader = R"***(
#version 150
uniform sampler2D TextureData;
varying vec2 vPosition;
void main()
{
float red = texelFetch(TextureData, ivec2(0, 0), 0).r;
gl_FragColor = vec4(red , 0, 0, 1);
}
)***";
#else
const char* pszFragmentShader = R"***(
#version 150
uniform usampler2D TextureData;
varying vec2 vPosition;
void main()
{
// Original post had this wrong
// float red = float(texelFetch(TextureData, ivec2(0, 0), 0)).r/255.0;
// Fixed version - still fails on Intel GPUs though
float red = float(texelFetch(TextureData, ivec2(0, 0), 0).r)/255.0;
gl_FragColor = vec4(red, 0, 0, 1);
}
)***";
#endif
int CompileShader(GLenum type, const char* pszSource)
{
int iShader = glCreateShader(type);
glShaderSource(iShader, 1, &pszSource, NULL);
glCompileShader(iShader);
// Dump log
int length;
glGetShaderiv(iShader, GL_INFO_LOG_LENGTH, &length);
if (length != 0)
{
char* pszLog = (char*)_alloca((sizeof(char) + 10) * length);
GLsizei len;
glGetShaderInfoLog(iShader, length, &len, pszLog);
OutputDebugStringA(pszLog);
}
// Check for error
int status;
glGetShaderiv(iShader, GL_COMPILE_STATUS, &status);
if (status == 0)
{
// Clean up after failuer
glDeleteShader(iShader);
return 0;
}
// Success
return iShader;
}
// Globals
HINSTANCE hInst;
HGLRC g_hRC;
GLint g_iVertexShader = 0;
GLint g_iFragmentShader = 0;
GLint g_iShaderProgram = 0;
GLuint g_iTexture = 0;
GLuint g_iVertexBuffer = 0;
#define glCheck() assert(glGetError() == 0)
struct VERTEX
{
float x;
float y;
float z;
};
bool Setup()
{
// Compile shaders
g_iVertexShader = CompileShader(GL_VERTEX_SHADER, pszVertexShader);
g_iFragmentShader = CompileShader(GL_FRAGMENT_SHADER, pszFragmentShader);
// Link program
g_iShaderProgram = glCreateProgram();
glAttachShader(g_iShaderProgram, g_iVertexShader);
glAttachShader(g_iShaderProgram, g_iFragmentShader);
glLinkProgram(g_iShaderProgram);
// Dump log
int length;
glGetProgramiv(g_iShaderProgram, GL_INFO_LOG_LENGTH, &length);
if (length != 0)
{
char* pszLog = (char*)_alloca((sizeof(char) + 10) * length);
GLsizei len;
glGetProgramInfoLog(g_iShaderProgram, length, &len, pszLog);
OutputDebugStringA(pszLog);
}
// Check for error
int status;
glGetProgramiv(g_iShaderProgram, GL_LINK_STATUS, &status);
if (status == 0)
return false;
// Create texture
glGenTextures(1, &g_iTexture);
glBindTexture(GL_TEXTURE_2D, g_iTexture);
uint8_t* pData = (uint8_t*)_alloca(32 * 32 * sizeof(uint8_t));
memset(pData, 128, 32 * 32 * sizeof(uint8_t));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifdef NORMALIZED
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 32, 32, 0, GL_RED, GL_UNSIGNED_BYTE, pData);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, 32, 32, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, pData);
#endif
glBindTexture(GL_TEXTURE_2D, 0);
// Create vertex buffer
glGenBuffers(1, &g_iVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, g_iVertexBuffer);
VERTEX v[] = {
{ 10, 10, 0, },
{ 10, 230, 0, },
{ 310, 10, 0, },
{ 310, 230, 0, },
};
glBufferData(GL_ARRAY_BUFFER, sizeof(v), v, GL_STATIC_DRAW);
// Done
return true;
}
void Cleanup()
{
if (g_iVertexBuffer)
glDeleteBuffers(1, &g_iVertexBuffer);
if (g_iTexture)
glDeleteTextures(1, &g_iTexture);
if (g_iShaderProgram)
glDeleteProgram(g_iShaderProgram);
if (g_iVertexShader)
glDeleteShader(g_iVertexShader);
if (g_iFragmentShader)
glDeleteShader(g_iFragmentShader);
}
void Display(RECT* prc)
{
// Setup viewport
glViewport(0, 0, prc->right, prc->bottom);
// Clear background
glClearColor(0, 0, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Setup program
glUseProgram(g_iShaderProgram);
// Bind vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, g_iVertexBuffer);
// Setup vertex buffer
int aPosition = glGetAttribLocation(g_iShaderProgram, "position");
glEnableVertexAttribArray(aPosition);
glVertexAttribPointer(aPosition, 3, GL_FLOAT, false, sizeof(VERTEX), 0);
// Setup texture
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, g_iTexture);
int uTextureData = glGetUniformLocation(g_iShaderProgram, "TextureData");
glUniform1i(uTextureData, 0);
// Setup ortho projection
float left = 0;
float right = 320;
float top = 0;
float bottom = 240;
float fnear = -1;
float ffar = 1;
float proj[] = {
(float)(2.0 / (right - left)), 0.0f, 0.0f, 0.0f,
0.0f, (float)(2.0 / (top - bottom)), 0.0f, 0.0f,
0.0f, 0.0f, (float)(-2.0 / (ffar - fnear)), 0.0f,
(float)(-(right + left) / (right - left)), (float)(-(top + bottom) / (top - bottom)), (float)(-(ffar + fnear) / (ffar - fnear)), 1.0f
};
int uTransform = glGetUniformLocation(g_iShaderProgram, "transform");
glUniformMatrix4fv(uTransform, 1, false, proj);
// Draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFlush();
}
bool InitOpenGLContext(HWND hWnd)
{
// Setup pixel format
HDC hDC = GetDC(hWnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
int pf = ChoosePixelFormat(hDC, &pfd);
if (pf == 0 || !SetPixelFormat(hDC, pf, &pfd))
return false;
DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
g_hRC = wglCreateContext(hDC);
if (!g_hRC)
return false;
// Init gl3w
wglMakeCurrent(hDC, g_hRC);
int err = gl3wInit();
if (err)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hRC);
g_hRC = NULL;
return false;
}
// Setup
if (!Setup())
{
assert(false);
Cleanup();
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hRC);
g_hRC = NULL;
return false;
}
ReleaseDC(hWnd, hDC);
return true;
}
void CleanupOpenGLContext(HWND hWnd)
{
HDC hDC = GetDC(hWnd);
wglMakeCurrent(hDC, g_hRC);
Cleanup();
wglMakeCurrent(NULL, NULL);
ReleaseDC(hWnd, hDC);
wglDeleteContext(g_hRC);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rc;
GetClientRect(hWnd, &rc);
HDC hdc = BeginPaint(hWnd, &ps);
wglMakeCurrent(hdc, g_hRC);
Display(&rc);
wglMakeCurrent(NULL, NULL);
EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_ERASEBKGND:
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
// Register Class
WNDCLASSEXW 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, MAKEINTRESOURCE(IDI_SHADERTEST));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"ShaderTest";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
RegisterClassExW(&wcex);
// Create Window
HWND hWnd = CreateWindowW(L"ShaderTest", L"ShaderTest", WS_OVERLAPPEDWINDOW,
50, 50, 100, 100, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
return 7;
RECT rc;
rc.left = 0;
rc.top = 0;
rc.right = 320;
rc.bottom = 240;
AdjustWindowRect(&rc, GetWindowLong(hWnd, GWL_STYLE), FALSE);
SetWindowPos(hWnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
if (!InitOpenGLContext(hWnd))
{
DestroyWindow(hWnd);
return 7;
}
// Show window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
CleanupOpenGLContext(hWnd);
DestroyWindow(hWnd);
return (int) msg.wParam;
}
Integer textures need to have nearest filtering (not linear or mipmap):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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 open an image file and put it in the rectangle when a button is clicked? I use winapi and c programming language, please help and thanks in advance
Global Variable :
HDC hdc, hdcMem;
HBITMAP hLogo, hGambar;
OPENFILENAME ofn = {0};
char szNamaFile[MAX_PATH] = {0}
WM_CREATE :
HWND btnLoad;
btnLoad = CreateWindow("Button", "&Load", WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_FLAT, 149, 421, 70, 25, hWnd, (HMENU)IDB_LOAD, GetModuleHandle(NULL), NULL);
if(btnLoad == NULL)
{
MessageBox(NULL, "Gagal Membuat Button!", "Error Uy!", MB_ICONEXCLAMATION);
exit(EXIT_FAILURE);
}
hGambar = (HBITMAP)LoadImage(hInst, szNamaFile, IMAGE_BITMAP, 5, 5, LR_LOADFROMFILE);
SendMessage(btnLoad, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hGambar);
WM_COMMAND:
if(HIWORD(wParam) != BN_CLICKED)
break;
switch(LOWORD(wParam))
{
case IDB_LOAD:
BukaFormDialog(hWnd);
break;
}
break;
WM_PAINT:
PAINTSTRUCT ps ;
InvalidateRect (hWnd, NULL, TRUE);
RECT rctWindow;
GetClientRect(hWnd, &rctWindow);
RECT rctPct;
rctPct.left = 3;
rctPct.top = rctKiri.top + 3;
rctPct.right = rctKiri.right - 3;
rctPct.bottom = rctKiri.bottom- 75;
hdc = BeginPaint(hWnd, &ps);
/*------------Picture Box--------------*/
Rectangle(hdc, rctPct.left, rctPct.top, rctPct.right, rctPct.bottom);
/*-------------------------------------*/
EndPaint(hWnd, &ps);
break;
Procedure :
char BukaFormDialog(HWND hWnd)
{
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "File JPG (*.JPG)\0*.JPG\0File BMP (*.BMP)\0*.BMP\0File PNG(*.PNG)\0*.PNG\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szNamaFile;
ofn.lpstrTitle = "Pilih Gambar ...";
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
hMem = CreateCompatibleDC(hdc);
SelectObject(hMem, hGambar);
BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
DeleteDC(hMem);
DeleteObject(hGambar);
}
else
MessageBox(hWnd, "Gagal Membuka File", "Error", MB_ICONERROR | MB_OK);
return (0);
}
you have four problems here
1st) LoadImage(..,IMAGE_BITMAP,...) does only support BMP files.
2nd) you are deleting the wrong Handle (hGambar)
if(GetOpenFileName(&ofn))
{
hMem = CreateCompatibleDC(hdc);
hGambar=SelectObject(hMem, hGambar); //save the original hBmp
BitBlt(hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
hGambar=SelectObject(hMem, hGambar); // Select Back the original hBmp
DeleteDC(hMem);
DeleteObject(hGambar); // now hGambar is back delete it
}
3rd) your above drawing is not persistent, it will be erased by any overlapping window. move it to WM_PAINT.
4th) Do not call InvalidateRect or InvalidateRgn in WM_PAINT.
here is a full working code
#include <windows.h>
HDC hdc, hdcMem;
HBITMAP hLogo, hGambar=NULL;
#ifndef IDB_LOAD
#define IDB_LOAD 1000
#endif
#define APP_CLASS TEXT("APP_CLASS")
char szNamaFile[MAX_PATH];
/*____________________________________________________________________________________________________________
*/
int BukaFormDialog(HWND hWnd)
{
OPENFILENAME ofn={sizeof(ofn)};
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = "File BMP (*.BMP)\0\0";
ofn.lpstrFile = szNamaFile;
ofn.lpstrTitle = "Pilih Gambar ...";
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_FILEMUSTEXIST;
if(GetOpenFileName(&ofn))
{
HBITMAP hTmp;
DeleteObject(hGambar);
hGambar=LoadImage(NULL,szNamaFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hGambar){
InvalidateRect(hWnd,NULL,1);
return 1;
}
MessageBox(hWnd, "Bad Image", "Error", MB_ICONERROR | MB_OK);
}
else{
//User pressed Cancel*/;
}
return (0);
}
/*____________________________________________________________________________________________________________
*/
int WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
switch(uMsg){
case WM_CREATE:
CreateWindow(
TEXT("Button"),
TEXT("&Load"),
WS_CHILD | WS_TABSTOP | WS_VISIBLE | BS_FLAT,
149, 421, 70, 25,
hWnd, (HMENU)IDB_LOAD, NULL, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_LOAD:
BukaFormDialog(hWnd);
break;
}
break;
case WM_PAINT:{
PAINTSTRUCT ps;
HDC hMem;
BeginPaint(hWnd,&ps);
if(hGambar){
if(hMem=CreateCompatibleDC(ps.hdc)){
hGambar=SelectObject(hMem,hGambar); /*select & swap*/
BitBlt(ps.hdc, 4, 4, 50,50, hMem, 0, 0, SRCCOPY);
hGambar=SelectObject(hMem,hGambar);/*select & swap back*/
DeleteDC(hMem);
}
}
EndPaint(hWnd,&ps);
}
break;
case WM_DESTROY:
DeleteObject(hGambar);
break;
default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
/*____________________________________________________________________________________________________________
*/
int WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int nShow){
WNDCLASS wc={
0,
WinProc,
0,
0,
hInstance,
NULL,
LoadCursor(NULL,IDC_ARROW),
(HBRUSH) (COLOR_WINDOW+1),
NULL,
APP_CLASS};
if(RegisterClass(&wc)){
if(CreateWindow(APP_CLASS,TEXT("Title"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,NULL,NULL)){
MSG msg;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return 0;
}
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