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;
Related
I am working on an ImGui project, and am trying to find a way to create custom key binds. I thought of just listing every single VK key code in a massive,
if
statement, but not only would that be bulky, but I would also not take certain keys such as some unique mouse buttons or any other key I may end up missing. I want a function that will store the next mouse or keyboard input into an integer, without the use of a predefined set of available inputs. I want to dynamically recognize any input key.
Minimal example:
const char* cbind0 = "none";
static bool bbind0 = false;
static int ibind0;
if (ImGui::Button(cbind0))
bbind0 = true
if (bbind0 == true)
{
cbind0 = "press any key...";
CopyNextInputTo(ibind0); // Function to copy pressed key to our integer
}
This code would show up as a box in the GUI, and then the integer ibind0 which is containing our now determined keybind, will be used like so:
static bool option = false;
if (GetAsyncKeyState(ibind0) & 1)
{
option =! option;
}
And now we can toggle our option on and off either using a GUI checkbox or by pressing the user-determined key.
The only problem now being I have no clue how to dynamically record all possible inputs! Does anyone know any possible functions or methods? Thanks!
Assuming you are the one creating the window you're using ImGui on, you can handle WM_KEYDOWN events in your WNDPROC callback. Here's a basic example of this in action that will create a pop-up message box whenever you press a key:
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
const wchar_t WINDOW_CLASS_NAME[] = L"Keyboard Listener Class";
const wchar_t WINDOW_TITLE[] = L"Keyboard Listener";
WNDCLASS windowClass{};
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hInstance;
windowClass.lpszClassName = WINDOW_CLASS_NAME;
RegisterClass(&windowClass);
HWND hWnd = CreateWindowEx(
0,
WINDOW_CLASS_NAME,
WINDOW_TITLE,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hWnd == NULL) {
return EXIT_FAILURE;
}
ShowWindow(hWnd, nCmdShow);
MSG msg{};
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return EXIT_SUCCESS;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return EXIT_SUCCESS;
case WM_KEYDOWN:
wchar_t keyName[64];
GetKeyNameText(lParam, keyName, sizeof(keyName));
MessageBox(hWnd, keyName, L"Key Pressed!", MB_OK);
return EXIT_SUCCESS;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return EXIT_SUCCESS;
}
Or, if you aren't the one actually creating the Window, you can hook into another Window's keyboard events using SetWindowsHookEx with the idHook parameter set to WH_KEYBOARD and passing in a KeyboardProc callback to the lpfn parameter.
This is a GameBoy emulator I've been working on for quite some time. It originally only worked on MacOS, but I've lately been working on a Windows port. I am using SDL for window creation and rendering. What I would like to do is create a normal Windows menu bar using Win32 API (CreateMenu, AppendMenu, etc). So far I can get the menu bar to exist on the Window, but I am trying to add callbacks using CALLBACK WndProc(), so the menu knows that you're clicking an option. This was not as successful. Anyone know how to do this?
This is the WinMain Entry Point:
/***************** INSTANCES *******************/
static Core::GameBoy* mGameBoy_Instance;
static FrontEnd::SDLContext* mSDL_Instance;
/***********************************************/
INT WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow)
{
// Load options
Core::Settings settings;
Util::CreateSettingsFromFile(OPTIONS_FILE_PATH, settings);
settings.skip_bootrom = true;
// Create the system instance
mGameBoy_Instance = new Core::GameBoy(settings, WIN_WIDTH, WIN_HEIGHT);
// Initalize Render Context
const char* windowTitle = mGameBoy_Instance->GetCurrentROM()->GetRomName();
mSDL_Instance = new FrontEnd::SDLContext(WIN_WIDTH, WIN_HEIGHT, WIN_SCALE, windowTitle);
// Initialize menubar
SDL_SysWMinfo sysInfo;
SDL_VERSION(&sysInfo.version);
SDL_GetWindowWMInfo(mSDL_Instance->GetWindow(), &sysInfo);
HWND hwnd = sysInfo.info.win.window;
/*********************************/
HMENU menubar = CreateMenu();
/*********************************/
HMENU file = CreateMenu();
HMENU exit = CreateMenu();
/*********************************/
AppendMenu(menubar, MF_POPUP, (UINT_PTR)file, "File");
AppendMenu(file, MF_STRING, (UINT_PTR)exit, "Exit");
SetMenu(hwnd, menubar);
// start sdl thread and main loop
}
And this is the callback function code:
LRESULT CALLBACK WndProc(_In_ HWND hwnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
{
switch (uMsg) {
case WM_COMMAND:
{
switch (LOWORD(wParam)) {
case 2: // Should be the Exit button in the order of the menu
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return NULL;
}
When I do this, the callbacks aren't called.
Any help is appreciated.
Thanks!
You are assigning the exit HMENU as the ID of the item ou create for exit, that is not going to match "case 2:".
Get rid of the "HMENU exit" Make the call:
AppendMenu(file, MF_STRING, 2, _T("Exit"));
I would suggest that you assign some constant name (either #define or enum) and use that identifier in both the AppendMenu and case statement.
Normal menu items have numeric ids, not a HMENU handle. Submenus should be created with CreatePopupMenu, not CreateMenu:
#define ID_EXIT 50
...
HMENU menubar = CreateMenu();
HMENU filemenu = CreatePopupMenu();
AppendMenu(filemenu, MF_STRING, ID_EXIT, "Exit");
AppendMenu(menubar, MF_POPUP, (UINT_PTR) filemenu, "File");
SetMenu(hwnd, menubar);
...
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_EXIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
break;
}
break;
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 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.
win32 windows application and want to capture full screen and remove the border of window displayed any one tell me how can i do so if this window capture the mouse keyboard controls then it will be ideal?
// MonitorScreen.cpp : Defines the entry point for the application. //
#include "stdafx.h"
#include "MonitorScreen.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_MONITORSCREEN, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance);
// Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MONITORSCREEN));
// 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;
int s =sizeof(WNDCLASSEX); wcex.cbSize =sizeof(WNDCLASSEX);
wcex.style = DESKTOP_HOOKCONTROL ;//CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = NULL;//hInstance; wcex.hIcon = NULL;//LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MONITORSCREEN)); wcex.hCursor = NULL;//LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(9); wcex.lpszMenuName = NULL;//MAKEINTRESOURCE(IDC_MONITORSCREEN); wcex.lpszClassName = szWindowClass; wcex.hIconSm = NULL;//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;
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_PAINT: hdc = BeginPaint(hWnd, &ps); // TODO: Add any drawing code here... EndPaint(hWnd, &ps); break; case WM_DESTROY: 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; }
I'm going to assume you don't actually want to capture the screen, your question cannot make sense. I'm guessing you simply want to make your window maximized to occupy the full screen. Put the template generated code back the way it was, simply change the ShowWindow call:
ShowWindow(hWnd, SW_MAXIMIZE);
Found this on google: Difficulties with Screen Capture, C/C++...
Try to understand the code and adjust it to you needs.
void CaptureScreen(LPCTSTR lpszFilePathName)
{
BITMAPFILEHEADER bmfHeader;
BITMAPINFO *pbminfo;
HBITMAP hBMP;
CFile oFile;
CDC *pDC = GetWindowDC();
INT nSizeImage = 1024 * 768 * 3;
CHAR *pBuff = new CHAR[sizeof(BITMAPINFOHEADER) + nSizeImage];
pbminfo = (BITMAPINFO *)pBuff;
hBMP = (HBITMAP)pDC->GetCurrentBitmap()->m_hObject;
ZeroMemory(pbminfo, sizeof(BITMAPINFO));
pbminfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(pDC->m_hDC,
hBMP,
0,
1,
NULL,
pbminfo,
DIB_RGB_COLORS);
GetDIBits(pDC->m_hDC,
hBMP,
0,
pbminfo->bmiHeader.biHeight,
pBuff + sizeof(BITMAPINFOHEADER),
pbminfo,
DIB_RGB_COLORS);
ReleaseDC(pDC);
bmfHeader.bfType = 0x4d42; /*"BM"*/
bmfHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nSizeImage;
bmfHeader.bfReserved1 = 0;
bmfHeader.bfReserved2 = 0;
bmfHeader.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
oFile.Open(lpszFilePathName, CFile::modeWrite | CFile::modeCreate);
oFile.Write(&bmfHeader, sizeof(BITMAPFILEHEADER));
oFile.Write(pBuff, sizeof(BITMAPINFOHEADER) + pbminfo->bmiHeader.biSizeImage);
delete []pBuff;
oFile.Close();
}
I think you want to use OpenGL or DirectX and invoke full-screen mode for your application. There will of course be a few keystrokes you can't intercept, such as the Secure Attention Sequence (usually CTRL+ALT+DEL) but most interaction with the computer will be directed to your program.
first to get the full screen you should type this in:
ShowWindow(hWnd, SW_SHOWMAXIMIZED)
Second to remove the border or witch is called thick frame in the window styles you should type in your CreateWindow function(the place where you put your styles (WS_)):
CreateWindow(your class,
window name,
WS_POPUP | WS_BORDER,
The rest of your params for the create window func.);
Ps:WS_BORDER IS JUST A 1 PX BLACK BORDER so you can know where is the window if it was not minimized and on top of a window woth the same bg color AND YOU CAN REMOVE IT WHENEVER YOU WANT
Also you wont be able to minimize,maximize,close the window with the built in buttons because they wont be there so if you want to close the app just make a button that sends a PostQuitMessage when pressed.
good luck