i have developing a C++ Api project.
i will use dialogboxparam to create a dialogbox...
i done to create and set the textbox,labels and buttons...
its work fine...
now i want to add a image in the top of the dialogbox...
i did use this code in WM_INITDIALOG:
HBITMAP hImage= (HBITMAP)LoadImage(NULL,L"C:\\WINDOWS\\system32\\BMA-Images\\login-header",IMAGE_BITMAP,LR_DEFAULTSIZE ,LR_DEFAULTSIZE ,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
SendMessage(_hwnd,STM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hImage);
But it didnt work...
Can anyone help to resolve this...
Thanks in advance
Sonu
The easiest way is to override the WM_PAINT for the window and paint the bitmap at that point (between the BeginPaint and EndPaint) calls
There is an MFC based example here:
http://www.programmersheaven.com/mb/mfc_coding/113034/113034/how-do-i-set-a-background-picture-in-a-dialog-box-/
When processing the WM_INITDIALOG message use HWND hImageCtl = GetDlgItem(_hwnd, <image-control-resource-id>) to get the handle of the image-control (this assumes _hwnd is the handle to the dialog itself).
Then use hImageCtl to send the STM_SETIMAGE message to.
This works for years, since Windows 98 for me:
//globals
HBRUSH hbr;
PAINTSTRUCT wcd;
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam )
{
switch( msg ) {
case WM_PAINT:
if (GetUpdateRect(hWnd,r,0)) {
BeginPaint(hWnd,&wcd);
if (wParam == NULL) FillRect(wcd.hdc,&wcd.rcPaint,hbr);
EndPaint(hWnd,&wcd);
}
break;
case WM_COMMAND:
///your code
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int Show)
{
ghInstance = hInstance;
//Prepare brush for background
hbr=CreatePatternBrush(LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BGROUND)));
///your code
DialogBox(hInstance,"MAIN", NULL,(DLGPROC)MainWndProc);
///your code
return(FALSE);
}
IDB_BGROUND - id of image resource, linked in.
Related
I recently acquired a laptop with a high dpi screen and have been adapting one of my programs to take advantage of the higher resolution. In one of my Win32 dialog boxes I display a bitmap picture. Normally I have that assigned to a picture control via the resource editor and don't have to deal with it. Now I need to display one of two images depending on the screen resolution. I know how to load a bitmap and get its handle, but how do I assign it to the picture control during the dialog's initiation so that the built in dialog routine will display it? I'm thinking I should put a static picture control in the dialog, but I can't find a win32 example of how I assign the image to that picture control at run time. Seems like it should be simple, but I can't seem to find a windows function that's appropriate. I'm not using MFC.
--Terry
Here's an example. Basically, you need to send the STM_SETIMAGE message to the picture control, along with the handle of the image you'd like to display.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "resource.h"
HINSTANCE hInst;
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HBITMAP bmp1, bmp2;
static bool isImg1 = true;
switch(uMsg)
{
case WM_INITDIALOG:
bmp1 = (HBITMAP)SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_GETIMAGE, IMAGE_BITMAP, 0);
bmp2 = (HBITMAP)LoadImage(NULL, "bitmap2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
return TRUE;
case WM_DESTROY:
DeleteObject(bmp1);
DeleteObject(bmp2);
return true;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_QUIT:
EndDialog(hwndDlg, 0);
return TRUE;
case IDC_BTN_TEST:
if (isImg1)
SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp2);
else
SendDlgItemMessage(hwndDlg, IDC_STATIC1, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)bmp1);
isImg1 = !isImg1;
return TRUE;
}
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst = hInstance;
// The user interface is a modal dialog box
return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}
Load a bitmap from resource
Send a message to Picture Control
case IDC_BUTTON_RELOAD:
hbitmap = (HBITMAP)LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP_FULL));
hbitmap2 = (HBITMAP)LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP_EMPTY));
/*hdc = GetDC(hDlg);
image_dc = CreateCompatibleDC(hdc);
old_hbitmap = (HBITMAP)SelectObject(image_dc,hbitmap);
BitBlt(hdc,1,0,530,450,image_dc,0,0,SRCCOPY);*/
SendMessage(GetDlgItem(hDlg,IDC_STATIC_PICTURE2), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbitmap);
SendMessage(GetDlgItem(hDlg,IDC_STATIC_PICTURE), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbitmap2);
return TRUE;
case IDC_BUTTON_EXIT:
MessageBox(hDlg,_T("Thank you"),_T("Close"),MB_ICONINFORMATION|MB_OK);
DeleteObject(hbitmap);
DeleteObject(hbitmap2);
DestroyWindow(hDlg);
return TRUE;
I have created a standard win32 windows application in Visual Studio 2010. The only addition I have made is a TextOut call in the WM_PAINT handler that displays the alphabet (repeated 4 times for width) at position 0, 0.
My problem is that when I resize the window, expanding to the right, there is some drawing error by the right side border. Black blocks are shown during the resizing/drawing process as if the right hand edge is being stretched. The result is a strange black “smearing” effect as I resize. It only happens during the resize; once I release the mouse, the window looks correct.
I have tried double buffering to a memory DC, but see the same effect. I am not using any windows themes code.
The only way I can remove the effect is to handle WM_NCPAINT (and return 0) – but, of course, this means the border isn’t painted which won’t be an acceptable solution! I mention it in case it helps anybody with an idea.
Thanks for any ideas or help!
#Arx – Sorry, I hadn’t made myself clear. When I say the borders smear, I meant the right hand edge of the displayed text, not the border itself.
It happens if I just add the TextOut call in the WM_PAINT handler. Handling WM_ERASEBKGRND and setting the window class background brush makes no difference.
#David – Apologies. I see the effect after adding only one line to the standard VS 2008 Win32 application created by the new project wizard – so I didn’t see the point in posting 200+ lines of code with only one line of interest :)
I added this line to the WM_PAINT handler:
TextOut (hdc, 0, 0, L"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ", 104);
Here is the full version of the code with double buffering added. The smearing of the text on the right hand side (by the window edge) as the window is expanded still occurs.
// win32_smearing_at_border.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "win32_smearing_at_border.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // 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);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32_SMEARING_AT_BORDER, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32_SMEARING_AT_BORDER));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
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, MAKEINTRESOURCE(IDI_WIN32_SMEARING_AT_BORDER));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0; //(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32_SMEARING_AT_BORDER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
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;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static HDC memory_dc;
static HBITMAP memory_bmp;
static HBITMAP oldbmp;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_ERASEBKGND:
return 1;
case WM_CREATE:
// Create memory DC
{
HDC dc = GetDC (hWnd);
memory_dc = CreateCompatibleDC (dc);
memory_bmp = CreateCompatibleBitmap (dc, 2560, 1440); // arbitary values for testing, set to my current monitor size.
oldbmp = (HBITMAP)SelectObject(memory_dc, memory_bmp);
TextOut (memory_dc, 0, 0, L"ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ",104);
ReleaseDC (hWnd, dc);
}
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
BitBlt(hdc, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right-ps.rcPaint.left, ps.rcPaint.bottom-ps.rcPaint.top, memory_dc, ps.rcPaint.left, ps.rcPaint.top, SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
// Clean up memory DC
SelectObject (memory_dc,oldbmp);
DeleteObject (memory_bmp);
DeleteDC (memory_dc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Hi Arx
Many thanks for trying out the code – I appreciate it. Good to know it’s working for you.
But … I am experiencing some very strange results. I have the “blurring” issue when running on a Windows 7 x64 machine. However, trying it out in a Windows 7 virtual machine (using VMWare on the SAME machine), it works perfectly.
So on the same machine, natively it blurs, but virtually it doesn’t. I even tried a Windows 8 virtual machine, and again it worked fine.
What I have found is that if I select a NON-Aero theme, all works ok (although it’s doesn’t look so good without Aero). And yet on the other machines where it IS working, they DO have Aero themes selected. I really don’t get it.
So that’s not really a solution. I don’t want to have to ask my potential customers to turn Aero off.
I have tried calls to many of the Dwm* functions trying to find a difference between the working and non-working machines but cannot find anything.
If I insert this code:
DWMNCRENDERINGPOLICY policy = DWMNCRP_DISABLED;
DwmSetWindowAttribute(hWnd,
DWMWA_NCRENDERING_POLICY,
(void*)&policy,
sizeof(DWMNCRENDERINGPOLICY));
Just after creating the main window then all works fine again (although, once again, doesn’t look as good without the Aero border).
So am I really at a loss as to how I can move forward. Any ideas gratefully received!
I think I can provide some more insight about where that smearing/blurring is coming from and why you see it more (or differently) in Windows 8/10 Aero.
The fact that your code has:
wcex.style = CS_HREDRAW | CS_VREDRAW;
is the reason why you didn't see the smearing/blurring on Win7. This causes Windows to fill in newly exposed areas of the window that have not yet been drawn by your WM_PAINT with a solid color, which is not perfect but not that distracting.
But under Windows 8/10 Aero, things are different. Apps don't draw directly to the screen, but rather draw to offscreen buffers that are then composited by the evil DWM.exe window manager. It turns out that DWM actually adds another layer of BitBlt-type behavior on top of the existing legacy XP/Vista/7 BitBlt behavior that is affected by CS_HREDRAW | CS_VREDRAW.
And the DWM blit behavior is even more crazy because they don't just copy the client area, but they actually replicate pixels at the edges of your old client area to make the new one.
Unfortunately, making DWM not do its blit is much harder than just passing some extra flags.
I don't have a 100% solution, but please see this Q&A for a sort of timing trick that can be used to greatly reduce the frequency with which DWM messes with your window client area, which will reduce the smearing/blurring:
How to smooth ugly jitter/flicker/jumping when resizing windows, especially dragging left/top border (Win 7-10; bg, bitblt and DWM)?
Enjoy!
I've created a dialog box inside a Win32 DLL (using resource editor) and now want to show it as application program (using this DLL) calls DisplayDialog, but it is not working.
// AppProgram.cpp
...
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_COMMAND:
switch (LOWORD (wParam)) {
case IDM_FILE_NEW_DIALOG:
DisplayDialog (hInst, hWnd);
break;
...
}
break;
....
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
My DLL appears like
#include "stdafx.h"
#include "myDLL.h"
EXPORT BOOL CALLBACK DisplayDialog (HINSTANCE hInst, HWND hWnd) {
DialogBox (hInst, MAKEINTRESOURCE (IDD_DIALOG1), hWnd, reinterpret_cast<DLGPROC> (DiagProc));
// MessageBox works here
}
...
I've tested that this DLL displays dialog if the dialog belongs to AppProgram.
Here, I want to display dialog when it is a part of DLL.
Please suggest whether we should create dialog inside DLL or should pass it from program. + how to show dialog in given scenario. Thanks in advance.
The hInst parameter is the handle to the module that contains the dialog resource. If you want to get the dialog from the DL's resourcesL, then pass the handle to the DLL rather than the handle to the main application.
Something like this:
HMODULE module = LoadLibrary("MyDll.dll");
HRSRC res = FindResource(module, "#1234", RT_DIALOG);
DLGTEMPLATE* pTemplate = (DLGTEMPLATE*)LoadResource(module, res);
DialogBoxIndirect(0, pTemplate, hwnd, dlgproc);
I'm looking for a way to manipulate a MessageBox like a window, would I have to actually use CreateWindowEx and make a tiny window in order to get the full functionality of a window including using things such as WM_GETTEXT and WM_SETTEXT or is there some way I can send these messages to a MessageBox? If I absolutely can't send windows messages to a MessageBox, then is there alternate methods less complicated then using CreateWindowEx to accomplish what I'm looking for?
Use a thread-local WH_CBT hook when you call MessageBox() to get its window handle. You can then send messages to it like any other window.
// if you need to customize MessageBox() across threads,
// store this variable in a TLS (thread-local storage) slot...
HHOOK hHook;
LRESULT CALLBACK MyCBTHook(int nCode, WPARAM wParam, LPARAM lParam)
{
switch (nCode)
{
case HCBT_CREATEWND: // window is being created
case HCBT_ACTIVATE: // window is being activated
{
HWND hWnd = (HWND) wParam;
// use hWnd as needed...
break;
}
}
return CallNextHookEx(hHook);
}
int MyMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC)&MyCBTHook, NULL, GetCurrentThreadId());
int iResult = MyMessageBox(hWnd, lpText, lpCaption, uType);
UnhookWindowsHookEx(hHook);
return iResult;
}
I'm at a loss on this one, not really even sure what code to post.
I have a c++ application. The initial window has an edit control, custom control, and menu. When the user presses F5, a new window open with its own message loop. The new window then opens a DialogBox, and is modal as expected.
The odd part is that the DialogBox seems to be hidden behind the 2nd window. I have to alt-tab to the first window, then alt-tab back to the second window to get to the dialog box.
If I cancel out of the DialogBox, the second window is destroyed (as expected), and a MessageBox is shown (as expected), but that message box seems to be hidden behind the first window. Again, I have to alt-tab twice to get the MessageBox to the forefront, and OK out of it.
Does anyone have any ideas what could cause this odd Pop-Under behavior with both the DialogBox and MessageBox?
Thanks!
Code to create the Dialog Box
INT_PTR ip = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_AMBIGUOUS), this->hWnd, DlgAmbiguous);
Code for the DlgAmbiguous Proceduels
INT_PTR CALLBACK DlgAmbiguous(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
HWND hStaticTerm = GetDlgItem(hDlg, IDC_TERM);
SetWindowText(hStaticTerm, lpAmbiguousTerm);
if (wcscmp(lpAmbiguousTerm, L"canvas") == 0)
{
HWND hComboBox = GetDlgItem(hDlg, IDC_MEANING);
SendMessage(hComboBox, CB_ADDSTRING, NULL, reinterpret_cast<LPARAM>(_T("Program Window")));
SendMessage(hComboBox, CB_ADDSTRING, NULL, reinterpret_cast<LPARAM>(_T("Region in Program Window")));
}
return (INT_PTR)TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
HWND hComboBox = GetDlgItem(hDlg, IDC_MEANING);
iAmbiguousResult = SendMessage(hComboBox, CB_GETCURSEL, NULL, NULL);
EndDialog(hDlg, iAmbiguousResult + 100);
return (iAmbiguousResult == -1) ? FALSE : TRUE;
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, DLG_CANCEL);
return (INT_PTR)FALSE;
}
break;
}
return (INT_PTR)FALSE;
When you open a window, regardless if it runs on the same thread or not, it makes sense to specify its parent window. This will force new window to appear above its parent. The same applies to the dialog box:
INT_PTR WINAPI DialogBox(
_In_opt_ HINSTANCE hInstance,
_In_ LPCTSTR lpTemplate,
_In_opt_ HWND hWndParent,
_In_opt_ DLGPROC lpDialogFunc
);
Note the third parameter. Other option to bring your window on top is by calling the SetWindowPos function.