My problem is related to GtkLayout, which, I have read is responsible for creating infinite scrollable areas with widgets. I've created one using the following code:
GtkWidget *window;
GtkWidget *button1;
GtkWidget *button2;
GtkWidget *layout;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window wowowwow");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
layout = gtk_layout_new(NULL, NULL);
gtk_container_add (GTK_CONTAINER (window), layout);
button1 = gtk_button_new_with_label ("Hello World");
g_signal_connect (button1, "clicked", G_CALLBACK (print_hello), NULL);
g_signal_connect_swapped (button1, "clicked", G_CALLBACK (gtk_widget_destroy), window);
gtk_layout_put(GTK_LAYOUT(layout), button1, 20,20);
button2 = gtk_button_new_with_label ("Button 2");
g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_widget_destroy), button2);
gtk_layout_put(GTK_LAYOUT(layout), button2, 200,200);
GtkAdjustment *hadjustment = gtk_adjustment_new(0, 0, 1000, 1, 1, 20);
gtk_layout_set_size(GTK_LAYOUT(layout), 1000,1000);
gtk_scrollable_set_hadjustment(GTK_SCROLLABLE(layout), hadjustment);
gtk_widget_show_all (window);
This shows a window that looks like following:
Window initial
.. and when resized:
Window resized
The problem is, I want the contents of GtkLayout to be scrollable. I think the problem lies within the adjustment object, that I have to configure, but I don't know what exactly the parameters mean and the documentation is very scarce about this. So, I'm looking here forward for help.
To make it work I had to put the GtkLayout inside a GtkScrolledWindow and override the default dimensions of the layout. The code that works looks like following:
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *scrolled_window;
GtkWidget *layout;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add (GTK_CONTAINER (window), scrolled_window);
layout = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrolled_window), layout);
int i = 0;
while (i < 10) {
GtkWidget *a_button;
a_button = gtk_button_new_with_label("Button");
gtk_layout_put(GTK_LAYOUT(layout), a_button, 10, 10 + 40 * i);
i += 1;
}
gtk_layout_set_size(GTK_LAYOUT(layout), 1000,1000);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
I created a semi transparent window using the source code from this discussion (posted by wilkie):
How to make an OpenGL rendering context with transparent background?
It works, but I would like my window not to pick up mouse, touch and keyboard click messages.
But above all, I would like these messages (click, touch, keyboard) to be received by the application located below my transparent window.
Example:
My transparent window (TOP_MOST) is located on top of NotePad ++. If I click on my transparent window, I want this click to be received by NotePad++ and not by my transparent window.
I did not find valid answers on the Internet.
I see two possibilities:
_ Make my window transparent to all messages
_ Or my transparent window must receive the messages, not react and redirect them by sending them to the window located below.
In both cases, I don't know how to do it :)
Is it possible ?
Regards,
Edit with code:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <windowsx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <dwmapi.h>
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "dwmapi.lib")
#include <assert.h>
#include <tchar.h>
#ifdef assert
#define verify(expr) if(!expr) assert(0)
#else verify(expr) expr
#endif
const TCHAR szAppName[] = _T("TransparentGL");
const TCHAR wcWndName[] = _T("TransparentGL");
HDC hDC;
HGLRC m_hrc;
int w = 240;
int h = 240;
BOOL initSC() {
glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
return 0;
}
void resizeSC(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
BOOL renderSC() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top
glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
glEnd();
glPopMatrix();
glFlush();
return 0;
}
BOOL CreateHGLRC(HWND hWnd) {
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_SUPPORT_COMPOSITION | // Format Must Support Composition
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // An Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
24, // 16Bit Z-Buffer (Depth Buffer)
8, // Some Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
HDC hdc = GetDC(hWnd);
int PixelFormat = ChoosePixelFormat(hdc, &pfd);
if (PixelFormat == 0) {
assert(0);
return FALSE;
}
BOOL bResult = SetPixelFormat(hdc, PixelFormat, &pfd);
if (bResult == FALSE) {
assert(0);
return FALSE;
}
m_hrc = wglCreateContext(hdc);
if (!m_hrc) {
assert(0);
return FALSE;
}
ReleaseDC(hWnd, hdc);
return TRUE;
}
LRESULT CALLBACK WindowFunc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
break;
case WM_DESTROY:
if (m_hrc) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_hrc);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR str, int nWinMode) {
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowFunc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hThisInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)CreateSolidBrush(0x00000000);
wc.lpszClassName = szAppName;
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("RegisterClassEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
return FALSE;
}
HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT, szAppName, wcWndName,
WS_VISIBLE | WS_POPUP, 200, 150, w, h,
NULL, NULL, hThisInst, NULL);
if (!hWnd) {
MessageBox(NULL, _T("CreateWindowEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
return FALSE;
}
DWM_BLURBEHIND bb = { 0 };
HRGN hRgn = CreateRectRgn(0, 0, -1, -1);
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
bb.hRgnBlur = hRgn;
bb.fEnable = TRUE;
DwmEnableBlurBehindWindow(hWnd, &bb);
CreateHGLRC(hWnd);
HDC hdc = GetDC(hWnd);
wglMakeCurrent(hdc, m_hrc);
initSC();
resizeSC(w, h);
ReleaseDC(hWnd, hdc);
MSG msg;
while (1) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
HDC hdc = GetDC(hWnd);
wglMakeCurrent(hdc, m_hrc);
renderSC();
SwapBuffers(hdc);
ReleaseDC(hWnd, hdc);
}
}
return (FALSE);
}
You can add the WS_EX_LAYERED style when creating the form, and set the opacity of the window through the SetLayeredWindowAttributes function.
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_APPWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT, szAppName, wcWndName,
WS_VISIBLE | WS_POPUP, 200, 150, w, h,
NULL, NULL, hThisInst, NULL);
if (!hWnd) {
MessageBox(NULL, _T("CreateWindowEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
return FALSE;
}
SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
Then it works for me:
Thanks to Zhu Song - MSFT and David Heffernan, this is the full source code of the modified sample originaly posted in this post:
How to make an OpenGL rendering context with transparent background?
It display an OpenGL Triangle in transparente Window and support Click-Through:
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <windowsx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <dwmapi.h>
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "dwmapi.lib")
#include <assert.h>
#include <tchar.h>
#ifdef assert
#define verify(expr) if(!expr) assert(0)
#else verify(expr) expr
#endif
const TCHAR szAppName[] = _T("TransparentGL");
const TCHAR wcWndName[] = _T("TransparentGL");
HDC hDC;
HGLRC m_hrc;
int w = 240;
int h = 240;
BOOL initSC() {
glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
return 0;
}
void resizeSC(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
BOOL renderSC() {
static float angle = 0.0f;
angle = (angle < 360.0f ) ? angle + 0.01f : 0.0f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor4f(1.0f, 0.0f, 0.0f,0.5f); // Set The Color To Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top
glColor4f(0.0f, 1.0f, 0.0f, 0.5f); // Set The Color To Green
glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
glColor4f(0.0f, 0.0f, 1.0f, 0.5f); // Set The Color To Blue
glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
glEnd();
glPopMatrix();
glFlush();
return 0;
}
BOOL CreateHGLRC(HWND hWnd) {
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_SUPPORT_COMPOSITION | // Format Must Support Composition
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // An Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
24, // 16Bit Z-Buffer (Depth Buffer)
8, // Some Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
HDC hdc = GetDC(hWnd);
int PixelFormat = ChoosePixelFormat(hdc, &pfd);
if (PixelFormat == 0) {
assert(0);
return FALSE;
}
BOOL bResult = SetPixelFormat(hdc, PixelFormat, &pfd);
if (bResult == FALSE) {
assert(0);
return FALSE;
}
m_hrc = wglCreateContext(hdc);
if (!m_hrc) {
assert(0);
return FALSE;
}
ReleaseDC(hWnd, hdc);
return TRUE;
}
LRESULT CALLBACK WindowFunc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
switch (msg) {
case WM_CREATE:
break;
case WM_DESTROY:
if (m_hrc) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_hrc);
}
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR str, int nWinMode) {
WNDCLASSEX wc;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowFunc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hThisInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)CreateSolidBrush(0x00000000);
wc.lpszClassName = szAppName;
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("RegisterClassEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
return FALSE;
}
HWND hWnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_APPWINDOW | WS_EX_TOPMOST | WS_EX_TRANSPARENT, szAppName, wcWndName,
WS_VISIBLE | WS_POPUP, 200, 150, w, h,
NULL, NULL, hThisInst, NULL);
if (!hWnd) {
MessageBox(NULL, _T("CreateWindowEx - failed"), _T("Error"), MB_OK | MB_ICONERROR);
return FALSE;
}
SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
DWM_BLURBEHIND bb = { 0 };
HRGN hRgn = CreateRectRgn(0, 0, -1, -1);
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
bb.hRgnBlur = hRgn;
bb.fEnable = TRUE;
DwmEnableBlurBehindWindow(hWnd, &bb);
CreateHGLRC(hWnd);
HDC hdc = GetDC(hWnd);
wglMakeCurrent(hdc, m_hrc);
initSC();
resizeSC(w, h);
ReleaseDC(hWnd, hdc);
MSG msg;
while (1) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
HDC hdc = GetDC(hWnd);
wglMakeCurrent(hdc, m_hrc);
renderSC();
SwapBuffers(hdc);
ReleaseDC(hWnd, hdc);
}
}
return (FALSE);
}
If it can help other peoples, if you want to do the same thing with QT:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_glWidget = new MyOpenGLWidget(this); // derived from: public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
setAttribute(Qt::WA_InputMethodTransparent);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_TransparentForMouseEvents);
SetForegroundWindow((HWND)winId());
setWindowFlags(Qt::Window | Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
ui->verticalLayout->addWidget(_glWidget);
}
So I was trying to set a ComboBox's background & text color using Common-Controls & WINAPI.
I did manage to set the background & text color of the combo box itself, but the colors of its dropdown list remain the same.
Here is pretty much what I did:
When creating the combo box, I used the CBS_DROPDOWNLIST style (along with WS_VISIBLE & WS_CHILD).
Then in the window handler function, I handled the CTLCOLOR_LISTBOX messge in the following way:
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 255));
SetBkColor(dc, 0x383838);
comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
As I said, this only colors the combobox itself, and not its drop-down list.
How can I can color the drop down list as well?
For the background color and text color of the combo box, you can handle the WM_CTLCOLORLISTBOX and WM_CTLCOLOREDIT messages respectively.
WM_CTLCOLORLISTBOX : Sent to the parent window of a list box before
the system draws the list box. By responding to this message, the
parent window can set the text and background colors of the list box
by using the specified display device context handle.
Some code:
// Create Combox control
int xpos = 100; // Horizontal position of the window.
int ypos = 100; // Vertical position of the window.
int nwidth = 200; // Width of the window
int nheight = 200; // Height of the window
HWND hwndParent = hWnd; // Handle to the parent window
hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""),
CBS_DROPDOWNLIST | WS_CHILD | WS_VISIBLE,
xpos, ypos, nwidth, nheight, hwndParent, NULL, hInstance,
NULL);
// load the combobox with item list.
// Send a CB_ADDSTRING message to load each item
TCHAR Planets[9][10] =
{
TEXT("Mercury"), TEXT("Venus"), TEXT("Terra"), TEXT("Mars"),
TEXT("Jupiter"), TEXT("Saturn"), TEXT("Uranus"), TEXT("Neptune"),
TEXT("Pluto??")
};
TCHAR A[16];
int k = 0;
memset(&A, 0, sizeof(A));
for (k = 0; k <= 8; k += 1)
{
wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]);
// Add string to combobox.
SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A);
}
// Send the CB_SETCURSEL message to display an initial item
// in the selection field
SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)2, (LPARAM)0);
...
Updated:
//Windows Process
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CTLCOLORLISTBOX:
{
COMBOBOXINFO info;
info.cbSize = sizeof(info);
SendMessage(hWndComboBox, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info);
COMBOBOXINFO info1;
info1.cbSize = sizeof(info1);
SendMessage(hWndComboBox1, CB_GETCOMBOBOXINFO, 0, (LPARAM)&info1);
if ((HWND)lParam == info.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
if ((HWND)lParam == info1.hwndList)
{
HDC dc = (HDC)wParam;
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 0));
SetBkColor(dc, RGB(0, 0, 255));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 0, 255));
return (LRESULT)comboBrush;
}
}
case WM_CTLCOLOREDIT:
{
HWND hWnd = (HWND)lParam;
HDC dc = (HDC)wParam;
if (hWnd == hWndComboBox)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 0, 255));
SetBkColor(dc, 0x383838); //0x383838
HBRUSH comboBrush = CreateSolidBrush(0x383838); //global var
return (LRESULT)comboBrush;
}
else if (hWnd == hWndComboBox1)
{
SetBkMode(dc, OPAQUE);
SetTextColor(dc, RGB(255, 255, 0));
SetBkColor(dc, RGB(0, 255, 0));
HBRUSH comboBrush = CreateSolidBrush(RGB(0, 255, 0));
return (LRESULT)comboBrush;
}
}
...
Change the background color by comparing the edit handle and listbox handle returned by the window.
Debug:
I need to create Rounded Rectangle Buttons in MFC. I tried several resources but did not found the correct way of explanation. Even in **Code Project ** I founded circular or elliptical buttons.
Please suggest how we can create Rounded Rectangle Buttons or any other article
My answers are...
1. Use Skin Library.
I usually use Codejock SkinFramework.
That's ver easy. Include XTSkinFrameworkPro.h in your stdafx.h then load skin file before your dialog is invoked.
XTPSkinManager()->LoadSkin(_T("..."));
2-1. Draw by yourself.
Most simple one is here. Read it first.
https://vcpptips.wordpress.com/tag/owner-draw-button-control/
Then use this code for making round button. It would be nicer if you slide the label text 1px to right-bottom when they hit the button.
http://www.codeproject.com/Articles/11683/CRoundButton-A-fancy-graphical-button
2-2. Draw by yourself. (Use bitmap)
The other one is using bitmap button. Make a bitmap image of rounded button then set it to your button.
how to add bitmap image to buttons in MFC?
Exsample:
Save below as a SimpleBitmapButton.h and include it in your project.
#pragma once
#include <afxwin.h>
class CSimpleBitmapButton : public CButton
{
DECLARE_DYNAMIC(CSimpleBitmapButton)
protected:
enum EButtonState
{
NORMAL = 0,
PUSHED = 1
};
public:
CSimpleBitmapButton();
BOOL Open( int resource_id );
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
protected:
int Width, Height;
BOOL Pushed;
CBitmap Bitmap;
};
Save below as a SimpleBitmapButton.cpp and include it in your project.
#include "stdafx.h"
#include "SimpleBitmapButton.h"
const int BUTTON_IMAGE_NUM = 2;
IMPLEMENT_DYNAMIC(CSimpleBitmapButton, CButton)
BEGIN_MESSAGE_MAP(CSimpleBitmapButton, CButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_CREATE()
END_MESSAGE_MAP()
CSimpleBitmapButton :: CSimpleBitmapButton()
{
Pushed = FALSE;
Width = 0;
Height = 0;
}
void CSimpleBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC memDC;
memDC.CreateCompatibleDC( NULL );
CBitmap *oldBitmap = memDC.SelectObject( &Bitmap );
if( Pushed == FALSE )
BitBlt( lpDrawItemStruct->hDC, 0, 0, Width, Height, memDC, 0, 0, SRCCOPY );
else
BitBlt( lpDrawItemStruct->hDC, 0, 0, Width, Height, memDC, Width , 0, SRCCOPY );
memDC.SelectObject( oldBitmap );
}
BOOL CSimpleBitmapButton :: Open( int resource_id )
{
Pushed = FALSE;
Bitmap.LoadBitmap( resource_id );
//adjust the button size
BITMAP bm;
Bitmap.GetObject(sizeof(BITMAP),&bm);
Width = bm.bmWidth / BUTTON_IMAGE_NUM;
Height = bm.bmHeight;
RECT rect;
GetWindowRect( &rect );
GetParent()->ScreenToClient( &rect );
rect.right = rect.left + Width;
rect.bottom = rect.top + Height;
MoveWindow( &rect );
return TRUE;
}
void CSimpleBitmapButton::OnLButtonDown(UINT nFlags, CPoint point)
{
Pushed = TRUE;
Invalidate( FALSE );
CButton::OnLButtonDown(nFlags, point);
}
void CSimpleBitmapButton::OnLButtonUp(UINT nFlags, CPoint point)
{
Pushed = FALSE;
Invalidate( FALSE );
CButton::OnLButtonUp(nFlags, point);
}
Import this bitmap to resource.
Then set IDB_ROUND_BUTTON for resource ID.
Add button on your dialog and set the "Owner Darw" proerty to True. Important!
Add member variables of the button as m_PlayButton.
At the dialog header, include SimpleBitmapButton.h and change the class of m_PlayButton from CButton to CSimpleBitmapButton.
CSimpleBitmapButton m_Button; // it was CButton m_Button;
At the last, set the bitmap on OnInitDialog()
m_PlayButton.Open( IDB_ROUND_BUTTON );
I'm trying to create a borderless window that fills the screen with an OpenGL viewport. The problem is, when I set the window and viewport to be the same size as the desktop, the window flashes black on losing and gaining focus, on exit, and on creation. This may have to do with Windows setting the window to some type of true "fullscreen" mode. This doesn't seem to happen in other applications (DirectX?) that use this type of borderless fullscreen window.
I believe SFML has/had a variant of this problem.
There is a workaround: don't set the window to the exact size of the desktop. For instance, make the width of the window one pixel more than the width of the desktop.
Below is an SSCCE without error checking that shows what I'm talking about. This will create a borderless fullscreen window with a desktop-sized OpenGL viewport displaying an ugly green color (exit with AltF4). You can change the line #define FIX_BUG 0 to #define FIX_BUG 1 which just adds one extra pixel to the window's width to see what the behavior I want looks like.
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#pragma comment(lib, "OpenGL32.lib")
#define FIX_BUG 0
void MyRegisterClass(HINSTANCE hInstance);
HWND MyCreateWindow(HINSTANCE hInstance);
void MySetupOpenGLContext(HWND hWnd);
void MySetPixelFormat(HDC hDC);
RECT MyGetDesktopRect();
void MyLoadOpenGLFunctions();
#define APIENTRYP APIENTRY *
#define GLAPI extern
#define GL_COLOR 0x1800
typedef int GLint;
typedef int GLsizei;
typedef unsigned int GLenum;
typedef float GLfloat;
typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
PFNGLVIEWPORTPROC glViewport;
typedef void (APIENTRYP PFNGLCLEARBUFFERFVPROC) (GLenum buffer, GLint drawbuffer, const GLfloat *value);
PFNGLCLEARBUFFERFVPROC glClearBufferfv;
static const char *windowClass = "CLASS";
static const char *windowTitle = "TITLE";
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
MyRegisterClass(hInstance);
const HWND hWnd = MyCreateWindow(hInstance);
MySetupOpenGLContext(hWnd);
ShowWindow(hWnd, nCmdShow);
HDC hDC = GetDC(hWnd);
MSG msg;
while (IsWindow(hWnd)) {
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
const GLfloat color[] = {0.5f, 0.5f, 0.0f, 1.0f};
glClearBufferfv(GL_COLOR, 0, color);
SwapBuffers(hDC);
}
return 0;
}
void MyRegisterClass(HINSTANCE hInstance) {
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = &DefWindowProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOWFRAME + 1);
wcex.lpszClassName = windowClass;
RegisterClassEx(&wcex);
}
HWND MyCreateWindow(HINSTANCE hInstance) {
RECT dRect = MyGetDesktopRect();
return CreateWindow(windowClass, windowTitle, WS_POPUP,
0, 0, dRect.right + FIX_BUG, dRect.bottom,
NULL, NULL, hInstance, NULL);
}
void MySetupOpenGLContext(HWND hWnd) {
HDC hDC = GetDC(hWnd);
MySetPixelFormat(hDC);
HGLRC hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hGLRC);
MyLoadOpenGLFunctions();
RECT dRect = MyGetDesktopRect();
glViewport(0, 0, dRect.right, dRect.bottom);
}
void MySetPixelFormat(HDC hDC) {
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof pfd;
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int format = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, format, &pfd);
}
RECT MyGetDesktopRect() {
RECT desktopRect;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktopRect);
return desktopRect;
}
void MyLoadOpenGLFunctions() {
glViewport = (PFNGLVIEWPORTPROC) GetProcAddress(GetModuleHandleA("OpenGL32.dll"), "glViewport");
glClearBufferfv = (PFNGLCLEARBUFFERFVPROC) wglGetProcAddress("glClearBufferfv");
}
Github Gist | Raw Text
Why does this happen? Is there a way to get the behavior I want with the window set to the exact size of the desktop?
My platform: Visual Studio 2013 / 64-bit Windows 8 / AMD Radeon HD6850.