I have had this problem and added the following code to MainFrame.cpp and WinPos.h and WinPos.cpp to control where the app opens upon startup. DirOpus needs something similar.
Bob Van Tuyl
Software Engineer, Retired
San Jose, CA
void MainFrame::OnMove(int x, int y)
{CRect winRect; GetWindowRect(&winRect); winPos.set(winRect); CFrameWndEx::OnMove(x, y);}
void MainFrame::OnSize(UINT nType, int cx, int cy) {
CRect winRect;
CFrameWndEx::OnSize(nType, cx, cy);
if (!isInitialized) return;
GetWindowRect(&winRect); winPos.set(winRect);
}
_____
#pragma once
struct WinPosData {
int left;
int top;
int width;
int depth;
WinPosData() : left(0), top(0), width(0), depth(0) { }
~WinPosData() { }
WinPosData& operator= (RECT& r)
{left = r.left; top = r.top; width = r.right - r.left; depth = r.bottom - r.top; return *this;}
operator RECT() {return {left, top, width, depth};}
void load(RECT& defaultRect);
void save();
void normalize(int screenWidth, int maxDepth);
};
class WinPos {
WinPosData data; // App window position and size
int screenWidth; // Maximum Screen width and height
int screenHeight;
public:
WinPos();
~WinPos() {save();}
// initialize the window to the saved position and size, only call once
void initialPos(CWnd* wnd, RECT& defaultRect);
void set(CRect& curPos) {data = curPos;}
private:
void save() {data.save();} // Save the current position and size when app closes.
};
----------
// Manage Window Position and Size
#include "pch.h"
#include "WinPos.h"
#include "IniFile.h"
WinPos::WinPos() {
RECT rsys;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rsys, 0);
screenWidth = rsys.right; screenHeight = rsys.bottom;
}
void WinPos::initialPos(CWnd* wnd, RECT& defaultRect) {
data.load(defaultRect); data.normalize(screenWidth, screenHeight);
wnd->SetWindowPos(0, data.left, data.top, data.width, data.depth, SWP_NOCOPYBITS); // | SWP_NOACTIVATE
}
static TCchar* WindowPosData = _T("Window Position Data");
static TCchar* Left = _T("Left");
static TCchar* Top = _T("Top");
static TCchar* Width = _T("Width");
static TCchar* Depth = _T("Depth");
void WinPosData::save() {
iniFile.write(WindowPosData, Left, left);
iniFile.write(WindowPosData, Top, top);
iniFile.write(WindowPosData, Width, width);
iniFile.write(WindowPosData, Depth, depth);
}
void WinPosData::load(RECT& defaultRect) {
left = iniFile.readInt(WindowPosData, Left, defaultRect.left);
top = iniFile.readInt(WindowPosData, Top, defaultRect.top);
width = iniFile.readInt(WindowPosData, Width, defaultRect.right - defaultRect.left);
depth = iniFile.readInt(WindowPosData, Depth, defaultRect.bottom - defaultRect.top);
if (left < 0) left = 0;
if (top < 0) top = 0;
if (width < 100) width = 100;
if (depth < 100) depth = 100;
}
void WinPosData::normalize(int screenWidth, int screenHeight) {
if (width < 100 || screenWidth < width) width = 100;
if (depth < 100 || screenHeight < depth) depth = 100;
if (left < 0) left = 0;
if (left + width > screenWidth) left = screenWidth - width;
if (top < 0) top = 0;
if (top + depth > screenHeight) top = screenHeight - depth;
}