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

📄 generic.html

📁 非常好的在win32下用c++sdk开发的工具书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    Controller * pCtrl = WinGetLong<Controller *> (hwnd);

    switch (message)
    {
    case WM_CREATE:
        <font color="#cc0066">// Have to catch exception in case new throws!</font>
        try
        {
            pCtrl = new Controller (hwnd, 
                    reinterpret_cast&lt;CREATESTRUCT *&gt; (lParam));
            WinSetLong&lt;Controller *&gt; (hwnd, pCtrl);
        }
        catch (WinException e)
        {
            ::<font color="#000099"><b>MessageBox</b></font> (hwnd, e.GetMessage(), "Initialization",
                MB_ICONEXCLAMATION | MB_OK);
            return -1;
        }
        catch (...)
        {
            ::<font color="#000099"><b>MessageBox</b></font> (hwnd, "Unknown Error", "Initialization",
                MB_ICONEXCLAMATION | MB_OK);
            return -1;
        }
        return 0;
    case WM_SIZE:
        pCtrl-&gt;Size (LOWORD(lParam), HIWORD(lParam));
        return 0;
    case WM_PAINT:
        pCtrl-&gt;Paint ();
        return 0;
    case WM_COMMAND:
        pCtrl-&gt;Command (LOWORD (wParam));
        return 0;
    case WM_DESTROY:
        WinSetLong&lt;Controller *&gt; (hwnd, 0);
        delete pCtrl;
        return 0;
    }
    return ::<font color="#000099"><b>DefWindowProc</b></font> (hwnd, message, wParam, lParam);
}</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->
<hr><!--Text-->
Here are examples of simple implementations of a few controller methods. The constructor has to remember the handle to the window for later use, the destructor has to post the quit message, The <font color="#cc0066"><b>Size</b></font> method passes its argument to <font color="#cc0066"><b>View</b></font>, etc. We'll talk about painting the window a little later. For now, notice that the controller prepares the paint canvas for the <font color="#cc0066"><b>View</b></font> to work with.
<hr><!--End Text-->

         <!--Yellow background-->
        <table cellpadding=10 cellspacing=0 width="100%">
                <tr>
                      <td width=20>
                      <td bgcolor="#e0e080">
<pre><font face="courier">
<font color="#cc0066"><b>Controller::Controller</b></font> (HWND hwnd, CREATESTRUCT * pCreate)
   :_hwnd (hwnd), 
    _model ("Generic") 
{
}

<font color="#cc0066"><b>Controller::~Controller</b></font> ()
{
    ::<font color="#000099"><b>PostQuitMessage</b></font>(0);
}

void <font color="#cc0066"><b>Controller::Size</b></font> (int cx, int cy)
{
    _view.SetSize (cx, cy);
}

void <font color="#cc0066"><b>Controller::Paint</b></font> ()
{
    <font color="#cc0066">// prepare the canvas and let View do the rest</font>
    PaintCanvas canvas (_hwnd);
    _view.Paint (canvas, _model);
    <font color="#cc0066">// Notice: The destructor of PaintCanvas called automatically!</font>
}

</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->
<hr><!--Text-->
When the user selects one of the menu items, Window procedure is called with the message <font color="#009966">WM_COMMAND</font>. The appropriate controller method dispatches the command based on the command id. When you create the menu using your resource editor, you pick these command ids for each menu item. They are stored in the appropriate header file (resource.h in our case) that has to be included in the controller's source file. 
<p>Our menu contains only three items with the ids <b>IDM_EXIT</b>, <b>IDM_HELP</b>, and <b>IDM_ABOUT</b>. The dialog box that is displayed in response to <b>IDM_ABOUT</b> is also created using the resource editor and given the id <b>IDD_ABOUT</b>. Its dialog procedure is <font color="#cc0066"><b>AboutDlgProc</b></font>. 
<p>Finally, in order to display a dialog box we need the handle to the application instance. The standard way to retrieve it is to access the internal Windows data structure using the appropriate <b>hwnd</b>.
<hr><!--End Text-->

         <!--Yellow background-->
        <table cellpadding=10 cellspacing=0 width="100%">
                <tr>
                      <td width=20>
                      <td bgcolor="#e0e080">
<pre><font face="courier">
<font color="#cc0066">// Menu command processing</font>

void <font color="#cc0066"><b>Controller::Command</b></font> (int cmd)
{
    switch (cmd)
    {
    case IDM_EXIT:
        ::<font color="#000099"><b>SendMessage</b></font> (_hwnd, <font color="#009966">WM_CLOSE</font>, 0, 0L);
        break;
    case IDM_HELP:
        ::<font color="#000099"><b>MessageBox</b></font> (_hwnd, "Go figure!",
            "Generic", MB_ICONINFORMATION | MB_OK);
        break;
    case IDM_ABOUT:
        {
            <font color="#cc0066">// Instance handle is available through HWND</font>
            HINSTANCE hInst = WinGetLong&lt;HINSTANCE&gt; (_hwnd, GWL_HINSTANCE);
            ::<font color="#000099"><b>DialogBox</b></font> (hInst, 
                   MAKEINTRESOURCE (IDD_ABOUT), 
                   _hwnd, 
                   AboutDlgProc);
        }
        break;
    }
}</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->
<hr><!--Text-->
The view object usually stores the dimensions of the client area. They are updated whenever the controller processes the WM_SIZE message. The first WM_SIZE message is sent during window creation and before WM_PAINT, so we can safely assume that when Paint is called, the dimensions of the client area are known.
<p>Graphical output to the window is done by calling appropriate methods of the Canvas object. In our case, we print text obtained from the model and draw a vertical line ten pixels from the left edge of the client area.
<hr><!--End Text-->

         <!--Yellow background-->
        <table cellpadding=10 cellspacing=0 width="100%">
                <tr>
                      <td width=20>
                      <td bgcolor="#e0e080">
<pre><font face="courier">
class <font color="#cc0066"><b>View</b></font>
{
public:

    void SetSize (int cxNew, int cyNew)
    {
        _cx = cxNew;
        _cy = cyNew;
    }

    void Paint (Canvas &amp; canvas, Model &amp; model);

protected:

   int _cx;
   int _cy;
};

void <font color="#cc0066"><b>View::Paint</b></font> (Canvas &amp; canvas, Model &amp; model)
{
    canvas.Text (12, 1, model.GetText(), model.GetLen());
    canvas.Line (10, 0, 10, _cy);
}</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->
<hr><!--Text-->
The canvas object encapsulates what is called a Device Context in Windows parlance. Our Canvas is very simple, it only knows how to print text and draw lines, but your Canvas can have many more methods that do creative things. We'll talk about Canvas more in one of the next tutorials.
<hr><!--End Text-->

         <!--Yellow background-->
        <table cellpadding=10 cellspacing=0 width="100%">
                <tr>
                      <td width=20>
                      <td bgcolor="#e0e080">
<pre><font face="courier">
class <font color="#cc0066"><b>Canvas</b></font>
{
public:
    operator HDC () { return _hdc; }

    void Line ( int x1, int y1, int x2, int y2 )
    {
        ::<font color="#000099"><b>MoveToEx</b></font> (_hdc, x1, y1, 0);
        ::<font color="#000099"><b>LineTo</b></font> (_hdc, x2, y2);
    }

    void Text (int x, int y, char const * buf, int cBuf)
    {
        ::<font color="#000099"><b>TextOut</b></font> ( _hdc, x, y, buf, cBuf );
    }

    void Char (int x, int y, char c)
    {
        ::<font color="#000099"><b>TextOut</b></font> (_hdc, x, y, &amp; c, 1);
    }

protected:

    <font color="#cc0066">// Protected constructor: You can't construct
    // a Canvas object, but you may be able
    // to construct objects derived from it.</font>
    Canvas (HDC hdc): _hdc (hdc) {}

    <font color="#009966">HDC</font>  _hdc;
};</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->
<hr><!--Text-->
The canvas that you create in response to <font color="#009966">WM_PAINT</font> message are of special kind. They obtain the device context by calling <font color="#000099"><b>BeginPaint</b></font> and release it by calling <font color="#000099"><b>EndPaint</b></font>. <font color="#009966">PAINTSTRUCT</font> contains additional information about which parts of the user area should be painted, etc. For now we ignore such details, but if you're serious about performance, you should learn more about it.
<hr><!--End Text-->

         <!--Yellow background-->
        <table cellpadding=10 cellspacing=0 width="100%">
                <tr>
                      <td width=20>
                      <td bgcolor="#e0e080">
<pre><font face="courier">
<font color="#cc0066">// Concrete example of canvas.
// Create this object after WM_PAINT message</font>

class <font color="#cc0066"><b>PaintCanvas</b></font>: public <font color="#cc0066"><b>Canvas</b></font>
{
public:
    <font color="#cc0066">// Constructor obtains the DC</font>
    PaintCanvas (HWND hwnd)
    : Canvas (::<font color="#000099"><b>BeginPaint</b></font> (hwnd, &amp; _paint)),
      _hwnd (hwnd)
    {}

    <font color="#cc0066">// Destructor releases the DC</font>
    ~PaintCanvas ()
    {
        ::<font color="#000099"><b>EndPaint</b></font>(_hwnd, &amp; _paint);
    }

protected:

    <font color="#009966">PAINTSTRUCT</font> _paint;
    HWND        _hwnd;
};
</font></pre><!--End Code-->
                      <td width=20>
        </table>
        <!--End of yellow background-->

<hr>
What would Windows programming be without <a href="controls.html">controls</a>?
<hr>
<!--End Text-->

   <!-- end main box -->
   </table>
   
   <td width=10><!-- Right margin -->
</table> <!-- End main table -->



<layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility=hidden id=a1 width=600 onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer>
</body>
</html>

⌨️ 快捷键说明

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