⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,26370,00.html

📁 C++builder学习资料C++builder
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</table>  

  

<p>  

The one part that really warrants explanation is testing the work area (screen). When we test for the window to snap, we only snap right-to-left, left-to-right, top-to-bottom, and bottom-to-top. (This is more explanatory if you run the example application). When we test for the screen we must reverse the order, snapping the right of our window to the right  

edge of the scree, etc.  

</p>  

</td></tr>  

  

<tr><td align = left>  

<a name="code"></a><A class=loginLink href="#top"><font face="Arial">Code</font></a> 

</td></tr> 

 

<tr><td> 

<img src="images/snapwin.jpg" alt="this is our form" width="267" height="118"><br><br> 

<table width=100% border=1 cellpadding=3 cellspacing=0> 

<tr><td bgcolor=blue><font color=white>mainunit.h</font></td></tr> 

<tr><td> 

<pre><code>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<font color="202020">#ifndef mainunitH</font>

<font color="202020">#define mainunitH</font>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<font color="202020">#include &lt;Classes.hpp&gt;</font>

<font color="202020">#include &lt;Controls.hpp&gt;</font>

<font color="202020">#include &lt;StdCtrls.hpp&gt;</font>

<font color="202020">#include &lt;Forms.hpp&gt;</font>



<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>class</b> Tsnapform : <b>public</b> TForm

{

<b>__published</b>:	<font color="#0000AA"><i>// IDE-managed Components</i></font>

  TEdit *Edit1;

  TButton *Button1;

  TEdit *Edit2;

  TButton *Button2;

  TCheckBox *move;

  <b>void</b> <b>__fastcall</b> Button1Click(TObject *Sender);

  <b>void</b> <b>__fastcall</b> Button2Click(TObject *Sender);

<b>private</b>:	<font color="#0000AA"><i>// User declarations</i></font>

  HWND snapwin;

  RECT work_area;

  <b>bool</b> snapped;

  <b>bool</b> winprocthing;

  <b>int</b> thresh;

  <b>void</b> <b>__fastcall</b> SettingChanged(TMessage &amp;msg);

  <b>void</b> <b>__fastcall</b> WMWindowPosChanging(TWMWindowPosChanging &amp;msg);

  <b>void</b> <b>__fastcall</b> UpdateWorkArea();

<b>public</b>:		<font color="#0000AA"><i>// User declarations</i></font>

  <b>__fastcall</b> Tsnapform(TComponent* Owner);

  <b>__fastcall</b> ~Tsnapform();



BEGIN_MESSAGE_MAP

  MESSAGE_HANDLER(WM_WINDOWPOSCHANGING,TWMWindowPosChanging,WMWindowPosChanging);

  MESSAGE_HANDLER(WM_SETTINGCHANGE,TMessage,SettingChanged);

END_MESSAGE_MAP(TForm);

};

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>extern</b> PACKAGE Tsnapform *snapform;

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<font color="202020">#endif</font>

</code></pre> 

</td></tr> 

</table> 

<br> 

<table border=1 width=100% cellpadding=3 cellspacing=0> 

<tr><td bgcolor=blue><font color=white>mainunit.cpp</font></td></tr> 

<tr><td> 

<pre><code>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>



<font color="202020">#include &lt;vcl.h&gt;</font>

<font color="202020">#pragma hdrstop</font>



<font color="202020">#include &quot;mainunit.h&quot;</font>

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<font color="202020">#pragma package(smart_init)</font>

<font color="202020">#pragma resource &quot;*.dfm&quot;</font>

Tsnapform *snapform;

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>__fastcall</b> Tsnapform::Tsnapform(TComponent* Owner)

  : TForm(Owner)

{

  snapped=<b>false</b>;

  UpdateWorkArea();  <font color="#0000AA"><i>//set our workarea rect</i></font>

  thresh = StrToInt(Edit2-&gt;Text); <font color="#0000AA"><i>//get threshold</i></font>

  snapwin = FindWindow(Edit1-&gt;Text.c_str(),NULL); <font color="#0000AA"><i>//get window to snap to</i></font>

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>__fastcall</b> Tsnapform::~Tsnapform()

{

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>void</b> <b>__fastcall</b> Tsnapform::SettingChanged(TMessage &amp;msg)

{

  UpdateWorkArea();

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>void</b> <b>__fastcall</b> Tsnapform::WMWindowPosChanging(TWMWindowPosChanging &amp;msg)

{

  RECT sr;

  snapped=<b>false</b>;

  <font color="#0000AA"><i>//test window</i></font>

  <b>if</b> (snapwin &amp;&amp; IsWindowVisible(snapwin))

  {

    <b>if</b> (GetWindowRect(snapwin,&amp;sr))

    {

      <b>if</b> ( (msg.WindowPos-&gt;x &lt;= (sr.right+thresh)) &amp;&amp;

           (msg.WindowPos-&gt;x &gt;= (sr.right-thresh)) ) {

        <b>if</b> ((msg.WindowPos-&gt;y &gt; sr.top) &amp;&amp; (msg.WindowPos-&gt;y &lt; sr.bottom)) {

          snapped=true;

          msg.WindowPos-&gt;x = sr.right;

        }

      }

      <b>else</b> <b>if</b> ((msg.WindowPos-&gt;x + msg.WindowPos-&gt;cx) &gt;= (sr.left-thresh) &amp;&amp;

               (msg.WindowPos-&gt;x + msg.WindowPos-&gt;cx) &lt;= (sr.left+thresh)) {

        <b>if</b> ((msg.WindowPos-&gt;y &gt; sr.top) &amp;&amp; (msg.WindowPos-&gt;y &lt; sr.bottom)) {

          snapped=true;

          msg.WindowPos-&gt;x = sr.left-msg.WindowPos-&gt;cx;

        }

      }



      <b>if</b> ( (msg.WindowPos-&gt;y &lt;= (sr.bottom+thresh)) &amp;&amp;

           (msg.WindowPos-&gt;y &gt;= (sr.bottom-thresh)) ) {

        <b>if</b> ((msg.WindowPos-&gt;x &gt; sr.left) &amp;&amp; (msg.WindowPos-&gt;x &lt; sr.right)) {

          snapped=true;

          msg.WindowPos-&gt;y = sr.bottom;

        }

      }

      <b>else</b> <b>if</b> ((msg.WindowPos-&gt;y + msg.WindowPos-&gt;cy) &lt;= (sr.top+thresh) &amp;&amp;

               (msg.WindowPos-&gt;y + msg.WindowPos-&gt;cy) &gt;= (sr.top-thresh)) {

        <b>if</b> ((msg.WindowPos-&gt;x &gt; sr.left) &amp;&amp; (msg.WindowPos-&gt;x &lt; sr.right)) {

          snapped=true;

          msg.WindowPos-&gt;y = sr.top-msg.WindowPos-&gt;cy;

        }

      }

    }

  }



  <font color="#0000AA"><i>//test screen</i></font>

  sr = work_area;

  <b>if</b> (abs(msg.WindowPos-&gt;x) &lt;= (sr.left+thresh)) {

    snapped=true;

    msg.WindowPos-&gt;x = sr.left;

  }

  <b>else</b> <b>if</b> ((msg.WindowPos-&gt;x + msg.WindowPos-&gt;cx) &gt;= (sr.right-thresh) &amp;&amp;

           (msg.WindowPos-&gt;x + msg.WindowPos-&gt;cx) &lt;= (sr.right+thresh)) {

    snapped=true;

    msg.WindowPos-&gt;x = sr.right-msg.WindowPos-&gt;cx;

  }



  <b>if</b> (abs(msg.WindowPos-&gt;y) &lt;= (sr.top+thresh)) {

    snapped=true;

    msg.WindowPos-&gt;y = sr.top;

  }

  <b>else</b> <b>if</b> ((msg.WindowPos-&gt;y+msg.WindowPos-&gt;cy) &gt;= (sr.bottom-thresh) &amp;&amp;

           (msg.WindowPos-&gt;y+msg.WindowPos-&gt;cy) &lt;= (sr.bottom+thresh)) {

    snapped=true;

    msg.WindowPos-&gt;y = sr.bottom-msg.WindowPos-&gt;cy;

  }

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>void</b> <b>__fastcall</b> Tsnapform::UpdateWorkArea()

{

  SystemParametersInfo(SPI_GETWORKAREA, 0, &amp;work_area, 0);

  <font color="#0000AA"><i>//get the size of the desktop or whatever</i></font>

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>void</b> <b>__fastcall</b> Tsnapform::Button1Click(TObject *Sender)

{

  snapwin = FindWindow(Edit1-&gt;Text.c_str(),NULL);

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

<b>void</b> <b>__fastcall</b> Tsnapform::Button2Click(TObject *Sender)

{

  thresh = StrToInt(Edit2-&gt;Text);

}

<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

</code></pre> 

</td></tr> 

</table> 

</td></tr> 

 

</table>   

 

</body>  

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -