imagenetics
Programmer
This example is based on the various sample applications I've found on the Internet. The following code makes your application's form, when dragged, snap to screen edges.
Green lines is what you have to write. Red are already in the code.
In "Unit1.h" add this:
Now in "Unit1.cpp" add:
In the precending code the snap value is 15 (measured in pixels - form is snapped when its rectangle is 15 pixels or less from the edge of the screen, which I think is optimal. You can, ofcourse, change it, or assign it to an integer variable if you want to allow user to adjust this value.
Note I:
This works only for the form which it's applied to. If your program consists of more than one form and you want all of then to snap, rewrite the code for all forms.
Note II:
The code reads the dimensions of working area - desktop excluding the taskbar - the bottom edge is taskbar's top border.
Note III:
While compiling, you might get a warning "[C++ Warning] Unit1.h(60): W8027 Functions containing switch are not expanded inline". This IS NOT critical and everything will work.
If you want to get rid of this warning, go to compiler options (Project->Options[Shit+Ctrl+F11]->Compiler tab), click Warnings button, in the warnings list find "W8027 Functions containing..." and uncheck it.
I hope that is helpful.
Green lines is what you have to write. Red are already in the code.
In "Unit1.h" add this:
Code:
[COLOR=red]public:
__fastcall TForm1(TComponent* Owner);[/color]
[COLOR=green]BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_WINDOWPOSCHANGING, TWMWindowPosChanging, WMWindowPosChanging);
END_MESSAGE_MAP(TForm);[/color]
Code:
[COLOR=green]void __fastcall TForm1::WMWindowPosChanging(TWMWindowPosChanging &msg)
{
RECT workArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, &workArea, 0);
if(abs(msg.WindowPos->x) <= (workArea.left + 15)){
msg.WindowPos->x = workArea.left;
}else if((msg.WindowPos->x + msg.WindowPos->cx) >= (workArea.right - 15) && (msg.WindowPos->x + msg.WindowPos->cx) <= (workArea.right + 15)){
msg.WindowPos->x = workArea.right - msg.WindowPos->cx;
}
if(abs(msg.WindowPos->y) <= (workArea.top + 15)){
msg.WindowPos->y = workArea.top;
}else if((msg.WindowPos->y + msg.WindowPos->cy) >= (workArea.bottom - 15) && (msg.WindowPos->y + msg.WindowPos->cy) <= (workArea.bottom + 15)){
msg.WindowPos->y = workArea.bottom - msg.WindowPos->cy;
}
}[/color]
Note I:
This works only for the form which it's applied to. If your program consists of more than one form and you want all of then to snap, rewrite the code for all forms.
Note II:
The code reads the dimensions of working area - desktop excluding the taskbar - the bottom edge is taskbar's top border.
Note III:
While compiling, you might get a warning "[C++ Warning] Unit1.h(60): W8027 Functions containing switch are not expanded inline". This IS NOT critical and everything will work.
If you want to get rid of this warning, go to compiler options (Project->Options[Shit+Ctrl+F11]->Compiler tab), click Warnings button, in the warnings list find "W8027 Functions containing..." and uncheck it.
I hope that is helpful.